]> Creatis software - CreaPhase.git/blob - octave_packages/signal-1.1.3/cheby2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / signal-1.1.3 / cheby2.m
1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2003 Doug Stewart <dastew@sympatico.ca>
3 ##
4 ## This program is free software; you can redistribute it and/or modify it under
5 ## the terms of the GNU General Public License as published by the Free Software
6 ## Foundation; either version 3 of the License, or (at your option) any later
7 ## version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but WITHOUT
10 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public License along with
15 ## this program; if not, see <http://www.gnu.org/licenses/>.
16
17 ## Generate an Chebyshev type II filter with Rs dB of stop band attenuation.
18 ## 
19 ## [b, a] = cheby2(n, Rs, Wc)
20 ##    low pass filter with cutoff pi*Wc radians
21 ##
22 ## [b, a] = cheby2(n, Rs, Wc, 'high')
23 ##    high pass filter with cutoff pi*Wc radians
24 ##
25 ## [b, a] = cheby2(n, Rs, [Wl, Wh])
26 ##    band pass filter with edges pi*Wl and pi*Wh radians
27 ##
28 ## [b, a] = cheby2(n, Rs, [Wl, Wh], 'stop')
29 ##    band reject filter with edges pi*Wl and pi*Wh radians
30 ##
31 ## [z, p, g] = cheby2(...)
32 ##    return filter as zero-pole-gain rather than coefficients of the
33 ##    numerator and denominator polynomials.
34 ##
35 ## [...] = cheby2(...,'s')
36 ##     return a Laplace space filter, W can be larger than 1.
37 ## 
38 ## [a,b,c,d] = cheby2(...)
39 ##  return  state-space matrices 
40 ## 
41 ## References: 
42 ##
43 ## Parks & Burrus (1987). Digital Filter Design. New York:
44 ## John Wiley & Sons, Inc.
45
46 function [a,b,c,d] = cheby2(n, Rs, W, varargin)
47
48   if (nargin>5 || nargin<3) || (nargout>4 || nargout<2)
49     print_usage;
50   end
51
52   ## interpret the input parameters
53   if (!(length(n)==1 && n == round(n) && n > 0))
54     error ("cheby2: filter order n must be a positive integer");
55   end
56
57
58   stop = 0;
59   digital = 1;  
60   for i=1:length(varargin)
61     switch varargin{i}
62     case 's', digital = 0;
63     case 'z', digital = 1;
64     case { 'high', 'stop' }, stop = 1;
65     case { 'low',  'pass' }, stop = 0;
66     otherwise,  error ("cheby2: expected [high|stop] or [s|z]");
67     endswitch
68   endfor
69
70   [r, c]=size(W);
71   if (!(length(W)<=2 && (r==1 || c==1)))
72     error ("cheby2: frequency must be given as w0 or [w0, w1]");
73   elseif (!(length(W)==1 || length(W) == 2))
74     error ("cheby2: only one filter band allowed");
75   elseif (length(W)==2 && !(W(1) < W(2)))
76     error ("cheby2: first band edge must be smaller than second");
77   endif
78
79   if ( digital && !all(W >= 0 & W <= 1))
80     error ("cheby2: critical frequencies must be in (0 1)");
81   elseif ( !digital && !all(W >= 0 ))
82     error ("cheby2: critical frequencies must be in (0 inf)");
83   endif
84
85   if (Rs < 0)
86     error("cheby2: stopband attenuation must be positive decibels");
87   end
88
89   ## Prewarp to the band edges to s plane
90   if digital
91     T = 2;       # sampling frequency of 2 Hz
92     W = 2/T*tan(pi*W/T);
93   endif
94
95   ## Generate splane poles and zeros for the chebyshev type 2 filter
96   ## From: Stearns, SD; David, RA; (1988). Signal Processing Algorithms. 
97   ##       New Jersey: Prentice-Hall.
98   C = 1;                        # default cutoff frequency
99   lambda = 10^(Rs/20);
100   phi = log(lambda + sqrt(lambda^2-1))/n;
101   theta = pi*([1:n]-0.5)/n;
102   alpha = -sinh(phi)*sin(theta);
103   beta = cosh(phi)*cos(theta);
104   if (rem(n,2))
105     ## drop theta==pi/2 since it results in a zero at infinity
106     zero = 1i*C./cos(theta([1:(n-1)/2, (n+3)/2:n]));
107   else
108    zero = 1i*C./cos(theta);
109   endif
110   pole = C./(alpha.^2+beta.^2).*(alpha-1i*beta);
111
112   ## Compensate for amplitude at s=0
113   ## Because of the vagaries of floating point computations, the
114   ## prod(pole)/prod(zero) sometimes comes out as negative and
115   ## with a small imaginary component even though analytically
116   ## the gain will always be positive, hence the abs(real(...))
117   gain = abs(real(prod(pole)/prod(zero)));
118
119   ## splane frequency transform
120   [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
121
122   ## Use bilinear transform to convert poles to the z plane
123   if digital
124     [zero, pole, gain] = bilinear(zero, pole, gain, T);
125   endif
126
127   ## convert to the correct output form
128   if nargout==2, 
129     a = real(gain*poly(zero));
130     b = real(poly(pole));
131   elseif nargout==3,
132     a = zero;
133     b = pole;
134     c = gain;
135   else
136     ## output ss results 
137     [a, b, c, d] = zp2ss (zero, pole, gain);
138   endif
139
140 endfunction