How to use array as input for a function and store outputs and then graph the function?
Show older comments
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
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 Comments
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
on 24 Sep 2023
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

