AN2182 Application note Filters using the ST10 DSP library Introduction The ST10F2xx family provides a 16-bit multiply and accumulate unit (MAC) allowing control-oriented signal processing and filtering widely used in digital applications. An ST10 DSP software library, developed by STMicroelectronics, contains a set of basic arithmetic operations such as multiplication as well as two main filter functions, FIR (finite impulse response) and IIR (infinite impulse response), mainly used in digital signal processing. The first chapter of this application note describes a theoretical digital implementation of four different filters: ■ Low-pass filter ■ High-pass filter ■ Passband filter ■ Cut-band filter The method adopted for each filter is the approximation of the ideal filter model by a FIR filter. This theory aims to compute the FIR’s coefficients by truncating the real signal with a known window. The second chapter illustrates a practical implementation of a low-pass filter using the ST10 DSP library, its results and its limitations. July 2007 Rev 1 1/18 www.st.com 18 AN2182 Contents Contents 1 ST10 DSP library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Digital filtering principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.1 Fourier transform of a sampled signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.2 Linear filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.3 Finite impulse response filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3 Low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4 High-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5 Passband filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 6 Cutoff band filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 7 Implementation example using the ST10 DSP library . . . . . . . . . . . . . . . 12 7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 7.2 Sampling frequency and FIR coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 7.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 7.3.1 Frequency response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 7.3.2 Phase response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 8 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 9 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2/18 AN2182 1 ST10 DSP library ST10 DSP library The ST10 DSP free library is a set of arithmetic and signal processing functions based on the ST10 MAC unit. These functions are callable from C and fully compatible with the Tasking compiler. This library manipulates signed integers coded on 16 or 32 bits. These integers represent numbers belonging to the interval [–1, 1[. We name these formats: Q1.31 and Q1.15. Table 1. Examples of integer representations –1 -0.5 -0.25 0 0.25 0.5 1 - 1/215 1 - 1/231 Q1.15 0xFFFF 0xC000 0xA000 0x0000 0x2000 0x4000 0x7FFF 0x7FFF Q1.31 0xFFFF FFFF 0xC000 0000 0xA0000 0x00000 0x20000 0x40000 000 000 000 000 0x7FFF 0000 0x7FFFFFFF For a detailed description of the ST10 DSP library, please refer to the application note AN1442 “Signal processing with ST10-DSP”. 3/18 AN2182 Digital filtering principles 2 Digital filtering principles Assume a continuous signal x(t) (the complex form corresponds to the signal’s phase and magnitude at the instant t) with a pass band B. Assume that this signal will be filtered using a filter with a continuous impulse response h(t). When digital processing has to be used, it is necessary to sample the input signal with a frequency of Fs = 1/Ts (Ts being the sampling period). The output signal is then reconstituted from the samples obtained at the filter’s output. Figure 1. Example of input and output signals Filter h Input signal Output signal The Shannon theorem states that when sampling a signal at discrete intervals, the sampling frequency Fs should be greater than twice the highest frequency of the input signal. 2.1 Fourier transform of a sampled signal Signals are converted from the time domain to the frequency domain usually through the Fourier transform. With the Fourier transform, the signal is converted to a magnitude and phase at each frequency. The Fourier transform of the sampled signal x(k) has the following expression: ∞ X(f) = ∑x ( k )e i2kπf –∞ The time representation can be computed from the X(f) as follows: ∞ x(k ) = ∑X ( f )e – i 2kπf –∞ 2.2 Linear filtering Using the notations defined in the previous section, the output signal y(n) is the convolution of the input signal x(k) and the filter impulse response h(k) . ∞ y(n) = ∑h ( n – k )x ( k ) = (h(k) ⊗ x(k)) n –∞ where x(n) = x(nTs), h(n – k) = h((n – k)Ts) and y(n) = y(nTs). 4/18 AN2182 Digital filtering principles The output signal frequency response is given by the following expression: Y(f) = H(f) ⋅ X(f) where H(f), X(f) and Y(f) are the respective Fourier transforms of h(k), x(k) and y(k). 2.3 Finite impulse response filters The FIR (Finite Impulse Response) are non-recursive filters, meaning that the output signal y(i) is a linear combination of N input samples x(k) in the case of a N–1 order filter. Its equation is N–1 ⎛ ⎞ ⎜ y(n) = a k x ( n – k )⎟ ∑ ⎜ ⎟ ⎝k = 0 ⎠ where ak are the FIR’s coefficients. A FIR filter is characterized by its order and its coefficients and can be used to implement any kinds of filters (low-pass, high-pass, pass-band or cutoff band). 5/18 AN2182 Low-pass filter 3 Low-pass filter The aim of this section is to create a low-pass filter with a cutoff frequency Fc and a gain G = 1, by determining a FIR filter using the digital approach. The FIR coefficients correspond to h(n) where h is its continuous time response and h(n) = h(nTs). In the frequency domain, the ideal filter corresponding to these criteria has the following response: Ideal low-pass filter frequency response Magnitude Figure 2. 1 -Fc 0 Fc Frequency This filter’s impulse response in the time domain is ∞ (1) h(t ) = ∫ H ( f )e 2πift df = 2F c sin c ( 2tF c ) –∞ This response is sampled with a rate Fs= 1/Ts (sampling frequency), so the discrete response has the following expression ∞ ( 2) ∑h ( nTs )δ( t – nTs ) hs ( t ) = –∞ The impact of sampling h(t) with a rate Fs is a periodization of the analog signal spectrum around Fs and a gain of Fs. In fact, the frequency response corresponding to the sampled impulse response is ∞ H s ( f ) = F s ∑H ( f – nF s ) –∞ Therefore, to obtain a gain of 1, the hs response filter should be divided by Fs. Using equations (1) and (2), the low-pass filter time response becomes: ∞ (3) hs ( t ) = Fc sin c ( 2nF c T s )δ( t – ns ) ∑2 -----F –∞ s The impulse response is a sinus cardinal (sinc) function centered at the origin. 6/18 AN2182 Low-pass filter Figure 3. Ideal low-pass filter impulse response h(kTs) h(k/2Fc) = 0 h(0)= 2Fc k>=1 Two issues can be observed at this stage: First, the filter length is infinite. This means that in order to have the ideal low-pass filter, an infinite number of filter coefficients is required. In practice, this cannot be done because of the calculation complexity. To solve this feasibility problem, the filter’s impulse response should be truncated by a known window. In this application note, we will truncate this response using a rectangular window. Truncating the filter’s impulse response, however, changes the ideal rectangular response; more fluctuations are observed in the pass band. In order to reduce this effect, the FIR’s coefficients’ numbers can be increased. Effect of truncating the ideal impulse with a rectangular window Magnitude Figure 4. W(n) 1 Time -Fc 0 Fc Frequency domain Frequency Time domain In the following sections, we will note: ● W is the window’s time response ● N the number of coefficients included in the window W. This means that the window’s length is (N–1)Ts. The window should be large enough to include at least the first lobe of the sinus cardinal. Second, the truncated response is finite but not causal because there are non-null coefficients on the negative time axis. This means that the filter reacts before being excited by the input signal. Practically, this filter is not feasible. To solve this issue, we will shift the filter’s impulse response by (N–1)Ts/2. By doing so, all coefficients on the negative time axis are 0 and the filter becomes feasible. f The impact of shifting the filter’s time response is a phase shift of ( N – 1 ) ⋅ π ⋅ -----F s in the frequency domain. 7/18 AN2182 Low-pass filter By fixing a number of points N and a sampling period Ts, the FIR coefficients h(n) corresponding to a low-pass filter with a cutoff frequency Fc are (4) Fc –1 N–1 h ( n ) = 2 ------ sin c ⎛ 2F c ⎛ n – N -------------⎞⎠ T s⎞⎠ ⋅ W ⎛⎝ n – -------------⎞⎠ ⎝ ⎝ Fs 2 2 if n = 0 .. N – 1 and 0 if n > N – 1. 8/18 AN2182 4 High-pass filter High-pass filter The aim of this section is to create a high-pass filter with a cutoff frequency Fc and a gain G = 1, by determining a FIR h filter using the digital approach. The FIR coefficients correspond to h(n) where h is its continuous time response and h(n) = h(nTs). In the frequency domain, the ideal high-pass filter corresponding to these criteria has the following response: Figure 5. Ideal high-pass filter frequency response 1 1 -Fs -Fc 0 Fc Fs -Fs -Fc 0 Fc Fs High-pass filter Fc Low-pass filter Fc If we consider HH(f) the frequency response of the high-pass filter with a cutoff frequency Fc and HL(f) the frequency response of the low-pass filter with a cutoff frequency Fc, we can easily notice that (5) HH ( f ) = 1 – HL ( f ) In the time domain, this gives (6) Fc ⎛ ⎞ –1 N–1 N–1 h H ( n ) = ⎜ δ⎛ n – N -------------⎞⎠ – 2 ------ sin c ⎛⎝ 2F c ⎛⎝ n – -------------⎞⎠ T s⎞⎠ ⎟ ⋅ W ⎛⎝ n – -------------⎞⎠ ⎝ Fs 2 2 2 ⎝ ⎠ where N is the number of the filter’s coefficients, N–1 is the filter’s order and W is the window’s time function. In the case of a high-pass filter, N should be odd because of the dirac. 9/18 AN2182 Passband filter 5 Passband filter The aim of this section is to create a passband filter with a pass frequency F1, a cutoff frequency F2> F1, and a gain G=1, by determining a FIR h filter using the digital approach. In the frequency domain, the ideal passband filter corresponding to these criteria has the following response: Ideal passband filter frequency response Magnitude Figure 6. 1 -F2 0 -F1 F2 F1 Frequency This filter’s impulse response in the time domain is ∞ h(t) = ∫ – F1 H ( f )e –∞ 2πift df = ∫ F2 e 2πift df + – F2 ∫ e 2πift df = 2F 2 sin c ( 2F 2 t ) – 2F 1 sin c ( 2F1 t ) F1 Here, the same method described in the section entitled Low-pass filter is handled. Please refer to this section for more details. If we fix N the number of the filter’s coefficients and Ts the sampling period then we obtain (7) F1 ⎛ F2 ⎞ N–1 – 1⎞ T ⎞ – 2 -----–1 h ( n ) = ⎜ 2 ------- sin c ⎛ 2F 2 ⎛ n – N - sin c ⎛ 2F 1 ⎛ n – N ------------------------⎞⎠ T s⎞⎠ ⎟ ⋅ W ⎛⎝ n – -------------⎞⎠ s ⎝ ⎠ ⎝ ⎝ ⎠ ⎝ F F 2 2 2 ⎝ s ⎠ s with n = 0.. N – 1 and where 10/18 ● N is the number of the FIR coefficients ● N – 1 is the FIR’s order ● W is the window time response AN2182 Cutoff band filter The aim of this section is to create a cutoff band filter with a cutoff frequency F1, a pass frequency F2> F1, and a gain of G=1, by determining a FIR filter using the digital approach. The FIR coefficients correspond to h(n) where h is the continuous time response of the filter and h(n) = h(nTs). In the frequency domain, the ideal cutoff band filter corresponding to these criteria has the following response: Ideal cut-band filter frequency response Magnitude Figure 7. Magnitude 6 Cutoff band filter 1 -F1 -F2 0 1 F1 F2 Frequency -F2 -F1 0 F1 F2 Frequency Cut-band filter Passband filter If we consider ● HP(f) the frequency response of the passband filter with a pass frequency F1 and cutoff frequency F2 ● HC(f) the frequency response of the cutoff band filter with a cutoff frequency F1 and pass frequency F2 ● Ts is the sampling period then, we can easily notice that (8) HC ( f ) = 1 – HP ( f ) In the time domain, this gives F F1 ⎞ –1 ⎛ 2 N–1 N–1 h C ( n ) = δ⎛⎝ n – N -------------⎞⎠ – ⎜ 2 ------- sin c ⎛⎝ 2F 2 ⎛⎝ n – -------------⎞⎠ T s⎞⎠ – 2 ------- sin c ⎛⎝ 2F 1 ⎛⎝ n – -------------⎞⎠ T s⎞⎠ ⎟ Fs 2 2 2 ⎝ Fs ⎠ with n = 0.. N – 1 and where ● N is the number of FIR coefficients ● N – 1 is the FIR’s order ● W is the window time response In the case of a cutoff band filter, N should be odd because of the dirac, that is the FIR’s order should be even. 11/18 AN2182 Implementation example using the ST10 DSP library 7 Implementation example using the ST10 DSP library 7.1 Overview In this example, an input signal is fed to an analog input of the ST10F27x, processed by a digital low-pass filter and then output on a PWM output setup as a digital to analog converter. The digital filter is a low-pass filter with a cutoff frequency equal to 2 kHz and an order of 14 implemented on the ST10F27x using the DSP library provided by STMicroelectronics. P5.0 Signal ADC module sampled Lowpass filter Signal filtered PWM module DSP P7.0 27 kΩ ST10F276 Output signal Input signal P5.0 Low-pass filter modules 220pc Figure 8. The whole implementation includes the following elements: 12/18 ● ADC (analog-to-digital converter): the 10-bit ADC of the ST10F27x is used. The input signal is entered on channel 0 (P5.0) of the ST10F27x. The ADC conversion time is 4.85 µs (ADCST = ADCTC and ST10 frequency = 40 MHz). When the conversion is complete, the result stored in the ADDAT register is provided to the FIR filter module. ● Digital filter module: it provides a digital filtered sample. The function used in this application is the fir_q15_q15_q15() provided in the LibST10.h. This means that the inputs, outputs and the FIR coefficients are fractional between –1 and 1 and are coded in the format Q1.15. ● DAC (digital-to-analog converter): this module converts the digital filtered signal into an analog one. An 8-bit DAC is implemented using the ST10F27x PWM and an analog lowpass filter RC. The PWM frequency is 158.8 kHz. The RC filter is used to retain the continuous components and to remove the frequency of the PWM module. In our case, the RC cutoff frequency is 26.8 kHz. The digital filtered sample sets the pulse width of the PWM signal. AN2182 Implementation example using the ST10 DSP library The following flowchart summarizes the software implementation of the low-pass filter: Figure 9. Low-pass filter flowchart PWM initialization (PP0 = 0xFF) FIR structure initialization(1) ADC initialization Circular buffer initialization(2) A No Yes PWMIR = 1 Clear PWMIR Trigger ADC conversion No Yes ADCIR = 1 ADCIR = 0 Update the circular buffer with (ADDAT << 5)(3) Apply the filter->Output PW0 = PP0 +1 - (Output >> 7)(4) B 1. Please refer to the technical note ST10 DSP library for a detailed description of the FIR structure used and its fields. 2. If the filter’s order is 14, 15 input samples are needed to compute an output. A circular buffer is used to store the 15 input samples of the input signal. The size of the circular buffer is 2 x (filter’s order +1), which is 30 in this example. An input sample is stored twice in the circular buffer: at buffer[i] and buffer[i + filter’s order +1] where 0<= i<= filter’s order. 3. The ST10F27x ADC converts analog voltages between 0 and 5V into 10-bit digital values. The inputs for the fir_q15_q15_q15 should be fractional. To normalize these samples, they are shifted to the left. 4. The FIR function delivers a fractional output coded on 16 bits(Q1.15). The DAC realized with the ST10F27x is an 8-bit one. 13/18 AN2182 Implementation example using the ST10 DSP library 7.2 Sampling frequency and FIR coefficients Using the flowchart in the previous section, the sampling period is the time needed between point A and point B. To calculate the FIR’s coefficients, the sampling period should be known. We measure the time between points A and B and the result is 17.5 kHz. Note that to reconstitute the filtered signal, the input signal should not contain frequencies greater than 8.75 kHz. The FIR’s coefficients are then computed according to this sampling frequency ··· h( 0) – 1402 0.23 sin c ( – 1.61 ) – 0.0428 h( 1) – 1605 0.23 sin c ( – 1.37 ) – 0.049 h( 2) – 895 0.23 sin c ( – 1.14 ) – 0.0273 h( 3) 701 0.23 sin c ( – 0.914 ) 0.0214 h( 4) 2956 0.23 sin c ( – 0.68 ) 0.0902 h( 5) 5174 0.23 sin c ( – 0.46 ) 0.1579 h( 6) 6898 0.23 sin c ( – 0.23 ) 0.2105 = = 7537 h( 7) = 0.23 sin c ( 0 ) 0.23 h( 8) 6898 0.23 sin c ( 0.23 ) 0.2105 h( 9) 5174 0.23 sin c ( 0.46 ) 0.1579 h ( 10 ) 2956 0.23 sin c ( 0.68 ) 0.0902 h ( 11 ) 701 0.23 sin c ( 0.914 ) 0.0214 h ( 12 ) – 895 0.23 sin c ( 1.14 ) – 0.0273 h ( 13 ) – 1605 0.23 sin c ( 1.37 ) – 0.049 h ( 14 ) – 1402 0.23 sin c ( 1.61 ) – 0.0428 7.3 ⋅ 2 – 15 Results To analyze the filter’s frequency and phase responses, a sinusoidal signal of a 2V amplitude is used. The input voltage varies between 0 and 2 V. 7.3.1 Frequency response To draw the filter’s frequency response, the amplitude of the output signal is measured. The following figure gives the filter’s response. Figure 10. Filter’s frequency response Gain in dB 10 0 -10 1 10 100 -20 -30 Frequency 14/18 1000 10000 AN2182 Phase response The following figure gives the phase response. Figure 11. Phase response 120 Phase in ° 7.3.2 Implementation example using the ST10 DSP library 20 -80 1 10 100 1000 10000 -180 Frequency The code implemented to generate this digital low-pass filter can be used to implement a highpass, a passband or a cutoff band filter. One simply needs to change the filter’s coefficients and the number of coefficients. However, with this implementation, care should be taken with the sampling frequency, which changes with the filter’s coefficients number and the ST10F27x frequency. 15/18 AN2182 References 8 16/18 References 1. ST10F276 user manual 2. ST10F276 datasheet 3. AN1442 Signal processing with ST10-DSP, application note AN2182 9 Revision history Revision history Table 2. Document revision history Date Revision 19-Jul-2007 1 Changes Initial release. 17/18 AN2182 Please Read Carefully: Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any time, without notice. All ST products are sold pursuant to ST’s terms and conditions of sale. Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no liability whatsoever relating to the choice, selection or use of the ST products and services described herein. No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein. UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT. UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY, DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK. Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any liability of ST. ST and the ST logo are trademarks or registered trademarks of ST in various countries. Information in this document supersedes and replaces all information previously supplied. The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners. © 2007 STMicroelectronics - All rights reserved STMicroelectronics group of companies Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America www.st.com 18/18