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

5 views (last 30 days)
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 Comments
AndreasO
AndreasO on 24 Sep 2023
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 on 24 Sep 2023
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?

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 24 Sep 2023
%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)

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!