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

  1. %FILTDEMO Demonstration of filter design techniques.
  2. echo on
  3. clc
  4. %    This example demonstrates the use of the YULEWALK, BUTTER and
  5. %    CHEBY1 functions to design bandpass filters.  A sampling rate
  6. %    of 1000 Hz is assumed.
  7.  
  8. %    The YULEWALK function allows you to specify a piecewise shape
  9. %    for the desired frequency response magnitude.  YULEWALK then
  10. %    finds an infinite-impulse response filter of the desired order
  11. %    that fits the frequency response in a least-squares sense.
  12. %
  13. %    We start by specifying the desired frequency response, point-wise,
  14. %    with 1.0 corresponding to half the sample rate:
  15.  
  16. f = [0 .4 .4 .6 .6 1];
  17. H = [0  0   1   1   0  0];
  18.  
  19. pause % Strike any key to continue.
  20.  
  21. clc
  22. %    Next we plot the desired frequency response to make sure it is what
  23. %    we want.  To plot the frequency response, we can scale the
  24. %    normalized frequency axis:
  25.  
  26. fs = 1000;        % sampling rate
  27. fhz = f*fs/2;
  28.   
  29. %    Now we are ready to plot.
  30.  
  31. pause % Strike any key for plot.
  32.  
  33. plot(fhz,H), title('Desired Frequency Response'), ...
  34. xlabel('Frequency (Hz)'), ylabel('Magnitude'),  pause
  35. clc
  36. %     If the desired frequency response did not look right, 
  37. %     we would go back and change H. However, in this case
  38. %     everything looks OK so we perform the design:
  39.  
  40. N = 8;        % Order of the filter (number of poles and zeros).
  41.  
  42. [Bh,Ah] = yulewalk(N,f,H);        % Working, please wait.....
  43.  
  44. %    The design is complete and we can look at the filter coefficients.
  45.  
  46. pause % Strike any key to continue.
  47. format compact
  48. clc
  49. Ah,Bh    % Here are the filter coefficients:
  50. pause % Strike any key to continue.
  51. format
  52. clc
  53. %    Of course it is impossible to know by looking at the coefficients
  54. %    whether the design is good or bad. Let's compute the frequency
  55. %    response of the filter:
  56.  
  57. n = 256;
  58. hh = freqz(Bh,Ah,n);    % compute complex frequency response
  59. hy  = abs(hh);        % compute magnitude
  60.  
  61. %    Now we can plot the frequency response magnitude
  62. %    and compare it to the desired response:
  63.  
  64. pause % Strike any key for plot.
  65. ff  = fs/(2*n) * (0:n-1);
  66. plot(fhz,H,ff,hy), title('Actual vs. Desired Frequency Response'), ...
  67. xlabel('Frequency (Hz)'), ylabel('Magnitude'), pause
  68. clc
  69. %    Now let's design Butterworth and Chebyshev bandpass filters
  70. %    with the same passband (defined between 0.0 and 1.0).
  71.  
  72. N = 4;                % Filter order
  73. passband = [.4 .6];        % Passband specification
  74. [Bb,Ab] = butter(N, passband);
  75.  
  76. ripple = .1;            % Allowable ripple, in decibels
  77. [Bc,Ac] = cheby1(N, ripple, passband);
  78.  
  79. %     Now find the frequency responses:
  80.  
  81. hb = freqz(Bb,Ab,n);
  82. hc = freqz(Bc,Ac,n);
  83. h = [abs(hh) abs(hb) abs(hc)];
  84.  
  85. pause % Strike any key for plot.
  86.  
  87. plot(ff,h), title('YuleWalk, Butterworth and Chebyshev filters'), ...
  88. xlabel('Frequency (Hz)'), ylabel('Magnitude'),pause
  89. clc
  90. %    Often it is useful to look at the frequency response on
  91. %    logarithmic (dB) scales.
  92.  
  93. pause % Strike any key for plot.
  94.  
  95. plot(ff(2:n),20*log10(h(2:n,:))), ...
  96. title('YuleWalk, Butterworth and Chebyshev filters'), ...
  97. xlabel('Frequency (Hz)'), ylabel('Magnitude in dB'),  pause
  98. clc
  99.