How to use array as input for a function and store outputs and then graph the function?

조회 수: 5 (최근 30일)
Hello! I am working on a code that samples a sinusoid signal and then qants to quantize the signal and find the SNR. I believe my code for those parts work, but my issue is that I have mutliple input values for the same function.
My prompt is:
Consider the signal x(t) = A cos(80t). Write a MATLAB script that generates 10,000 samples of x(t) at a sampling rate of fs =500 Hz for 10 log10(Px/V^2 ) = −40, −35, · · · , −5 dB. For each value of 10 log10(Px/V 2 ), quantize the samples with k = 7 bit uniform quantization with V = 1 and calculate the actual signal-to-quantization noise ratio in dB. Use MATLAB to plot the SNR versus 10 log10(Px/V^2 ).
My current code is:
clear all; close all;
fs = 500;
T = 1/fs;
k = 7;
m = 2^k;
t = 0:1/fs:((10000-1).*1/fs);
d = [-40 -35 -30 -25 -20 -15 -10 -5];
for i = 1:length(d)
A(i) = sqrt(2*(10.^(d(i)/10)));
x = A(i).*cos(80.*t);
v = 1;
del = 2*v/m;
partition = -v+del:del:-v+(m-1)*del;
levels = -v+del/2:del:-v+del/2+(m-1)*del;
[index,xq] = quantiz(x, partition, levels);
SNRuq = 10*log10(sum(abs(x).^2)./sum(abs(x-xq').^2));
mu = 255;
x2 = compand(x,mu,v,'mu/compressor');
[index, x2] = quantiz(x2,partition, levels);
xq = compand(x2, mu,v, 'mu/expander');
SNRcp = 10*log10(sum(abs(x).^2)./sum(abs(x-x2').^2));
figure
plot(SNRuq, d)
end
Error using plot
Vectors must be the same length.
my error is:
Error using plot
Vectors must be the same length.
Error in homework3project3 (line 30)
plot(SNRuq, d)
I am struggling with how to call the different inputs from the array and then store the values in another array to use later. I also am struggling with how to plot the SNR against the d[] values in my code since they are different size arrays in matlab. I appreciate any help, thank you!
  댓글 수: 2
AndreasO
AndreasO 2023년 9월 24일
You can write the SNRcp output into a matrix and then plot it after the loop, like this:
clear all; close all;
fs = 500;
T = 1/fs;
k = 7;
m = 2^k;
t = 0:1/fs:((10000-1).*1/fs);
d = [-40 -35 -30 -25 -20 -15 -10 -5];
for i = 1:length(d)
A(i) = sqrt(2*(10.^(d(i)/10)));
x = A(i).*cos(80.*t);
v = 1;
del = 2*v/m;
partition = -v+del:del:-v+(m-1)*del;
levels = -v+del/2:del:-v+del/2+(m-1)*del;
[index,xq] = quantiz(x, partition, levels);
SNRuq = 10*log10(sum(abs(x).^2)./sum(abs(x-xq').^2));
mu = 255;
x2 = compand(x,mu,v,'mu/compressor');
[index, x2] = quantiz(x2,partition, levels);
xq = compand(x2, mu,v, 'mu/expander');
SNRcp(i,:) = 10*log10(sum(abs(x).^2)./sum(abs(x-x2').^2));
end
plot(SNRcp.')
legend(num2str(d.'))
Nicole
Nicole 2023년 9월 24일
Thank you so much! I am also trying to graph the SNRcp, would I do the same thing that you did for SNRuq where its stored in a matrix and used the same plot function?

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 9월 24일
%No need of using these commands
%clear all; close all;
fs = 500;
T = 1/fs;
k = 7;
m = 2^k;
t = 0:1/fs:((10000-1).*1/fs);
d = (-40:5:-5);
%Bring independent operations out of the loop
v = 1;
del = 2*v/m;
partition = -v+del:del:-v+(m-1)*del;
levels = -v+del/2:del:-v+del/2+(m-1)*del;
mu = 255;
%Vectorize A
A = sqrt(2*(10.^(d/10)));
%number of elements in d
n = numel(d);
%Pre-allocate
[SNRuq,SNRcp] = deal(zeros(n,numel(t)));
%Define a function handle instead of repeatidely calculating
f = @(a,b) 10*log10(sum(abs(a).^2))./sum(abs(a-b').^2);
for i = 1:n
x = A(i).*cos(80.*t);
%Only assign outputs that are required
[~,xq] = quantiz(x, partition, levels);
SNRuq(i,:) = f(x,xq);
x2 = compand(x,mu,v,'mu/compressor');
[~, x2] = quantiz(x2,partition, levels);
%This line does not seem to have any use
%xq = compand(x2, mu,v, 'mu/expander');
SNRcp(i,:) = f(x,x2);
end
%Plot the outputs
figure
plot(d,SNRuq)
figure
plot(d,SNRcp)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by