Need help writing a code with a summation divided by a summation
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
Awais Saeed
2021년 12월 16일
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)
size(Hb)
% sizes after summation
Ha = sum(Ha,1);
Hb = sum(Hb,1);
size(Hb)
size(Ha)
H_ejw = Ha./Hb;
plot(w,abs(H_ejw));
title('Magnitude Response')
Torsten
2021년 12월 16일
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')
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!