]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/besself.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / besself.m
1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2003 Doug Stewart <dastew@sympatico.ca>
3 ## Copyright (C) 2009 Thomas Sailer <t.sailer@alumni.ethz.ch>
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 bessel filter.
19 ## Default is a Laplace space (s) filter.
20 ## 
21 ## [b,a] = besself(n, Wc)
22 ##    low pass filter with cutoff pi*Wc radians
23 ##
24 ## [b,a] = besself(n, Wc, 'high')
25 ##    high pass filter with cutoff pi*Wc radians
26 ##
27 ## [b,a] = besself(n, [Wl, Wh])
28 ##    band pass filter with edges pi*Wl and pi*Wh radians
29 ##
30 ## [b,a] = besself(n, [Wl, Wh], 'stop')
31 ##    band reject filter with edges pi*Wl and pi*Wh radians
32 ##
33 ## [z,p,g] = besself(...)
34 ##    return filter as zero-pole-gain rather than coefficients of the
35 ##    numerator and denominator polynomials.
36 ## 
37 ## [...] = besself(...,'z')
38 ##     return a discrete space (Z) filter, W must be less than 1.
39 ## 
40 ## [a,b,c,d] = besself(...)
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] = besself (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 ("besself: filter order n must be a positive integer");
57   end
58
59   stop = 0;
60   digital = 0;  
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 ("besself: 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 ("besself: frequency must be given as w0 or [w0, w1]");
75   elseif (!(length(W)==1 || length(W) == 2))
76     error ("besself: only one filter band allowed");
77   elseif (length(W)==2 && !(W(1) < W(2)))
78     error ("besself: first band edge must be smaller than second");
79   endif
80
81   if ( digital && !all(W >= 0 & W <= 1))
82     error ("besself: critical frequencies must be in (0 1)");
83   elseif ( !digital && !all(W >= 0 ))
84     error ("besself: 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 bessel filter
94   [zero, pole, gain] = besselap(n);
95
96   ## splane frequency transform
97   [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
98
99   ## Use bilinear transform to convert poles to the z plane
100   if digital
101      [zero, pole, gain] = bilinear(zero, pole, gain, T);
102   endif
103
104   ## convert to the correct output form
105   if nargout==2, 
106     a = real(gain*poly(zero));
107     b = real(poly(pole));
108   elseif nargout==3,
109     a = zero;
110     b = pole;
111     c = gain;
112   else
113     ## output ss results 
114     [a, b, c, d] = zp2ss (zero, pole, gain);
115   endif
116
117 endfunction