Construction, frequency and impulse response of Butterworth filters
Contents
Set up Butterworth filter
Type help butter to get more info from Matlab SP toolbox
N = 4; % Filter order is N. Try N= 2,8 as well to see sensitivities Wn = 0.4; % Threshold frequency (in Nyquist units of 1/(2*dt)) [b a] = butter(N,Wn) % Coefficients for 2N-order low-pass Butterworth filter % Use v = filter(b,a,u) to filter the time series u
b = 0.0466 0.1863 0.2795 0.1863 0.0466 a = 1.0000 -0.7821 0.6800 -0.1827 0.0301
Plot squared amplitude and phase of filter response vs. frequency
clf subplot(2,2,1) [R,w]=freqz(b,a); ymax = 1.2; plot(w/pi,abs(R).^2,'b',[Wn Wn],[0 ymax],'k--'); % Filter power ylim([0 ymax]) xlabel('freq [\Deltat^{-1}]') ylabel('|R|^2') title(['Butterworth low pass, N = ', int2str(N)]) subplot(2,2,2) phase = angle(R); jump = round(diff(phase)/(2*pi)); cycles = cumsum(jump); phase = phase - 2*pi*[0; cycles]; plot(w/pi,phase/(2*pi)); xlabel('freq [\Deltat^{-1}]') ylabel('phase(R) [cycles]') % Impulse response to a signal [1 0 ....] subplot(2,2,3) impz(b,a,20) % Filtering a square wave subplot(2,2,4) f = 0.1; % Square wave frequency (inverse period) nt = 35; % Signal length j = 1:nt; u = sign(cos(2*pi*f*(j-1))); % Square wave signal v = filter(b,a,u); % Filter once v2 = filtfilt(b,a,u); % Filter again backward to undo phase delays (if needed) plot(j,u,'k-',j,v,'r-',j,v2,'m-')
