afilt.m version 09Sep23
(Classical analog filters)


Author: Paul Mennen
Email:  paul@mennen.org


Overview

Displays the frequency response for the five "classical" analog filters:

Butterworth Designed to be as flat as possible in the passband without passband ripples. An Nth-order Butterworth filter will roll off at 6N dB per octave (or 20N dB per decade). This rolloff isn't as fast as with the Chebyshev or Elliptic filters, but the phase for those filters will be more nonlinear. First described in 1930 by Stephen Butterworth
Bessel This filter is designed to have the best phase linearity possible in the passband. The phase delay and group delay will be nearly flat in the passband so this will be the best choice if it is important to preserve the wave shape of the passband signals. The step response has some overshoot, but much less than with the other filter types. The price to pay for this linearity is that it has a noticeably slower rolloff and thus less stopband attenuation. Friedrich Bessel developed the mathematical theory which was then applied to the filter design problem by W.E. Thomson in 1949.
Chebyshev type 1 This filter gets its name because they are derived from Chebyshev polynomials. It achieves a steeper rolloff than the Butterworth filter by allowing ripples in the passband.
Chebyshev type 2 This filter also achieves a steeper rolloff, but it does this by allowing ripples in the stop band. (i.e. no ripples in the passband). Also sometimes known as an inverse Chebyshev filter.
Elliptic This filter type achieves even a steeper rolloff than the Chebyshev filters by allowing ripples in both the passband and the stopband. Also sometimes known as a Cauer filter (after the German mathematician Wilhelm Cauer).

Although these 5 filters are analog filters by design, some people design digital filters by choosing one of these filter types and then transforming them from the continuous-time representation in the s-plane to the discrete-time representation in the z-plane, usually using the bilinear transform. This is done because the designers are familiar with these analog filters, but it is generally misguided since there are better design techniques for digital filters. (See the erip.m application for example.)

This application comes in 3 flavors:
  1. afiltS.m   - A simplified version appropriate for study by a programmer learning GUI design.
  2. afilt.m    - The full version.
  3. afiltALT.m - An alternate version to explore another method of displaying the phase information
All 3 are included in the sig folder, but only the second one is run by demoplt.

1. afiltS.m


By keeping the goals modest with few frills we can create an application with just a few dozen lines of code. The goals and the design method for this application are described here in the GUI building with plt section.

afiltS first creates a prototype filter for each of the five filter types. This prototype filter is always low pass and always uses a cutoff frequency of 1 radian/sec regardless of the settings of the frequency sliders. Then the prototype filter is transformed to high pass, band pass, or stop band if one of those types was selected as well as translated to the frequencies selected by the frequency sliders.

Note that the lower limit on the frequency sliders is .01 radians/sec and the upper limit is 10. However, you can enter any value you want for these sliders by typing the number into the slider edit box. That number will be used even if it is less than .01 or greater than 10. The X axis limits will be set appropriately so that the cutoff frequency is somewhat close to the middle of the plot.

When you LEFT click on the No. of Points popup (the 200) in this default setup, the popup will expand showing you the choices available as shown here. If you want to change the number of points without opening the popup, instead RIGHT click on the popup and it will change to 400. Another right click and it will change to 800, and then to 1600, and then cycles back to the first options (100). Clicking on the black "X" closes the popup without making a new selection.

When you RIGHT click on band pass (just to the right of the # of points popup) the popup will expand showing you the choices available as shown here. If you don't want to open the popup, LEFT click instead and it will cycle thru the four options as you click. Notice that the use of the right vs. left mouse clicks are the reverse of what is used for the number of points popup described above. Notice in the code where this popup is created there is the argument 'swap' and the end of the call, and that argument enables the reversed use of the left and right clicks. Whether you use the regular or the swapped usage is largely a matter of taste, but the swapped style is often used when there are only a few choices and you want to be able to easily see the results of every choice. Usually, popups are created with a label (as with the number of points popup above) since the number alone would have no meaning. But for this popup, a label isn't necessary because the choices make the meaning self-evident. To find out more details about the popup pseudo objects, refer to the Pseudo objects section. There you can also read about the edit pseudo object, which is used here for the filter order (just to the left of the number of Decades popup.

2. afilt.m


To create this application, I started with afiltS.m above and made the following enhancements:
  1. Elliptic transition ratio (shown with multi-line text string)
  2. Selecting colors
  3. Phase display
  4. Pole/zero plot
  5. Display of phase delay and group delay
  6. Saving/restoring the GUI state using a configuration file
  7. Adding temporary user help message
The motivation and some of the coding techniques behind these seven enhancements are discussed in the here in the GUI building with plt section.

The text on the plots (in orange and yellow) provides some basic user help. Those help messages disappear after any parameter is modified. This prevents the help text from being a distraction when using the application.

With the default settings, the prototype checkbox will be checked which means the poles and zeros displayed in the pole/zero plot will be for a low pass filter with a 1 radian per second cutoff frequency. (These prototype filters are calculated as a first step in deriving the filters with the specified frequencies of all four configurations (band pass, high pass, passband, and stopband). If you uncheck the prototype checkbox, then the poles and zeros of the filter in its requested configuration is displayed which also enables the linking of cursors with the main plot. (i.e. clicking on a trace in the main plot will move the cursor to the corresponding position of the imaginary axis of the s plane.)



With the default parameters (as shown above), select "low pass" from the popup, and then left click the phase delay button. You will see a figure similar to this one, which shows the phase delay for each of the five filter types.

Notice that the Bessel function has almost perfect flatness up to the cutoff frequency (0.1 radians/sec). This makes it good at preserving the shape of the passband signals. The Butterworth filter is also reasonably linear in the passband whereas the other filters are not very linear. If this non-linearity is not important, then you can take advantage of the much sharper rolloff of the magnitude response beyond the cutoff frequency.

If instead of left clicking on the phase delay button you right click on it, then a similar figure to this will appear except that it will show group delay instead of phase delay. Note that the Bessel filter also has a nearly constant group delay in the passband.

When you close the afilt figure window, a file called afilt.mat will be written to the plt\ini folder which contains the numerator and denominator polynomials associated with the current display, the current values of all the afilt controls as well as the current figure size and position. The next time you restart afilt, it will use the settings in afilt.mat so that the figure looks the same as when you last closed it. (Delete the afilt.mat file if you want to return to the default settings.)

If you want to save the afilt.mat file without exiting the application, click on the Save tag at the bottom of the menu box. If you right click on the Save tag, in addition to saving the afilt.mat file it will also create a file called afilt.txt (also in the plt\ini folder) that contains the numerator and denominator polynomials in text form (in order of decreasing powers of s). For example, if you start afilt with the default settings, switch to the low pass filter type, and then right click on the Save tag, the text file created will look like this:

----- Butter -----
num:                     0                     0                     0                     0                     0  1.000000000000000e-5
den:  1.000000000000000000  0.323606797749978914  .0523606797749978856  5.236067977499788e-3  3.236067977499789e-4  9.999999999999999e-6

----- Bessel -----
num:                     0                     0                     0                     0                     0  9.999999999999999e-6
den:  1.000000000000000000  0.381070120534927559  .0677667371567686472  6.886367652423625e-3  3.936283427035349e-4  9.999999999999996e-6

----- Cheby1 -----
num:                     0                     0                     0                     0                     0  8.172251697369242e-7
den:  1.000000000000000000  .0706460568061092337  .0149954326711260084  6.934769584958417e-4  4.593491210648848e-5  8.172251697369246e-7

----- Cheby2 -----
num:                     0  5.000250018751560e-3  6.92180711223233e-21  2.000100007500624e-4  1.55762265652227e-21  1.600080006000499e-6
den:  1.000000000000000000  0.214920041464015166  .0230828108613219812  1.550053632488997e-3  6.572941232619946e-5  1.600080006000500e-6

----- Elliptic -----
num:                     0  4.314287472988311e-3  -1.3272994986220e-21  1.794044791760442e-4  -3.6138205971008e-23  1.686430764421909e-6
den:  1.000000000000000000  .0697394438160997276  .0169373877260952863  8.336743397671178e-4  6.689503003586831e-5  1.686430764421909e-6

You can use the standard Matlab tools to verify that the frequency response curves shown by afilt are correct as follows:
>> load E:\mcode\plt\ini\afilt.mat % Load the afilt.mat file. The ini folder isn't usually
                                   % on the path, so you need to specify the full path
>> load(inifile('afilt'))          % Or, use this alternate way of specifying the plt\ini folder
>> figure;                         % This is needed if the afilt figure is still open. Otherwise freqs will
                                   % display its results inside the afilt figure which doesn't work well.
>> freqs(num{1},den{1});           % Display the Butterworth frequency response.
>> freqs(num{2},den{2});           % Display the Bessel frequency response.
>> freqs(num{3},den{3});           % Display the Chebychev type 1 frequency response.
>> freqs(num{4},den{4});           % Display the Chebychev type 2 frequency response.
>> freqs(num{5},den{5});           % Display the Elliptic frequency response.
>> [n,d] = bilinear(num{5},den{5},.5);  % In this example, we create a digital elliptic filter from the analog filter
>> freqz(n,d);                          % and display its frequency response using freqz or fvtool. However digital
>> fvtool(n,d);                         % filters are more easily designed using the erip.m application.

3. afiltALT.m


This is an alternate version of afilt.m. It was also created by enhancing afiltS.m, except that only enhancements number 1 and 3 listed above are implemented. This alternate version is included for two reasons:

  ●  A different method of displaying the phase information is used. Only 5 traces are used instead of 10. This means that each trace has to display both magnitude and phase information. You may find that this method of displaying two different functions on a single trace useful in applications of your own design.

  ●  Instead of computing the frequency response inside the application, this version calls on the butter( ), besself( ); cheby1( ); cheby2( ); and ellip( ) functions in the signal processing toolbox. Comparing the results of afilt.m and afiltALT.m allows you to verify that the frequency response calculations done inside afilt and afiltS are equivalent to these five functions. Of course, that means that you can't run this if you don't have the signal processing toolbox installed.




Copyright © 2023
Paul Mennen