Why is my loop not working?

3 vues (au cours des 30 derniers jours)
Giggs B.
Giggs B. le 21 Juil 2021
Commenté : Giggs B. le 21 Juil 2021
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!

Réponse acceptée

Stephen23
Stephen23 le 21 Juil 2021
Modifié(e) : Stephen23 le 21 Juil 2021
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 commentaires
Giggs B.
Giggs B. le 21 Juil 2021
Oh wow, that was a silly mistake. It's orking fine now. Thank you so much!
Giggs B.
Giggs B. le 21 Juil 2021
Yes, I changed the variable name and it is working as expected :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by