Need help writing a code with a summation divided by a summation

1 view (last 30 days)
AJ
AJ on 16 Dec 2021
Commented: Awais Saeed on 16 Dec 2021
Hi. I've been stuck trying to write a code that would take values from two arrays I created, applying them to an equation, and then plotting the frequency respose of it.
Below is the equation I'm trying to execute and plot:
Current code:
%Arrays a and b that were created:
a = [0.001, -0.014, 0.098, -0.424, 1.29, -2.96, 5.25, -7.35, 8.21, -7.3513, 5.25, -2.96, 1.297, -0.424, 0.098, -0.014, 0.001];
b = [1, -10.780, 55.31, -179.24, 410.38, -703.78, 934.93, -981.27, 822.29, -551.96, 295.81, -125.25, 41.08, -10.09, 1.75, -0.192, 0.01];
N = 2048; %samples
w = linspace(-pi,pi,N)
k = 0:16;
H = 0;
for ii = 1:length(a)
Ha = H + sum(ii.*exp(j.*w).^(16-k)); %Numerator
end
for jj = 1:length(b)
Hb = H + sum(jj.*exp(j.*w).^(16-k)); %Denominator
end
H_ejw = Ha./Hb;
plot(w,abs(H_ejw));
title('Magnitude Response')
I keep getting the error "Arrays have incompatible sizes for this operation." I'm wondering if it's due to w?
Thank you

Answers (2)

Awais Saeed
Awais Saeed on 16 Dec 2021
I do not know what graph are you expecting but your loops are for sure not storing any values. You have to store the computed values and then take sum of those. I still do not know if you want to sum rows or coloums. I am suming rows though. You can change it if you want to sum columns.
%Arrays a and b that were created:
a = [0.001, -0.014, 0.098, -0.424, 1.29, -2.96, 5.25, -7.35, 8.21, -7.3513, 5.25, -2.96, 1.297, -0.424, 0.098, -0.014, 0.001];
b = [1, -10.780, 55.31, -179.24, 410.38, -703.78, 934.93, -981.27, 822.29, -551.96, 295.81, -125.25, 41.08, -10.09, 1.75, -0.192, 0.01];
N = 2048; %samples
w = linspace(-pi,pi,N);
k = 0:16;
H = 0;
Ha = [];
for ii = 1:length(a)
Ha(ii,:) = H + a(ii).*(exp(j.*w)).^(16-k(ii)); % Numerator
Hb(ii,:) = H + b(ii).*(exp(j.*w)).^(16-k(ii)); % Denominator
end
% sizes before summation
size(Ha)
ans = 1×2
17 2048
size(Hb)
ans = 1×2
17 2048
% sizes after summation
Ha = sum(Ha,1);
Hb = sum(Hb,1);
size(Hb)
ans = 1×2
1 2048
size(Ha)
ans = 1×2
1 2048
H_ejw = Ha./Hb;
plot(w,abs(H_ejw));
title('Magnitude Response')
  2 Comments

Sign in to comment.


Torsten
Torsten on 16 Dec 2021
a = [0.001, -0.014, 0.098, -0.424, 1.29, -2.96, 5.25, -7.35, 8.21, -7.3513, 5.25, -2.96, 1.297, -0.424, 0.098, -0.014, 0.001];
b = [1, -10.780, 55.31, -179.24, 410.38, -703.78, 934.93, -981.27, 822.29, -551.96, 295.81, -125.25, 41.08, -10.09, 1.75, -0.192, 0.01];
N = 2048;
w = linspace(-pi,pi,N);
Ha = zeros(N,1);
Hb = zeros(N,1);
for i = 1:N
vec = (exp(j*w(i))).^(16-(0:16));
Ha(i) = a*vec.';
Hb(i) = b*vec.';
end
H = Ha./Hb
plot(w,abs(H));
title('Magnitude Response')

Community Treasure Hunt

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

Start Hunting!

Translated by