Example: dental hygienist

Implementing IIR Digital Filters

1997 Microchip Technology 1 INTRODUCTION This application note describes the implementation ofvarious Digital Filters using the PIC17C42, the firstmember of Microchip s 2nd generation of 8-bitmicrocontrollers. The PIC17C42 is a very high speed8-bit microcontroller with an instruction cycle time of250 ns (@ 16 MHz input clock). Even though thePIC17C42 is an 8-bit device, it s high speed and effi-cient instruction set allows implementation of Digital fil-ters for practical applications. Traditionally Digital filtershave been implemented using expensive Digital SignalProcessors (DSPs). In a system the DSP is normally aslave processor being controlled by either an 8-bit or16-bit microcontroller. Where sampling rates are nothigh (especially in mechanical control systems), a sin-gle chip solution is possible using the PIC17C42.

AN540 DS00540C-page 2 1997 Microchip Technology Inc. Realization of the above equation is called the Direct Form II structure. For example, in case of a second

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Implementing IIR Digital Filters

1 1997 Microchip Technology 1 INTRODUCTION This application note describes the implementation ofvarious Digital Filters using the PIC17C42, the firstmember of Microchip s 2nd generation of 8-bitmicrocontrollers. The PIC17C42 is a very high speed8-bit microcontroller with an instruction cycle time of250 ns (@ 16 MHz input clock). Even though thePIC17C42 is an 8-bit device, it s high speed and effi-cient instruction set allows implementation of Digital fil-ters for practical applications. Traditionally Digital filtershave been implemented using expensive Digital SignalProcessors (DSPs). In a system the DSP is normally aslave processor being controlled by either an 8-bit or16-bit microcontroller. Where sampling rates are nothigh (especially in mechanical control systems), a sin-gle chip solution is possible using the PIC17C42.

2 This application note provides a few examples of imple-menting Digital Filters . Example code for 2nd order Infi-nite Impulse Response (IIR) Filters is given. Thefollowing type of Filters are implemented: Low Pass High Pass Band Pass Band Stop (notch) filterThis application note does not explain how to design afilter. Filter design theory is well established and is beyondthe scope of this application note. It is assumed that a filteris designed according to the desired specifications. Thedesired Digital Filters may be designed using either stan-dard techniques or using commonly available Digital filterdesign software packages. Finite Impulse Response (FIR) Filters have manyadvantages over IIR Filters , but are much more resourceintensive (both in terms of execution time and RAM). Onthe other hand, IIR Filters are quite attractive forimplementing with the PIC17C42 resources.

3 Especiallywhere phase information is not so important, IIR filtersare a good choice (FIR Filters have a linear phaseresponse). Of the various forms used for realizing digitalfilters (like, Direct form, Direct II form, Cascade form,Parallel, Lattice structure, etc.) the Direct II form is usedin this application note. It is easy to understand andsimple macros can be built using these :Amar PalacherlaMicrochip Technology Inc. THEORY OF OPERATION Digital Filters in most cases assume the following formof relationship between the output and inputsequences. The above equation basically states that the presentoutput is a weighted sum of the past inputs and pastoutputs. In case of FIR Filters , the weighted constants ai = 0 and in case of IIR Filters , at least one of the aiconstants is non zero.

4 In case of IIR, the above formulamay be rewritten in terms of Z transform as:The above equation can further be rewritten indifference equation format as follows:yn()aiyn i ()io=M bjxn j ()jo=N +=Hz()Yz()Xz()------------bkZk ko=M 1akZk k1=N +--------------------------------------- -==yn()aiyn i ()i1=M bjxn j ()jo=N += AN540 Implementing IIR Digital Filters AN540 DS00540C-page 2 1997 Microchip Technology Inc. Realization of the above equation is called the DirectForm II structure. For example, in case of a secondorder structure, M = N = 2, gives the following differ-ence equations: EQUATION 1:EQUATION 2: The above difference equations may be represented asshown in Figure 1. FIGURE 1:2ND ORDER DIRECT FORM IISTRUCTURE (TRANSPOSED) The structure as shown in Figure 1 may be cascaded toattain a higher order filter.

5 For example, if two stagesare cascaded together, a 4th Order IIR Filter isobtained. This way, the output of the 1st stage becomesthe input to the second stage. Multiple order Filters arethus implemented by cascading a 2nd order filter struc-ture as shown in Figure 1. IMPLEMENTATION A 4th order IIR Filter is implemented by cascading twoof the structures shown in Figure 1. The output Y (out-put of each filter stage) is computed by direct imple-mentation of Equation 1 and Equation 2. Since eachstage is similar algorithmically, it is implemented as amacro using Microchip s, Assembler/Linker forPIC17C42. This Macro (labelled BIQUAD ) is calledtwice for Implementing a 4th order filter. The output ofthe 1st stage is directly fed to the input of the secondstage without any may be required depending on the particularapplication.

6 The user can modify the code very easilywithout any penalty on speed. Also, saturationarithmetic is not used. Overflows can be avoided by lim-iting the input sequence amplitude. All numbers areassumed to be 16 bits in Q15 format (15 decimal points,MSb is sign bit). Thus the user must scale and signextend the input sequence accordingly. For example, ifthe input is from a 12-bit A/D converter, the user mustsign extend the 12-bit input if bit 11 is a BIQUAD macro is a generic macro and can beused for all IIR Filters whether it is Low Pass, High Pass,Band Pass or Band Stop. A general purpose 16x16dn()xn()=a1dn1 ()a2dn2 ()++yn()b0dn()=b1dn1 ()b2dn2 ()()++X(n)Y(n)Z-1Z-1b0b1-a1b2-a2+ multiplier routine is also provided. This routine isimplemented as a straight line code forspeed 4th order IIR filter implemented is a Low Pass Filterwith the specifications shown in Table 1.

7 TABLE 1:FILTER CONSTANTS The Low Pass Filter is designed using a Digital filterdesign package (DFDP by Atlanta Signal ProcessorsInc.). The filter package produces filter constants of thestructure shown in Table 1. Table 2 shows the filterco-efficients that are obtained for the above Low Passfilter specification. TABLE 2:FILTER CO-EFFICIENTS The above filter co-efficients (5 per stage) are quan-tized to Q15 format ( they are multiplied by 32768)and saved in program memory (starting at label _coeff_lpass ). The constants for both the stagesare read into data memory using TLRD and TABLRD instructions in the Initialization Routine (labelled initFilter ). The user may read the coefficients ofonly one stage at a time and save some RAM at theexpense of sample 4th order Low Pass IIR Filter is tested byanalyzing the impulse response of the filter.

8 An impulsesignal is fed as input to the filter. This is simulated byforcing the input to the filter by a large quantity (say7F00h) on the first input sample, and the all zeros fromthe 2nd sample onwards. The output sequence is thefilter s impulse response and is captured into thePICMASTER s (Microchip s Universal In-CircuitEmulator) real-time trace buffer. This captureddata from PICMASTER is saved to file and was done using MathCad for Windows anda DSP Analysis program from Burr-Brown(DSPLAY ). The Fourier Transform of this impulseresponse of the filter should display the filter s fre-quency response, in this case being a Low Pass plots of the impulse response and the frequencyresponse are shown in Figure 2, Figure 3 and Figure 4. BAND1 BAND2 Lower Band HzUpper Band Edge500 Hz1 kHzNominal in Frequency = 2 kHz StageCo-efficientsa1a2b0b1b2 1 2 is a trademark of MathSoft, is a trademark of Burr-BrownDFDP is a trademark of Atlanta Signal Processing is a registered trademark of Microsoft Corporation.

9 1997 Microchip Technology 3 AN540 FIGURE 2:IMPULSE RESPONSE CAPTURED FROM PICMASTERFIGURE 3:SPECTRUM COMPUTED FROM IMPULSE RESPONSE8000 --6000 --4000 --2000 --0 ---2000 ---4000 --Impulse ResponseTime * (mSec)Magnitude|||||||816243240485606412 8192256320384448512 FREQUENCY/512 (kHz)FREQUENCY RESPONSEM agnitude600 --500 --400 --300 --200 --100 --0 -- PERFORMANCE The resource requirements for filter implementationsusing a PIC17C42 is given in Table 3. These numberscan be used to determine whether a higher order filtercan be executed in real-time. The same informationmay be used to determine the highest sampling ratepossible. FILTER APPLICATIONS Digital Filters find applications in many areas especiallythose involving processing of real world signals. Insome applications like ABS systems in an automobile, Digital filtering becomes a must.

10 In this case eliminationof noise (especially glitches and false readings ofsensors) is very critical and thus becomes a require-ment of Digital signal processing. TABLE 3:RESOURCE REQUIREMENTS Note1:The above numbers do not include the initialization routine. FIGURE 4:LOG MAGNITUDE SPECTRUM OF IMPULSE RESPONSE Digital Filters are also needed in Process Control wherenotch Filters and low pass Filters are desired becausethe signals from sensors are transmitted over longlines, especially in a very noisy environment. In thesecases, typically a notch filter (centering 50 Hz or 60 Hz)is used. In cases of eliminating background noise, aband stop filter ( , 40 Hz to 120 Hz) is used. Thesample code given in this application note can be usedto design a feedback control system's digitalcompensator. For example, a typical DC Motor's digitalcompensator (like a dead-beat compensator) is ofsecond order and has the same filter structure that isimplemented in this application note.


Related search queries