]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/butter.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / butter.m
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>
4 ##
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
8 ## version.
9 ##
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
13 ## details.
14 ##
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/>.
17
18 ## Generate a butterworth filter.
19 ## Default is a discrete space (Z) filter.
20 ## 
21 ## [b,a] = butter(n, Wc)
22 ##    low pass filter with cutoff pi*Wc radians
23 ##
24 ## [b,a] = butter(n, Wc, 'high')
25 ##    high pass filter with cutoff pi*Wc radians
26 ##
27 ## [b,a] = butter(n, [Wl, Wh])
28 ##    band pass filter with edges pi*Wl and pi*Wh radians
29 ##
30 ## [b,a] = butter(n, [Wl, Wh], 'stop')
31 ##    band reject filter with edges pi*Wl and pi*Wh radians
32 ##
33 ## [z,p,g] = butter(...)
34 ##    return filter as zero-pole-gain rather than coefficients of the
35 ##    numerator and denominator polynomials.
36 ## 
37 ## [...] = butter(...,'s')
38 ##     return a Laplace space filter, W can be larger than 1.
39 ## 
40 ## [a,b,c,d] = butter(...)
41 ##  return  state-space matrices 
42 ##
43 ## References: 
44 ##
45 ## Proakis & Manolakis (1992). Digital Signal Processing. New York:
46 ## Macmillan Publishing Company.
47
48 function [a, b, c, d] = butter (n, W, varargin)
49   
50   if (nargin>4 || nargin<2) || (nargout>4 || nargout<2)
51     print_usage;
52   end
53
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");
57   end
58
59   stop = 0;
60   digital = 1;
61   for i=1:length(varargin)
62     switch varargin{i}
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]");
68     endswitch
69   endfor
70
71
72   [r, c]=size(W);
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");
79   endif
80
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)");
85   endif
86
87   ## Prewarp to the band edges to s plane
88   if digital
89     T = 2;       # sampling frequency of 2 Hz
90     W = 2/T*tan(pi*W/T);
91   endif
92
93   ## Generate splane poles for the prototype butterworth filter
94   ## source: Kuc
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)
98   zero = [];
99   gain = C^n;
100
101   ## splane frequency transform
102   [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
103
104   ## Use bilinear transform to convert poles to the z plane
105   if digital
106      [zero, pole, gain] = bilinear(zero, pole, gain, T);
107   endif
108
109   ## convert to the correct output form
110   if nargout==2, 
111     a = real(gain*poly(zero));
112     b = real(poly(pole));
113   elseif nargout==3,
114     a = zero;
115     b = pole;
116     c = gain;
117   else
118     ## output ss results 
119     [a, b, c, d] = zp2ss (zero, pole, gain);
120   endif
121
122 endfunction
123
124 %!shared sf, sf2, off_db
125 %! off_db = 0.5;
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)];
129
130 %!test
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 )
137
138 %!test
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 )
145
146 %!test
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 )
153
154 %!test
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 )
161
162 %!demo
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);
167 %!
168 %! clf
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;")