필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
AJ
AJ 2021년 12월 16일
댓글: Awais Saeed 2021년 12월 16일
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

답변 (2개)

Awais Saeed
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)
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
AJ
AJ 2021년 12월 16일
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.
Awais Saeed
Awais Saeed 2021년 12월 16일
I'm glad to be of help!

댓글을 달려면 로그인하십시오.


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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by