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