home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 8.ddi / SIGNAL.DI$ / BUTTORD.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  3.1 KB  |  99 lines

  1. function [order,wn] = buttord(wp,ws,rp,rs,opt)
  2. %BUTTORD Butterworth filter order selection.
  3. %    [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the
  4. %    lowest order digital Butterworth filter that loses no more than
  5. %    Rp dB in the passband and has at least Rs dB of attenuation
  6. %    in the stopband.  The passband runs from 0 to Wp and the
  7. %    stopband extends from Ws to 1.0, the Nyquist frequency.
  8. %    BUTTORD also returns Wn, the Butterworth natural frequency (or, 
  9. %    the "3 dB frequency") to use with BUTTER to achieve the specifications. 
  10. %    [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation
  11. %    for an analog filter.
  12. %
  13. %    When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp
  14. %    in BUTTORD.  
  15. %
  16. %    See also BUTTER, CHEB1ORD, CHEB2ORD, and ELLIPORD.
  17.  
  18. %    Reference: Rabiner and Gold, p 241.
  19.  
  20. %    L. Shure 6-9-88
  21. %    updated 11-13-92, T. Krauss
  22. %    Copyright (c) 1988, 1992 by the MathWorks, Inc.
  23.  
  24. if nargin == 4
  25.     opt = 'z';
  26. elseif nargin == 5
  27.     if ~strcmp(opt,'z') & ~strcmp(opt,'s')
  28.         error('Invalid option for final argument.');
  29.     end
  30. end
  31. np1 = length(wp);
  32. ns1 = length(ws);
  33. if (np1 ~= ns1)
  34.     error('The frequency vectors must both be the same length.')
  35. end
  36. ftype = 2*(np1 - 1);
  37. if wp(1) < ws(1)
  38.     ftype = ftype + 1;    % low (1) or reject (3)
  39. else
  40.     ftype = ftype + 2;    % high (2) or pass (4)
  41. end
  42.  
  43. % first, prewarp frequencies from digital (unit circle) to analog (imag. axis):
  44. if strcmp(opt,'z')    % digital
  45.     WP=tan(pi*wp/2);
  46.     WS=tan(pi*ws/2);
  47. else  % don't have to if analog already
  48.     WP=wp;
  49.     WS=ws;
  50. end
  51. %note - on old systems that are NOT case sensitive, this will still work OK
  52.  
  53. % next, transform to low pass prototype with passband edge of 1 and stopband
  54. % edges determined by the following: (see Rabiner and Gold, p.258)
  55. if ftype == 1    % low
  56.     WA=WS/WP;
  57. elseif ftype == 2    % high
  58.     WA=WP/WS;
  59. elseif ftype == 3    % stop
  60.     WA=(WS*(WP(1)-WP(2)))./(WS.^2 - WP(1)*WP(2));
  61. elseif ftype == 4    % pass
  62.     WA=(WS.^2 - WP(1)*WP(2))./(WS*(WP(1)-WP(2)));
  63. end
  64.  
  65. % find the minimum order b'worth filter to meet the more demanding spec:
  66. WA=min(abs(WA));
  67. order = ceil( log10( (10 .^ (0.1*abs(rs)) - 1)./ ...
  68.     (10 .^ (0.1*abs(rp)) - 1) ) / (2*log10(WA)) );
  69.  
  70. % next find the butterworth natural frequency W0 (or, the "3dB frequency")
  71. % to give exactly rs dB at WA.  W0 will be between 1 and WA:
  72. W0 = WA / ( (10^(.1*abs(rs)) - 1)^(1/(2*order)) );
  73.  
  74. % now convert this frequency back from lowpass prototype 
  75. % to the original analog filter:
  76. if ftype == 1    % low
  77.     WN=W0*WP;
  78. elseif ftype == 2    % high
  79.     WN=WP/W0;
  80. elseif ftype == 3    % stop
  81.     WN(1) = ( (WP(2)-WP(1)) + sqrt((WP(2)-WP(1))^2 + ...
  82.         4*W0.^2*WP(1)*WP(2)))./(2*W0);
  83.     WN(2) = ( (WP(2)-WP(1)) - sqrt((WP(2)-WP(1))^2 + ...
  84.         4*W0.^2*WP(1)*WP(2)))./(2*W0);
  85.     WN=sort(abs(WN)); 
  86. elseif ftype == 4    % pass
  87.     W0=[-W0 W0];  % need both left and right 3dB frequencies
  88.     WN= -W0*(WP(2)-WP(1))/2 + sqrt( W0.^2/4*(WP(2)-WP(1))^2 + WP(1)*WP(2) );
  89.     WN=sort(abs(WN)); 
  90. end
  91.  
  92. % finally, transform frequencies from analog to digital if necessary:
  93. if strcmp(opt,'z')    % digital
  94.     wn=(2/pi)*atan(WN);  % bilinear transform
  95. else
  96.     wn=WN;
  97. end
  98.