annotate aim-mat/tools/@signal/bandpass.asv @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 74dedb26614d
children
rev   line source
tomwalters@0 1 % method of class @signal
tomwalters@0 2 %
tomwalters@0 3 % INPUT VALUES:
tomwalters@0 4 %
tomwalters@0 5 % RETURN VALUE:
tomwalters@0 6 %
tomwalters@0 7 %
tomwalters@0 8 % (c) 2003, University of Cambridge, Medical Research Council
tomwalters@0 9 % Stefan Bleeck (stefan@bleeck.de)
tomwalters@0 10 % http://www.mrc-cbu.cam.ac.uk/cnbh/aimmanual
tomwalters@0 11 % $Date: 2003/07/17 10:56:16 $
tomwalters@0 12 % $Revision: 1.2 $
tomwalters@0 13
tomwalters@0 14 function filtered_sig=bandpass(sig,lowfrequency,highfrequency,stopbandwidth_low,stopbandwidth_high,ripple,stopbandatten)
tomwalters@0 15 % hack for an phase true bandpassfilter with cutoff at frequency
tomwalters@0 16 % used is a ButterworthFilter
tomwalters@0 17 % this is all crap, but it does not work properly otherwise.
tomwalters@0 18
tomwalters@0 19 grafix=0;
tomwalters@0 20
tomwalters@0 21 if nargin < 7
tomwalters@0 22 stopbandatten=60; % in dB - how many dB the signal is reduced in the stopband at least
tomwalters@0 23 end
tomwalters@0 24 if nargin < 6
tomwalters@0 25 ripple=3; % in dB = ripple in the passband
tomwalters@0 26 end
tomwalters@0 27 if nargin < 5
tomwalters@0 28 stopbandwidth_high=highfrequency*2; % one octave above
tomwalters@0 29 end
tomwalters@0 30 if nargin < 4
tomwalters@0 31 stopbandwidth_low=lowfrequency/2; % one octave above
tomwalters@0 32 end
tomwalters@0 33
tomwalters@0 34 % I dont understand enough to make it work with a butterwort filer.
tomwalters@0 35 % easy solution: first perform a lowpass, then a highpass filter...
tomwalters@0 36 % sorry, Thanks for any help!
tomwalters@0 37 if grafix
tomwalters@0 38 figure(23534)
tomwalters@0 39 plot(powerspectrum(sig),[100,getsr(sig)/2],'b')
tomwalters@0 40 hold on
tomwalters@0 41 end
tomwalters@0 42
tomwalters@0 43 if highfrequency>0
tomwalters@0 44 filtered_sig_low=lowpass(sig,highfrequency,highfrequency+stopbandwidth_high,ripple,stopbandatten);
tomwalters@0 45 else
tomwalters@0 46 filtered_sig_low=sig;
tomwalters@0 47 end
tomwalters@0 48
tomwalters@0 49 if grafix
tomwalters@0 50 figure(23534)
tomwalters@0 51 plot(powerspectrum(filtered_sig_low),[100,getsr(filtered_sig_low)/2],'r')
tomwalters@0 52 end
tomwalters@0 53
tomwalters@0 54
tomwalters@0 55 if lowfrequency>0
tomwalters@0 56 filtered_sig=highpass(filtered_sig_low,lowfrequency,lowfrequency-stopbandwidth_low,ripple,stopbandatten);
tomwalters@0 57 if grafix
tomwalters@0 58 figure(23534)
tomwalters@0 59 plot(powerspectrum(filtered_sig),[100,getsr(filtered_sig_low)/2],'g')
tomwalters@0 60 end
tomwalters@0 61
tomwalters@0 62
tomwalters@0 63 return
tomwalters@0 64
tomwalters@0 65
tomwalters@0 66 % if nargin < 4
tomwalters@0 67 % ripple=1; % in dB = ripple in the passband
tomwalters@0 68 % end
tomwalters@0 69 % if nargin <3
tomwalters@0 70 % stopband=frequency*2; % eine Oktave drüber
tomwalters@0 71 % end
tomwalters@0 72
tomwalters@0 73 ripple=3;
tomwalters@0 74
tomwalters@0 75 nyquist=getsr(sig)/2;
tomwalters@0 76 % fre_low=2;
tomwalters@0 77 % fre_high=frequency;
tomwalters@0 78
tomwalters@0 79 Wpass=[lowfrequency highfrequency]/nyquist;
tomwalters@0 80 Wstop=[lowfrequency-stopbandwidth highfrequency+stopbandwidth]/nyquist;
tomwalters@0 81 [n,Wn] = buttord(Wpass,Wstop,ripple,stopbandatten);
tomwalters@0 82 % [n,Wn] = buttord(fre_high/nyquist,(fre_high+stopband)/nyquist,ripple,stopbandatten);
tomwalters@0 83 % Berechne den IIR-Filter
tomwalters@0 84 [b,a] = butter(n,Wn);
tomwalters@0 85
tomwalters@0 86 % zum testen:
tomwalters@0 87 freqz(b,a,128,getsr(sig))
tomwalters@0 88
tomwalters@0 89 vals=sig.werte';
tomwalters@0 90
tomwalters@0 91 % fill the part behind the signal and in front of the signal with
tomwalters@0 92 % values
tomwalters@0 93 % firstval=vals(1);
tomwalters@0 94 % lastval=vals(end);
tomwalters@0 95 % nr_vals=length(vals);
tomwalters@0 96 % vals=[ones(1,nr_vals)*firstval vals ones(1,nr_vals)*lastval];
tomwalters@0 97
tomwalters@0 98 plot(vals)
tomwalters@0 99 hold on
tomwalters@0 100
tomwalters@0 101 nvals = filter(b,a,vals);
tomwalters@0 102 % nvals = filtfilt(b,a,vals);
tomwalters@0 103 % extract the values back
tomwalters@0 104 % nvals=nvals(nr_vals+1:2*nr_vals);
tomwalters@0 105
tomwalters@0 106 plot(nvals,'r')
tomwalters@0 107
tomwalters@0 108 % filtered_sig=0;
tomwalters@0 109 % return
tomwalters@0 110
tomwalters@0 111 filtered_sig=sig; % a copy of the old one
tomwalters@0 112 newname=sprintf('Bandpass filterd (%3.2fkHz - %3.2fkHz) Signal: %s',lowfrequency/1000,highfrequency/1000,getname(sig));
tomwalters@0 113 filtered_sig=setname(filtered_sig,newname);
tomwalters@0 114 filtered_sig.werte=nvals';
tomwalters@0 115
tomwalters@0 116 % figure(235423)
tomwalters@0 117 % plot(sig);
tomwalters@0 118 % hold on
tomwalters@0 119 % plot(filtered_sig,'g');
tomwalters@0 120 % s=0;