/trunk/octave-forge/main/signal/inst/cheby1.m

# · Objective C · 131 lines · 116 code · 15 blank · 0 comment · 30 complexity · 91cfc7257b6853432c29ba164e494512 MD5 · raw file

  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. ## Generate an Chebyshev type I filter with Rp dB of pass band ripple.
  17. ##
  18. ## [b, a] = cheby1(n, Rp, Wc)
  19. ## low pass filter with cutoff pi*Wc radians
  20. ##
  21. ## [b, a] = cheby1(n, Rp, Wc, 'high')
  22. ## high pass filter with cutoff pi*Wc radians
  23. ##
  24. ## [b, a] = cheby1(n, Rp, [Wl, Wh])
  25. ## band pass filter with edges pi*Wl and pi*Wh radians
  26. ##
  27. ## [b, a] = cheby1(n, Rp, [Wl, Wh], 'stop')
  28. ## band reject filter with edges pi*Wl and pi*Wh radians
  29. ##
  30. ## [z, p, g] = cheby1(...)
  31. ## return filter as zero-pole-gain rather than coefficients of the
  32. ## numerator and denominator polynomials.
  33. ##
  34. ## [...] = cheby1(...,'s')
  35. ## return a Laplace space filter, W can be larger than 1.
  36. ##
  37. ## [a,b,c,d] = cheby1(...)
  38. ## return state-space matrices
  39. ##
  40. ## References:
  41. ##
  42. ## Parks & Burrus (1987). Digital Filter Design. New York:
  43. ## John Wiley & Sons, Inc.
  44. function [a,b,c,d] = cheby1(n, Rp, W, varargin)
  45. if (nargin>5 || nargin<3) || (nargout>4 || nargout<2)
  46. print_usage;
  47. endif
  48. ## interpret the input parameters
  49. if (!(length(n)==1 && n == round(n) && n > 0))
  50. error ("cheby1: filter order n must be a positive integer");
  51. endif
  52. stop = 0;
  53. digital = 1;
  54. for i=1:length(varargin)
  55. switch varargin{i}
  56. case 's', digital = 0;
  57. case 'z', digital = 1;
  58. case { 'high', 'stop' }, stop = 1;
  59. case { 'low', 'pass' }, stop = 0;
  60. otherwise, error ("cheby1: expected [high|stop] or [s|z]");
  61. endswitch
  62. endfor
  63. [r, c]=size(W);
  64. if (!(length(W)<=2 && (r==1 || c==1)))
  65. error ("cheby1: frequency must be given as w0 or [w0, w1]");
  66. elseif (!(length(W)==1 || length(W) == 2))
  67. error ("cheby1: only one filter band allowed");
  68. elseif (length(W)==2 && !(W(1) < W(2)))
  69. error ("cheby1: first band edge must be smaller than second");
  70. endif
  71. if ( digital && !all(W >= 0 & W <= 1))
  72. error ("cheby1: critical frequencies must be in (0 1)");
  73. elseif ( !digital && !all(W >= 0 ))
  74. error ("cheby1: critical frequencies must be in (0 inf)");
  75. endif
  76. if (Rp < 0)
  77. error("cheby1: passband ripple must be positive decibels");
  78. end
  79. ## Prewarp to the band edges to s plane
  80. if digital
  81. T = 2; # sampling frequency of 2 Hz
  82. W = 2/T*tan(pi*W/T);
  83. endif
  84. ## Generate splane poles and zeros for the chebyshev type 1 filter
  85. C = 1; # default cutoff frequency
  86. epsilon = sqrt(10^(Rp/10) - 1);
  87. v0 = asinh(1/epsilon)/n;
  88. pole = exp(1i*pi*[-(n-1):2:(n-1)]/(2*n));
  89. pole = -sinh(v0)*real(pole) + 1i*cosh(v0)*imag(pole);
  90. zero = [];
  91. ## compensate for amplitude at s=0
  92. gain = prod(-pole);
  93. ## if n is even, the ripple starts low, but if n is odd the ripple
  94. ## starts high. We must adjust the s=0 amplitude to compensate.
  95. if (rem(n,2)==0)
  96. gain = gain/10^(Rp/20);
  97. endif
  98. ## splane frequency transform
  99. [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
  100. ## Use bilinear transform to convert poles to the z plane
  101. if digital
  102. [zero, pole, gain] = bilinear(zero, pole, gain, T);
  103. endif
  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. endfunction