Indexing into a loop
Afficher commentaires plus anciens
So I am trying to index into this second loop in order to be able to for example at the top of the code be able to change filter_number and be able to see what the output of that filter is. Would I have to change how I wrote the loop? Thanks for your time!
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);
end
[h{ii},f] = freqz(bpfilt{ii}.Coefficients,1,4*8192,fs);
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) %THIS ONE!!!
[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))
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Scopes and Data Logging dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




