Why is my loop not working?

조회 수: 7 (최근 30일)
Giggs B.
Giggs B. 2021년 7월 21일
댓글: Giggs B. 2021년 7월 21일
Hi,
I have a code, where I am using elliptical bandpass filter and trying to filter some signals and calculate total power for them, and this should happen for cut-off frequency starting from 50 upto 5000 with a step size of 50. My code works fine if I run for each frequency seperately, but when I run inside all loops to get values all at once, I receive this problem:
>>Total_power
2.5945
Error using ellip (line 60)
Wn must be a one or two element vector.
Error in total_power (line 19)
[b,a]=ellip(1,20,25,[a 5000]/(fs/2),'bandpass');
My code is as follows:
close all;
%Filter design
a=0;
b=10;
%x=zeros[];
for k=1:100 %%here loop runs for 100 times since a goes from 50 to 5000
a=a+50;
[b,a]=ellip(1,20,25,[a 5000]/(fs/2),'bandpass');
% 2
y2=audioread('2.mp3');
y_h2=filter(b,a,y2);
power2=rms(y_h2)^2;
% 3
y3=audioread('3.mp3');
y_h3=filter(b,a,y3);
power3=rms(y_h3)^2;
%Power calculations
PowerN=((0.14*power2)+(0.14*power3))/2;
%%%%%%%%%%%
[x,fs]=audioread('fr1.mp3');
x_h=filter(b,a,x);
PowerW=rms(x_h)^2; %RMS formula
%%%%%%Figure of Merit%%%%%%
Total_power= PowerW/PowerN;
disp(Total_power)
end
Please help! Thanks!

채택된 답변

Stephen23
Stephen23 2021년 7월 21일
편집: Stephen23 2021년 7월 21일
Look closely at the variable a:
a=0;
for ..
a=a+50;
[b,a]=ellip(..[a 5000]..);
..
end
At the start of the first iteration a = 0, to which you add 50. However you then completely replace that variable a with an entirely different one (the second output from ELLIP, which is a vector of transfer function coefficients of the filter).
After that it appears that you assume that a is still as scalar, whereas in fact you have replaced it with a vector.
  댓글 수: 2
Giggs B.
Giggs B. 2021년 7월 21일
Oh wow, that was a silly mistake. It's orking fine now. Thank you so much!
Giggs B.
Giggs B. 2021년 7월 21일
Yes, I changed the variable name and it is working as expected :)

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by