Why is my loop not working?

8 次查看(过去 30 天)
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