Confused on making a function..
    7 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hey all,
I understand functions to a degree or the basic principles but i'd ideally like to perform something quite complex as a function rather than a for loop i have it in already. Below is my code:
        % CREATE ALL BANDPASS FILTERS..
        BandPassALL{1}=designfilt('bandpassiir', 'StopbandFrequency1', 247, 'PassbandFrequency1', 255, 'PassbandFrequency2', 269, 'StopbandFrequency2', 277, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        BandPassALL{2}=designfilt('bandpassiir', 'StopbandFrequency1', 279, 'PassbandFrequency1', 287, 'PassbandFrequency2', 301, 'StopbandFrequency2', 309, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        BandPassALL{3}=designfilt('bandpassiir', 'StopbandFrequency1', 315, 'PassbandFrequency1', 323, 'PassbandFrequency2', 337, 'StopbandFrequency2', 345, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        BandPassALL{4}=designfilt('bandpassiir', 'StopbandFrequency1', 335, 'PassbandFrequency1', 343, 'PassbandFrequency2', 357, 'StopbandFrequency2', 365, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        BandPassALL{5}=designfilt('bandpassiir', 'StopbandFrequency1', 377, 'PassbandFrequency1', 385, 'PassbandFrequency2', 399, 'StopbandFrequency2', 407, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        BandPassALL{6}=designfilt('bandpassiir', 'StopbandFrequency1', 425, 'PassbandFrequency1', 433, 'PassbandFrequency2', 447, 'StopbandFrequency2', 455, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        BandPassALL{7}=designfilt('bandpassiir', 'StopbandFrequency1', 479, 'PassbandFrequency1', 487, 'PassbandFrequency2', 501, 'StopbandFrequency2', 509, 'StopbandAttenuation1', 60, 'PassbandRipple', 1, 'StopbandAttenuation2', 60, 'SampleRate', 44100);
        for i=1:7;
           Filter_Out{i}=filter(BandPassALL{i},Wave);
           Filter_Out_Pos{i}=abs(Filter_Out{i});
           Clean_Sig{i}=zeros(1,length(Wave));
           for j = 1:1:length(Filter_Out_Pos{i});
               if (Filter_Out_Pos{i} > 0.5)
                   Clean_Sig{i}(j) = 1;
               end
           end
           subplot(7,1,i), plot(Clean_Sig{i});
        end
Wave is the samples vector of an audioread. I only want to possibly convert the for loop part into a function. As it is at the moment, it doesn't do anything, well i'm sure it does as my hd fan goes crazy but it doesn't produce any plots or weirdly any errors so although i thought the line:
Clean_Sig{i}(j) = 1;
...would be an issue as it looks wrong, it doesn't appear so..
Apart from a function, is there a quicker way to perform what i need. Loops and ifs are slow in matlab aren't they?
Thanks,
Paul..
0 Commenti
Risposte (1)
  Star Strider
      
      
 il 20 Nov 2015
        I am not certain what you want to do from reading your code. My filter design procedure for a bank of filters is:
Fs = 8200;                                      % Samping Frequency (Hz)
Fn = Fs/2;                                      % Nyquist Frequency
pf = linspace(20,4000,17);                      % Passband Frequencies
cf = pf(1:end-1)+(pf(2)-pf(1))/2;               % Centre Frequencies
for k1 = 1:length(cf)
    [z(k1,:),p(k1,:),k(k1)] = butter(7, [pf(k1) pf(k1+1)]/Fn);
    [sos{k1},g{k1}] = zp2sos(z(k1,:),p(k1,:),k(k1));
    [h(k1,:),w(k1,:)] = freqz(sos{k1},512,Fs);
end
figure(1)
plot(w([1 16],:), abs(h([1 16],:)))
grid
% axis([0  0.2    ylim])
figure(2)
freqz(sos{1})
hold on
for k1 = 2:16
    freqz(sos{k1})
end
hold off
This snippet just designs them and displays their transfer functions. It would be easy to add code that actually filters a signal for each filter, then store the results in a matrix. I always use the filtfilt function to do the actual filtering, since it does not induce the phase distortion that filter does.
0 Commenti
Vedere anche
Categorie
				Scopri di più su Filter Analysis in Help Center e File Exchange
			
	Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!