Hello I need help with for loop incrementing. Filterbanks
2 views (last 30 days)
Show older comments
Hello i have attempted to make a for loop that adds band pass filters into one filter bank.
I would like to increment bfilterbank by the next bfilterbank after going through each loop.
ie: bfilterbank(n) + bfilterbank(n+1)+ bfilterbank(n+3) + bfilterbank(n+4)........
Thankyou in advance
f0=1600;
f1=7600;
fs=20000;
t=0:1/fs:0.5-(1/fs);
x1=1*sin(2*pi*f0*t);
x2=1*sin(2*pi*f1*t);
xin=x1+x2;
signal_power=var(xin)
%add in some white gaussian noise
xnse=wgn(1,10000,0.1,'linear');
noise_power=var(xnse)
SNR=10*log10(signal_power/noise_power)
in_file = xin + xnse;
%specify taylor window to be used for spectral estimation
w0=taylorwin(512,4,-80);
%Specify use of the Taylor window
t_window=taylorwin(51,20,-100);
N=input("Please Enter The Amount Of Filters Needed: ")
for k = 1:N
disp('Band Pass Filter \n')
%Filter Band pass filter cutoffs in Hz
Lc1(k)=input('Input The Lcl: ');
Uc1(k)=input('Input The Ucl: ');
g(k)=input('Input The Gain: ');
Lc1_norm(k)=2*Lc1(k)/fs;
Uc1_norm(k)=2*Uc1(k)/fs;
end
end
b=fir1(50,[Lc1_norm(k) Uc1_norm(k)],t_window);
bfilterbank= g(k).*b(k) ;
out_file = filter(bfilterbank,1,in_file);
freqz(bfilterbank,1);
title('FIR Filter #1 Magn. & Phase Function Plots');
Here is the plot that I am getting
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287866/image.png)
%------------------------------------------------------------------------------------------
%Here is the older code that give the answer that I want
Lc1=20;
Uc1=350;
Lc1_norm=2*Lc1/fs;
Uc1_norm=2*Uc1/fs;
%Filter 2 cutoffs in Hz
Lc2=250;
Uc2=4000;
Lc2_norm=2*Lc2/fs;
Uc2_norm=2*Uc2/fs;
Lc3=3500;
Uc3=9900;
Lc3_norm=2*Lc3/fs;
Uc3_norm=2*Uc3/fs;
g2=0.001
g1=10^(20/20)*0.001
g3=10^(-30/20)*0.001
b1=fir1(50,[Lc1_norm Uc1_norm],t_window);
b2=fir1(50,[Lc2_norm Uc2_norm],t_window);
b3=fir1(50,[Lc3_norm Uc3_norm],t_window);
g2=0.001
g1=10^(20/20)*0.001
g3=10^(-30/20)*0.001
bfilterbank = g1*b1 + g2*b2 + g3*b3;
out_file = filter(bfilterbank,1,in_file);
figure();
freqz(bfilterbank,1);
title('FIR Filter bank Magn. & Phase Function Plots');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287867/image.png)
0 Comments
Answers (2)
Mehmed Saad
on 27 Apr 2020
In your code, you are designing only 1 filter which is for the last value of k because fir1 is outside for loop
for k = 1:N
disp('Band Pass Filter \n')
%Filter Band pass filter cutoffs in Hz
Lc1(k)=input('Input The Lcl: ');
Uc1(k)=input('Input The Ucl: ');
g(k)=input('Input The Gain: ');
Lc1_norm(k)=2*Lc1(k)/fs;
Uc1_norm(k)=2*Uc1(k)/fs;
end
b=fir1(50,[Lc1_norm(k) Uc1_norm(k)],t_window);
bfilterbank= g(k).*b(k) ;
suppose we put b and bfilterbank inside for loop
Now the problem is that you are accessing b(k) which means if k is 1, you are accessing b(1) which is the first value (tap) of filter output, what you want is all the values from b so replace b(k) with b.
bfilterbank= g(k)*b ;
bfilterbank is not keeping the values of previous iterations which mean we will get last iteration value.
so we have to index it or store previous value and add in current value
Intialize filterbank to zero (before for loop). and add previous iteration value in current iteration
bfilterbank= bfilterbank + g(k)*b ;
Add new values in filterbank rows (initialization not necessary)
bfilterbank(k,:) = g(k)*b ;
and sum them after for loop by simply applying sum command
Mrutyunjaya Hiremath
on 27 Apr 2020
f0=1600;
f1=7600;
fs=20000;
t=0:1/fs:0.5-(1/fs);
x1=1*sin(2*pi*f0*t);
x2=1*sin(2*pi*f1*t);
xin=x1+x2;
signal_power=var(xin)
%add in some white gaussian noise
xnse=wgn(1,10000,0.1,'linear');
noise_power=var(xnse)
SNR=10*log10(signal_power/noise_power)
in_file = xin + xnse;
%specify taylor window to be used for spectral estimation
w0=taylorwin(512,4,-80);
%Specify use of the Taylor window
t_window=taylorwin(51,20,-100);
N=input('Please Enter The Amount Of Filters Needed: ')
bfilterbankAll= 0;
for k = 1:N
disp('Band Pass Filter \n')
%Filter Band pass filter cutoffs in Hz
Lc1(k)=input('Input The Lcl: ');
Uc1(k)=input('Input The Ucl: ');
g(k)=input('Input The Gain: ');
Lc1_norm(k)=2*Lc1(k)/fs;
Uc1_norm(k)=2*Uc1(k)/fs;
b = fir1(50,[Lc1_norm(k) Uc1_norm(k)],t_window);
bfilterbank(k,:) = g(k) * b ;
bfilterbankAll = bfilterbankAll + bfilterbank(k,:);
end
out_file = filter(bfilterbank,1,in_file);
figure();
freqz(bfilterbankAll,1);
title('FIR Filter #1 Magn. & Phase Function Plots');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/287883/image.png)
See Also
Categories
Find more on Analog Filters in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!