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

# · Objective C · 117 lines · 103 code · 14 blank · 0 comment · 25 complexity · 30de67237cc15cfbff26337c6b12c5f2 MD5 · raw file

  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. ## Generate a bessel filter.
  18. ## Default is a Laplace space (s) filter.
  19. ##
  20. ## [b,a] = besself(n, Wc)
  21. ## low pass filter with cutoff pi*Wc radians
  22. ##
  23. ## [b,a] = besself(n, Wc, 'high')
  24. ## high pass filter with cutoff pi*Wc radians
  25. ##
  26. ## [b,a] = besself(n, [Wl, Wh])
  27. ## band pass filter with edges pi*Wl and pi*Wh radians
  28. ##
  29. ## [b,a] = besself(n, [Wl, Wh], 'stop')
  30. ## band reject filter with edges pi*Wl and pi*Wh radians
  31. ##
  32. ## [z,p,g] = besself(...)
  33. ## return filter as zero-pole-gain rather than coefficients of the
  34. ## numerator and denominator polynomials.
  35. ##
  36. ## [...] = besself(...,'z')
  37. ## return a discrete space (Z) filter, W must be less than 1.
  38. ##
  39. ## [a,b,c,d] = besself(...)
  40. ## return state-space matrices
  41. ##
  42. ## References:
  43. ##
  44. ## Proakis & Manolakis (1992). Digital Signal Processing. New York:
  45. ## Macmillan Publishing Company.
  46. function [a, b, c, d] = besself (n, W, varargin)
  47. if (nargin>4 || nargin<2) || (nargout>4 || nargout<2)
  48. print_usage;
  49. end
  50. ## interpret the input parameters
  51. if (!(length(n)==1 && n == round(n) && n > 0))
  52. error ("besself: filter order n must be a positive integer");
  53. end
  54. stop = 0;
  55. digital = 0;
  56. for i=1:length(varargin)
  57. switch varargin{i}
  58. case 's', digital = 0;
  59. case 'z', digital = 1;
  60. case { 'high', 'stop' }, stop = 1;
  61. case { 'low', 'pass' }, stop = 0;
  62. otherwise, error ("besself: expected [high|stop] or [s|z]");
  63. endswitch
  64. endfor
  65. [r, c]=size(W);
  66. if (!(length(W)<=2 && (r==1 || c==1)))
  67. error ("besself: frequency must be given as w0 or [w0, w1]");
  68. elseif (!(length(W)==1 || length(W) == 2))
  69. error ("besself: only one filter band allowed");
  70. elseif (length(W)==2 && !(W(1) < W(2)))
  71. error ("besself: first band edge must be smaller than second");
  72. endif
  73. if ( digital && !all(W >= 0 & W <= 1))
  74. error ("besself: critical frequencies must be in (0 1)");
  75. elseif ( !digital && !all(W >= 0 ))
  76. error ("besself: critical frequencies must be in (0 inf)");
  77. endif
  78. ## Prewarp to the band edges to s plane
  79. if digital
  80. T = 2; # sampling frequency of 2 Hz
  81. W = 2/T*tan(pi*W/T);
  82. endif
  83. ## Generate splane poles for the prototype bessel filter
  84. [zero, pole, gain] = besselap(n);
  85. ## splane frequency transform
  86. [zero, pole, gain] = sftrans(zero, pole, gain, W, stop);
  87. ## Use bilinear transform to convert poles to the z plane
  88. if digital
  89. [zero, pole, gain] = bilinear(zero, pole, gain, T);
  90. endif
  91. ## convert to the correct output form
  92. if nargout==2,
  93. a = real(gain*poly(zero));
  94. b = real(poly(pole));
  95. elseif nargout==3,
  96. a = zero;
  97. b = pole;
  98. c = gain;
  99. else
  100. ## output ss results
  101. [a, b, c, d] = zp2ss (zero, pole, gain);
  102. endif
  103. endfunction