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

1 view (last 30 days)
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
AJ
AJ on 16 Dec 2021
Awais, thanks for your help. I used your code and made some tweaks to get the plot I needed, and was able to find my mistakes.
My values for a and b were originally generated using a function that was given to me, but I didn't know how to correctly apply it to a for loop. I thought writing these values out long hand would've been fine, but after running it through, the plot wasn't correct. I ended up deleting my arrays, used the generated values for a and b, and then it gave me the correct plot.

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')

Categories

Find more on Mathematics 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!