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 I filter with Rp dB of pass band ripple.
19 ## [b, a] = cheby1(n, Rp, Wc)
20 ## low pass filter with cutoff pi*Wc radians
22 ## [b, a] = cheby1(n, Rp, Wc, 'high')
23 ## high pass filter with cutoff pi*Wc radians
25 ## [b, a] = cheby1(n, Rp, [Wl, Wh])
26 ## band pass filter with edges pi*Wl and pi*Wh radians
28 ## [b, a] = cheby1(n, Rp, [Wl, Wh], 'stop')
29 ## band reject filter with edges pi*Wl and pi*Wh radians
31 ## [z, p, g] = cheby1(...)
32 ## return filter as zero-pole-gain rather than coefficients of the
33 ## numerator and denominator polynomials.
35 ## [...] = cheby1(...,'s')
36 ## return a Laplace space filter, W can be larger than 1.
38 ## [a,b,c,d] = cheby1(...)
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] = cheby1(n, Rp, 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 ("cheby1: filter order n must be a positive integer");
59 for i=1:length(varargin)
61 case 's', digital = 0;
62 case 'z', digital = 1;
63 case { 'high', 'stop' }, stop = 1;
64 case { 'low', 'pass' }, stop = 0;
65 otherwise, error ("cheby1: expected [high|stop] or [s|z]");
70 if (!(length(W)<=2 && (r==1 || c==1)))
71 error ("cheby1: frequency must be given as w0 or [w0, w1]");
72 elseif (!(length(W)==1 || length(W) == 2))
73 error ("cheby1: only one filter band allowed");
74 elseif (length(W)==2 && !(W(1) < W(2)))
75 error ("cheby1: first band edge must be smaller than second");
78 if ( digital && !all(W >= 0 & W <= 1))
79 error ("cheby1: critical frequencies must be in (0 1)");
80 elseif ( !digital && !all(W >= 0 ))
81 error ("cheby1: critical frequencies must be in (0 inf)");
85 error("cheby1: passband ripple must be positive decibels");
88 ## Prewarp to the band edges to s plane
90 T = 2; # sampling frequency of 2 Hz
94 ## Generate splane poles and zeros for the chebyshev type 1 filter
95 C = 1; # default cutoff frequency
96 epsilon = sqrt(10^(Rp/10) - 1);
97 v0 = asinh(1/epsilon)/n;
98 pole = exp(1i*pi*[-(n-1):2:(n-1)]/(2*n));
99 pole = -sinh(v0)*real(pole) + 1i*cosh(v0)*imag(pole);
102 ## compensate for amplitude at s=0
104 ## if n is even, the ripple starts low, but if n is odd the ripple
105 ## starts high. We must adjust the s=0 amplitude to compensate.
107 gain = gain/10^(Rp/20);
110 ## splane frequency transform
111 [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
113 ## Use bilinear transform to convert poles to the z plane
115 [zero, pole, gain] = bilinear(zero, pole, gain, T);
118 ## convert to the correct output form
120 a = real(gain*poly(zero));
121 b = real(poly(pole));
128 [a, b, c, d] = zp2ss (zero, pole, gain);