# Why is my loop not working?

1 ビュー (過去 30 日間)
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
y_h2=filter(b,a,y2);
power2=rms(y_h2)^2;
% 3
y_h3=filter(b,a,y3);
power3=rms(y_h3)^2;
%Power calculations
PowerN=((0.14*power2)+(0.14*power3))/2;
%%%%%%%%%%%
x_h=filter(b,a,x);
PowerW=rms(x_h)^2; %RMS formula
%%%%%%Figure of Merit%%%%%%
Total_power= PowerW/PowerN;
disp(Total_power)
end

サインインしてコメントする。

### 採用された回答

Stephen 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 件のコメント表示非表示 1 件の古いコメント
Giggs B. 2021 年 7 月 21 日
Yes, I changed the variable name and it is working as expected :)

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!