1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2003 Doug Stewart <dastew@sympatico.ca>
3 ## Copyright (C) 2011 Alexander Klein <alexander.klein@math.uni-giessen.de>
5 ## This program is free software; you can redistribute it and/or modify it under
6 ## the terms of the GNU General Public License as published by the Free Software
7 ## Foundation; either version 3 of the License, or (at your option) any later
10 ## This program is distributed in the hope that it will be useful, but WITHOUT
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 ## You should have received a copy of the GNU General Public License along with
16 ## this program; if not, see <http://www.gnu.org/licenses/>.
18 ## Generate a butterworth filter.
19 ## Default is a discrete space (Z) filter.
21 ## [b,a] = butter(n, Wc)
22 ## low pass filter with cutoff pi*Wc radians
24 ## [b,a] = butter(n, Wc, 'high')
25 ## high pass filter with cutoff pi*Wc radians
27 ## [b,a] = butter(n, [Wl, Wh])
28 ## band pass filter with edges pi*Wl and pi*Wh radians
30 ## [b,a] = butter(n, [Wl, Wh], 'stop')
31 ## band reject filter with edges pi*Wl and pi*Wh radians
33 ## [z,p,g] = butter(...)
34 ## return filter as zero-pole-gain rather than coefficients of the
35 ## numerator and denominator polynomials.
37 ## [...] = butter(...,'s')
38 ## return a Laplace space filter, W can be larger than 1.
40 ## [a,b,c,d] = butter(...)
41 ## return state-space matrices
45 ## Proakis & Manolakis (1992). Digital Signal Processing. New York:
46 ## Macmillan Publishing Company.
48 function [a, b, c, d] = butter (n, W, varargin)
50 if (nargin>4 || nargin<2) || (nargout>4 || nargout<2)
54 ## interpret the input parameters
55 if (!(length(n)==1 && n == round(n) && n > 0))
56 error ("butter: filter order n must be a positive integer");
61 for i=1:length(varargin)
63 case 's', digital = 0;
64 case 'z', digital = 1;
65 case { 'high', 'stop' }, stop = 1;
66 case { 'low', 'pass' }, stop = 0;
67 otherwise, error ("butter: expected [high|stop] or [s|z]");
73 if (!(length(W)<=2 && (r==1 || c==1)))
74 error ("butter: frequency must be given as w0 or [w0, w1]");
75 elseif (!(length(W)==1 || length(W) == 2))
76 error ("butter: only one filter band allowed");
77 elseif (length(W)==2 && !(W(1) < W(2)))
78 error ("butter: first band edge must be smaller than second");
81 if ( digital && !all(W >= 0 & W <= 1))
82 error ("butter: critical frequencies must be in (0 1)");
83 elseif ( !digital && !all(W >= 0 ))
84 error ("butter: critical frequencies must be in (0 inf)");
87 ## Prewarp to the band edges to s plane
89 T = 2; # sampling frequency of 2 Hz
93 ## Generate splane poles for the prototype butterworth filter
95 C = 1; # default cutoff frequency
96 pole = C*exp(1i*pi*(2*[1:n] + n - 1)/(2*n));
97 if mod(n,2) == 1, pole((n+1)/2) = -1; end # pure real value at exp(i*pi)
101 ## splane frequency transform
102 [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
104 ## Use bilinear transform to convert poles to the z plane
106 [zero, pole, gain] = bilinear(zero, pole, gain, T);
109 ## convert to the correct output form
111 a = real(gain*poly(zero));
112 b = real(poly(pole));
119 [a, b, c, d] = zp2ss (zero, pole, gain);
124 %!shared sf, sf2, off_db
126 %! ##Sampling frequency must be that high to make the low pass filters pass.
127 %! sf = 6000; sf2 = sf/2;
128 %! data=[sinetone(5,sf,10,1),sinetone(10,sf,10,1),sinetone(50,sf,10,1),sinetone(200,sf,10,1),sinetone(400,sf,10,1)];
131 %! ##Test low pass order 1 with 3dB @ 50Hz
132 %! data=[sinetone(5,sf,10,1),sinetone(10,sf,10,1),sinetone(50,sf,10,1),sinetone(200,sf,10,1),sinetone(400,sf,10,1)];
133 %! [b, a] = butter ( 1, 50 / sf2 );
134 %! filtered = filter ( b, a, data );
135 %! damp_db = 20 * log10 ( max ( filtered ( end - sf : end, : ) ) );
136 %! assert ( [ damp_db( 4 ) - damp_db( 5 ), damp_db( 1 : 3 ) ], [ 6 0 0 -3 ], off_db )
139 %! ##Test low pass order 4 with 3dB @ 50Hz
140 %! data=[sinetone(5,sf,10,1),sinetone(10,sf,10,1),sinetone(50,sf,10,1),sinetone(200,sf,10,1),sinetone(400,sf,10,1)];
141 %! [b, a] = butter ( 4, 50 / sf2 );
142 %! filtered = filter ( b, a, data );
143 %! damp_db = 20 * log10 ( max ( filtered ( end - sf : end, : ) ) );
144 %! assert ( [ damp_db( 4 ) - damp_db( 5 ), damp_db( 1 : 3 ) ], [ 24 0 0 -3 ], off_db )
147 %! ##Test high pass order 1 with 3dB @ 50Hz
148 %! data=[sinetone(5,sf,10,1),sinetone(10,sf,10,1),sinetone(50,sf,10,1),sinetone(200,sf,10,1),sinetone(400,sf,10,1)];
149 %! [b, a] = butter ( 1, 50 / sf2, "high" );
150 %! filtered = filter ( b, a, data );
151 %! damp_db = 20 * log10 ( max ( filtered ( end - sf : end, : ) ) );
152 %! assert ( [ damp_db( 2 ) - damp_db( 1 ), damp_db( 3 : end ) ], [ 6 -3 0 0 ], off_db )
155 %! ##Test high pass order 4 with 3dB @ 50Hz
156 %! data=[sinetone(5,sf,10,1),sinetone(10,sf,10,1),sinetone(50,sf,10,1),sinetone(200,sf,10,1),sinetone(400,sf,10,1)];
157 %! [b, a] = butter ( 4, 50 / sf2, "high" );
158 %! filtered = filter ( b, a, data );
159 %! damp_db = 20 * log10 ( max ( filtered ( end - sf : end, : ) ) );
160 %! assert ( [ damp_db( 2 ) - damp_db( 1 ), damp_db( 3 : end ) ], [ 24 -3 0 0 ], off_db )
163 %! sf = 800; sf2 = sf/2;
164 %! data=[[1;zeros(sf-1,1)],sinetone(25,sf,1,1),sinetone(50,sf,1,1),sinetone(100,sf,1,1)];
165 %! [b,a]=butter ( 1, 50 / sf2 );
166 %! filtered = filter(b,a,data);
169 %! subplot ( columns ( filtered ), 1, 1)
170 %! plot(filtered(:,1),";Impulse response;")
171 %! subplot ( columns ( filtered ), 1, 2 )
172 %! plot(filtered(:,2),";25Hz response;")
173 %! subplot ( columns ( filtered ), 1, 3 )
174 %! plot(filtered(:,3),";50Hz response;")
175 %! subplot ( columns ( filtered ), 1, 4 )
176 %! plot(filtered(:,4),";100Hz response;")