Use output of a loop as its input to a filter
7 views (last 30 days)
Show older comments
S
on 22 Jan 2024
Commented: Sulaymon Eshkabilov
on 22 Jan 2024
I am trying to build a cascade of filters so that the output of one is the input to the next one. The filters are all bandpass filters and I want to be able to have an input signal and have it run through all 32 bandpass filters in series. I also ultimately want to be able to specify a filter number to be able to look at the output after that specific filter number. This is what I have so far, I am having issues figuring out how to make the bottom loop apply the filter desgined in the top part of the code and how to make a loop which takes its own output and uses it as input. Thank you for your time! This is what I have:
close all
fs = 16e3;
numFilts = 32;
filter_number = 10;
%range = [50 8000];
CF1=linspace(50, 8000, 32) -50;
CF2=linspace(50, 8000, 32) +50;
for ii = 1:numel(CF1)-2
bpfilt{ii} = designfilt( ...
'bandpassfir', ...
'FilterOrder',20, ...
'CutoffFrequency1',CF1(ii+1), ...
'CutoffFrequency2',CF2(ii+1), ...
'SampleRate',fs);
[h{ii},f] = freqz(bpfilt{ii}.Coefficients,1,4*8192,fs);
end
figure
subplot(211)
plot(f,db(abs([h{:}])));
title('Magnitude')
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
subplot(212);
plot(f,180/pi*(angle([h{:}])));
title('Phase')
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
impulse_input = 0*fs;
impulse_input(1) = 1;
for ii = 1:numFilts-1
[y,zf]=filter(impulse_input,:)
end
0 Comments
Accepted Answer
Sulaymon Eshkabilov
on 22 Jan 2024
This is the code:
close all
fs = 16e3;
numFilts = 32;
filter_number = 10;
%range = [50 8000];
CF1=linspace(50, 8000, numFilts+2) -50;
CF2=linspace(50, 8000, numFilts+2) +50;
for ii = 1:numel(CF1)-2
bpfilt{ii} = designfilt( ...
'bandpassfir', ...
'FilterOrder',20, ...
'CutoffFrequency1',CF1(ii+1), ...
'CutoffFrequency2',CF2(ii+1), ...
'SampleRate',fs);
[h{ii},f] = freqz(bpfilt{ii}.Coefficients,1,4*8192,fs);
end
figure
subplot(211)
plot(f,db(abs([h{:}])));
title('Magnitude')
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
subplot(212);
plot(f,180/pi*(angle([h{:}])));
title('Phase')
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
impulse_input = 0*fs;
impulse_input(1) = 1;
%%
% Reference signal with some white noise to benchmarch the created filter performances
t = linspace(0,2*pi,200);
rng(13) % Make it repeatable
x = sin(t) + 0.25*rand(size(t)); % Ref Signal with a noise
% Simulation of 1-D digital filter: x_filtered = filter(b, a, x);
a = 1;
figure
hold on
CoLoR = rand(numel(bpfilt), 3);
for ii = 1:numel(bpfilt)
[x_filtered(ii,:),zf(:,ii)]=filter(bpfilt{1, ii}.Coefficients, a, x);
plot(t,x_filtered(ii,:), 'LineWidth', 1.25, 'Color', CoLoR(ii,:))
LEGs{ii} = ['Filter # ' num2str(ii)];
legend(LEGs{:})
end
plot(t, x, 'k-', 'LineWidth', 2, 'DisplayName', 'Data')
xlabel('t')
ylabel('x(t) & x_{filtered} (t)')
grid on
legend('Show')
fprintf('Number of generated filters: %d \n', numel(bpfilt))
More Answers (1)
Sulaymon Eshkabilov
on 22 Jan 2024
If understood your question correctly, this is what you are trying to simulate, e.g.:
close all
fs = 16e3;
numFilts = 32;
filter_number = 10;
CF1=linspace(50, 8000, 32) -50;
CF2=linspace(50, 8000, 32) +50;
for ii = 1:numel(CF1)-2
bpfilt{ii} = designfilt( ...
'bandpassfir', ...
'FilterOrder',20, ...
'CutoffFrequency1',CF1(ii+1), ...
'CutoffFrequency2',CF2(ii+1), ...
'SampleRate',fs);
[h{ii},f] = freqz(bpfilt{ii}.Coefficients,1,4*8192,fs);
end
figure
subplot(211)
plot(f,db(abs([h{:}])));
title('Magnitude')
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
subplot(212);
plot(f,180/pi*(angle([h{:}])));
title('Phase')
ylabel('Phase (degrees)')
xlabel('Frequency (Hz)')
impulse_input = 0*fs;
impulse_input(1) = 1;
%%
% Create a reference signal with some white noise to benchmark the created filter performances
t = linspace(0,2*pi,200);
rng(13) % Make it repeatable
x = sin(t) + 0.25*rand(size(t)); % Ref Signal with a noise
% Simulation of 1-D digital filter: x_filtered = filter(b, a, x);
a = 1;
figure % Visualize Filter performances:
hold on
CoLoR = rand(numel(bpfilt), 3);
for ii = 1:numel(bpfilt)
[x_filtered(ii,:),zf(:,ii)]=filter(bpfilt{1, ii}.Coefficients, a, x);
plot(t,x_filtered(ii,:), 'LineWidth', 1.25, 'Color', CoLoR(ii,:))
LEGs{ii} = ['Filter # ' num2str(ii)];
legend(LEGs{:})
end
plot(t, x, 'k-', 'LineWidth', 2, 'DisplayName', 'Data')
xlabel('t')
ylabel('x(t) & x_{filtered} (t)')
grid on
legend('Show')
11 Comments
Sulaymon Eshkabilov
on 22 Jan 2024
Just copy and paste my code above and it should give you 32 filters as shown above.
All the best.
See Also
Categories
Find more on Digital Filter Design 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!