1 ## Copyright (C) 1999 Paul Kienzle <pkienzle@users.sf.net>
2 ## Copyright (C) 2003 Doug Stewart <dastew@sympatico.ca>
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
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
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/>.
17 ## Generate an Chebyshev type II filter with Rs dB of stop band attenuation.
19 ## [b, a] = cheby2(n, Rs, Wc)
20 ## low pass filter with cutoff pi*Wc radians
22 ## [b, a] = cheby2(n, Rs, Wc, 'high')
23 ## high pass filter with cutoff pi*Wc radians
25 ## [b, a] = cheby2(n, Rs, [Wl, Wh])
26 ## band pass filter with edges pi*Wl and pi*Wh radians
28 ## [b, a] = cheby2(n, Rs, [Wl, Wh], 'stop')
29 ## band reject filter with edges pi*Wl and pi*Wh radians
31 ## [z, p, g] = cheby2(...)
32 ## return filter as zero-pole-gain rather than coefficients of the
33 ## numerator and denominator polynomials.
35 ## [...] = cheby2(...,'s')
36 ## return a Laplace space filter, W can be larger than 1.
38 ## [a,b,c,d] = cheby2(...)
39 ## return state-space matrices
43 ## Parks & Burrus (1987). Digital Filter Design. New York:
44 ## John Wiley & Sons, Inc.
46 function [a,b,c,d] = cheby2(n, Rs, W, varargin)
48 if (nargin>5 || nargin<3) || (nargout>4 || nargout<2)
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");
60 for i=1:length(varargin)
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]");
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");
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)");
86 error("cheby2: stopband attenuation must be positive decibels");
89 ## Prewarp to the band edges to s plane
91 T = 2; # sampling frequency of 2 Hz
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
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);
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]));
108 zero = 1i*C./cos(theta);
110 pole = C./(alpha.^2+beta.^2).*(alpha-1i*beta);
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)));
119 ## splane frequency transform
120 [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
122 ## Use bilinear transform to convert poles to the z plane
124 [zero, pole, gain] = bilinear(zero, pole, gain, T);
127 ## convert to the correct output form
129 a = real(gain*poly(zero));
130 b = real(poly(pole));
137 [a, b, c, d] = zp2ss (zero, pole, gain);