You are on page 1of 9

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

Development Tools Selector

Search:
Email: office@mikroe.com

Home

Development Tools

Compilers

Accessory Boards

Special Offers

Easy Buy

Publications

Support

Projects

Download

Programming dsPIC MCU in PASCAL


TOC Ch. 1 Ch. 2 Ch. 3 Ch. 4 Ch. 5 Ch. 6 Ch. 7 Ch. 8 Ch. 9 Ch. 10 Ch. 11 Ch. 12 Ch. 13 Ch. 14 Ch. 15

Featured Development Tools dsPICPRO4 Development System

Chapter15: Digital filter design


15.1 General concepts 15.2 FIR filters 15.3 IIR filters
This chapter briefly describes Filter Designer Tool which is a constituent part of the mikroPascal, mikroC, and mikroBasic. This tool serves for designing digital filters.
The dsPICPRO4 supports the latest 64-

15.1 General concepts


Filter Designer Tool allows simple and very fast design of digital filters. Fig. 15-1 shows one option in the main menu of mikroPascal which enables access to Filter Designer Tool.

and 80-pin dsPIC30F microcontrollers from Microchip. It comes with a dsPIC30F6014A. The system includes an USB 2.0 programmer, mikroICD (In-Circuit Debugger), touch panel controller, CAN, RS485, DAC and RTC. [more info]

EasydsPIC4A Development System

EasydsPIC4A is a full-featured development system that supports the latest 18-, 28- and 40-pin dsPIC30F MCUs. It comes with the dsPIC30F4013 microcontroller. The board features a USB 2.0 programmer, mikroICD (In-Circuit Debugger), touch panel controller etc. [more info]

Featured Compilers
Fig. 15-1. Filter designer tool

mikroPascal PRO for dsPIC

There are two classes of digital filters. These are Finite Impulse Response (FIR) filters and Infinite Impulse Response (IIR) filters. Both classes have their own merits and shortcomings. When designing a digital filter, the first task is to select the class of filter. Table 15-1 presents the merits and shortcomings of FIR filters and, for comparison, Table 15-2 presents the merits and shortcomings of IIR filters.
MERITS Stability condition is always fulfilled! Linear phase (by a proper design applied in the Filter Designer Tool) Table 15-1. Merits and shortcomings of FIR filters MERITS For a given amplitude characteristic IIR filter is of considerably lower order compared to FIR filter SHORTCOMINGS Stability condition is not always fulfilled. As the order of IIR filter increases, the probability that the filter will become unstable increases Nonlinear phase Table 15-2. Merits and shortcomings of IIR filters SHORTCOMINGS For a given amplitude characteristic FIR filter is of considerably higher order compared to IIR filter (higher complexity) Exploit the full potential of 16-bit dsPIC and PIC24 microcontrollers with your favorite embedded mikroPascal compiler. [more info]

As one can see from the above Tables 15-1 and 15-2, a merit of one class of filters is a shortcoming of the other class. When starting a design, one should start from the type of signal to be filtered. E.g. when processing audio signals, the phase is irrelevant. Therefore, the phase linearity can be sacrificed. By selecting the class of IIR filters, one can obtain a filter of lower order or a filter of the same order but of much higher selectivity compared to the corresponding class FIR

1 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

filter. For some other signals it may be necessary to preserve phase linearity. An example of such signals is the ECG signal. In this case IIR filter must not be used. However, the price for linearity is paid by a much higher order of the filter. As the order of the filter increases, the corresponding signal processing will last longer and more memory will be required for the processing. For this reason it is necessary to select carefully the order of the filter and its input parameters. Fig. 15-2 shows the specifications for the amplitude characteristic of a digital filter. The class of filter (FIR or IIR) and design method will determine which input parameters will be available. The remaining parameters will be calculated by Filter Designer Tool to obtain the filter of the specified order.

Fig. 15-2. Digital filter specifications

A[dB] filter amplification in dB, Ap permitted bandpass ripple of amplification in dB, As minimum bandstop attenuation in dB, Wp, Wpass bandpass boundary frequencies, Ws, Wpass bandstop boundary frequencies, Ws one half of the sampling frequency.

The frequency range 0 to Wpass is called passband. In the passband the maximum attenuation or amplification of signals is Ap. The frequency range Wpass to Wstop is called transition zone. In the transition zone the characteristic of the filter is unknown. It is only known that it is monotonically decreasing or monotonically increasing. The frequency range Wstop to Ws/2 is called bandstop. In the bandstop the signal is attenuated at least by As dB.
After the class (FIR or IIR) and input parameters are selected, designing of the desired filter is performed. In Chapter 15.2 the design of the finite impulse response (FIR) filters is presented. In Chapter 15.3 the design of the infinite impulse response (IIR) filters is presented.

15.2 FIR filters


FIR is the abbreviated notation for the finite impulse response digital filters. The basic properties of FIR filters are:

relatively high order, linearity of phase, stability.


Design of a FIR filter starts by selecting Filter Designer Tool from the main menu of the mikroPascal compiler for dsPIC. The option FIR parameters on the left-hand side of the menu is selected. Then, the form shown in Fig. 15-3 appears.

2 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

Fig. 15-3 Entering FIR filter parameters

In the part Device setup appear the clock and designation of the used microcontroller. In the part Input Signal, two parameters, sampling frequency and input channel of the AD converter, should be selected. The sampling frequency has to be at least twice as high as the maximum frequency of the input signal. If, e.g. the input signal is in the range 100Hz 4400Hz, the minimum sampling frequency is 8800Hz. Of course, one never selects the minimum frequency, thus in this case the sampling frequency of at least 10kHz should be selected. In the part Filter settings the type of filter is selected first:

Lowpass filter, Highpass filter, Bandpass filter, Bandstop filter.


Then, the order of the filter is selected. This does not apply if Kaiser window function is applied, when on the basis of the selected parameters the order of filter is selected automatically. As the order of the filter increases, the selectivity of the filter will be higher (narrower transition zone), but the complexity of the filter will increase as a consequence of the more memory required for storing the samples and longer processing. The maximum sampling frequency depends on the length of processing (filter order) and selected clock.

Fig. 15-4 Type of FIR filter

After the type and order of the FIR filter are selected, the window function to be used can be selected. The bandstop attenuation and selectivity of the filter will depend on the window function. These two characteristics are mutually in conflict. A higher bandstop attenuation corresponds to a lower selectivity and vice versa. Kaiser window function is an optimum window function which gives the maximum attenuation for a given selectivity.

Fig. 15-5 Selecting window function

There remains only one parameter to be selected and that is the boundary frequency of the filter.

Fig. 15-6 Boundary frequency of the FIR filter

When all FIR filter parameters have been entered, design of the filter is carried out simply by selecting one of the options on the left-hand side

3 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

of Filter Designer Tool (FIR Window, FIR Frequency, mikroPascal, mikroBasic, mikroC). In the part FIR Window the shape of the window function and pulse response of the designed filter can be observed. If a lowpass filter of the order 20 is selected, Hamming window function having the boundary frequency 4000Hz will result in the pulse response shown in Fig.15-7.

Fig. 15-7 Window function and pulse response of the designed filter

In the part FIR Frequency one can see the shape of the frequency characteristic of the window function and the frequency characteristic of the designed digital filter. If a lowpass filter of the order 20 is selected, Hamming window function having the boundary frequency 4000Hz will result in the pulse response shown in Fig.15-8.

Fig. 15-8 Frequency characteristics of the window function (upper) and designed FIR filter (lower)

In the frequency characteristic of the FIR filter one can observe the deviation due to the finite wordlength (the coefficients are kept as 16-bit values). For the designed filter this deviation is negligible, but it can become significant for a higher order filter. The final product is the code in Pascal, Basic or C which could be accessed by selecting mikroPascal, mikroBasic or mikroC in the left-hand part of Filter Designer Tool. The corresponding parts of the code are shown in Figs. 15-9, 15-10. and 15-11.

4 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

Fig. 15-9 Program code for designing FIR filter (mikroPascal)

Fig. 15-10 Program code for designing FIR filter (mikroBasic)

5 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

Fig. 15-11 Program code for designing FIR filter (mikroC)

15.3 IIR filters


IIR is the abbreviated notation for the infinite impulse response digital filters. The basic properties of IIR filters are:

1. low order, 2. nonlinear phase, 3. potential instability.

Attention!!! When you are using IIR filters with order > 3, it is important to know that they are vary sensitive to wordlength effects and they can become unstable. IIR filters with order > 3, are normally broken down into smaller sections, typically second and/or first order blocks, which are then connected up in cascade or in parallel. For details refer to : "Signal Processing : Discrete Spectral Analysis, Detection and Estimation" M.Schwartz and L.Show (publisher MCGraw-Hill). "Digital Signal Processing" - E.C.Ifeachor and B.W.Jervis (publisher Addison-Wesley) Keep in mind that our filter designer tools implements IIR filters in direct canonical form, so you should not use IIR filters with order >3.

Design of an IIR filter starts by selecting Filter Designer Tool from the main menu of the mikroPascal compiler for dsPIC. The option IIR parameters on the left-hand side of the menu is selected. The form shown in Fig. 15-12 appears.

Fig. 15-12 Entering IIR filter parameters

6 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

In the part Device setup appear the clock and designation of the used microcontroller. In the part Input Signal, two parameters, sampling frequency and input channel of the AD converter should be selected. The sampling frequency has to be at least twice as high as the maximum frequency of the input signal. If, e.g. the input signal is in the range 230Hz 7500Hz, the minimum sampling frequency is 15kHz. Of course, one never selects the minimum frequency, thus in this case the sampling frequency of at least 20kHz should be selected. In the part Filter settings the type of filter is selected first:

Lowpass filter, Highpass filter, Bandpass filter, Bandstop filter.


Then, the order of the filter is selected. As the order of the filter increases, the selectivity of the filter will be higher (narrower transition zone), but the complexity of the filter will increase as a consequence of the more memory required for storing the samples and longer processing. The maximum sampling frequency depends on the length of processing (filter order) and selected clock. After the type of IIR filter is selected, the method of IIR filter design can be selected, as shown in Fig. 15-13.

Fig. 15-13 Selecting IIR filter design method

The basic IIR filter design methods are:

1. Butterworth Maximally flat amplitude characteristic at 0Hz frequency. The amplitude characteristc is a smooth curve. 2. Chebyshev I The minimum uniform ripple of the amplitude characteristc in the passband. 3. Chebyshev II Known as the Inverse Chebyshev filter. The amplitude characteristc is maximally flat at 0Hz (like Butterworth filter). The least variation of the amplitude characteristc in the bandstop range.
Depending on the selected design method and type of IIR filter, it is possible to enter the corresponding parameters, whereas the remaining filter parameters will be calculated automatically in the sourse of filter calculation. If the Bandpass filter and Chebyshev I method are selected, the parameters to be entered are: the maximum attenuation in the bandpass range Ap and bandpass boundary frequencies Wp1 and Wp2.

Fig. 15-14 Entering the bandpass attenuation and boundary frequencies

When all IIR filter parameters have been entered, design of the filter is carried out simply by selecting one of the options on the left-hand side of Filter Designer Tool (IIR Prototype, IIR Frequency, mikroPascal, mikroBasic, mikroC). In the part IIR Prototype one can see the transfer functions of the analogue filter prototype, analogue filter, and digital filter. The analogue prototype filter and analogue filter are the transfer functions obtained in the course of calculations of the characteristcs of the digital filter. For a set of selected parameters, one obtains transfer functions as shown in Fig. 15-15.

7 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

Fig. 15-15 Transfer functions of the analogue filter prototype, analogue filter, and obtained digital filter

In the part IIR Frequency one can see the amplitude characteristic of the obtained IIR filter. The present case is shown in Figs. 15-16 and 15-17. The final product is the code in Pascal, Basic or C which could be accessed by selecting mikroPascal, mikroBasic or mikroC in the left-hand part of Filter Designer Tool.

Fig. 15-16 Amplitude characteristic of the obtained IIR filter in LOG scale

8 of 9

29/01/2012 12:52 PM

Chapter 15: Digital filter design - Book: Programming dsPIC MCU in P...

http://www.mikroe.com/eng/chapters/view/33/chapter-15-digital-filter-de...

Fig. 15-17 Amplitude characteristic of the obtained IIR filter in LIN scale

previous chapter | table of contents

Related Links:

Products

News

Forums

Distributors

About MikroElektronika

Legal Information and Privacy Policy

Product Archive

Contact Us

Copyright 19982012. MikroElektronika. All rights reserved. All trade and/or services marks mentioned are the property of their respective owners.

9 of 9

29/01/2012 12:52 PM