Download presentation
Presentation is loading. Please wait.
Published byDerick Kelly Modified over 9 years ago
2
DSP Implementation Using PIC18C MCUs Hands-On ‘01 © 2001 Microchip Technology Incorporated. All rights reserved. Slide 1
3
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 2 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Excel DSP Activity-Playcenter tHands-On DSC Training Agenda: –Aliasing and Nyquist rate F/Fsamp<0.5 –Correlation and FIR waveshaping –Fourier transforms to filter coefficients Windowed FIR generation –Scilab FIR Generation to PIC ® MCU code in 18CFIR.ASM –Scilab IIR Generation to PIC ® MCU code in 18CIIR.ASM tCopy all files to C:\DSC from CD –disable read-only attributes on files if set tOpen DSC.XLS spreadsheet tOpen MPLAB ® IDE
4
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 3 DSP Implementation Using PIC18C MCUs Hands-On ‘01 General DSP Model tAccepts an analog signal tConverts this analog signal to digital numbers tPerforms computations tConverts results back into analog signal Processor 10110101 00100100 Signal Conditioning Low-pass filter A to D Converter Smoothing Filter D to A Converter Output Amp
5
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 4 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Excel DSP Activity-Playcenter Sampling-Sine worksheet ,xf1,xf2,xf3 –Everything normalized to F/F c –Nyquist sample rate 2x highest frequency Center at 0,4,6,0 –Aliasing demo 1,9 2,8 and 1,11 –Correlation I and Q detection and energy content 0-45,0,2,2 Easy math for single tone ID –DTMF –Graphic Equalizer
6
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 5 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Excel DSP Activity-Playcenter tNeed to anti-alias filter above 1/2 Fsamp
7
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 6 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Fs = 2000 Hz dB 650 Hz 1950 Hz 3250 Hz 4550 Hz Which Signals Will Be Aliased Below Fs/2? 2000 fs 1000 fs/2 4000 2fs 3000 3fs/2 5000 5fs/ 2 f (Hz) F aliased = | F signal - (N F sample ) |
8
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 7 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Fs = 2000 Hz dB 650 Hz (50 Hz) 1950 Hz (750 Hz) 3250 Hz ( 550 Hz ) 4550 Hz 2000 fs 1000 fs/2 4000 2fs 3000 3fs/2 5000 5fs/2 N=0 N=1 N=2 f (Hz) F aliased = | F signal - (N F sample ) |
9
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 8 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Fs = 2000 Hz f (Hz) dB (650 Hz) (1950 Hz) 50 Hz (3250 Hz) 750 Hz (4550 Hz) 550 Hz 500 1000 fs/2 N=0 750 250 F aliased = | F signal - (N F sample ) |
10
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 9 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Windowing – Rectangular Continuous Sine WaveSamples Time DTFT of a Finite-Length Sinusoidal Sequence FFT Results Even though the samples are of a pure sine wave, the DFT produces spectral values ‘riding’ on a curve This is useful when the absolute magnitude of a spectral bucket must be known, irrespective of the leakage
11
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 10 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Windowing – Hanning 1 Hanning Window Sample Interval By removing the starting and ending abrupt transitions, the side-lobe leakage is greatly reduced.
12
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 11 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Windowing – Response Rectangular Hanning Hamming Frequency
13
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 12 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Excel DSP Activity-Playcenter tFourier Transform-DFT worksheet –Square wave to Sinc function pair –0 n/NT frequencies F*T=2* 13dB 1st sidelobe DFT Periodic in 1/NT Redundant after 1/2NT (Nyquist again) –Flip F and T ; Show labels on row 1 Ideal filters -> Sinc waveshaping in time domain Identical |DFT| but different phasing X(n) = n= 0 N-1 x(n)e -j2 kn k = 0,1,…N-1 N typically base 2 e j = cos( ) + jsin( ) e -j = cos( ) - jsin( ) cos( ) = Re{e j } = 1/2 (e j + e -j ) sin( ) = Im{e j } = 1/2j (e j - e -j )
14
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 13 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Excel DSP Activity-Playcenter tWindowing FIR-WFIR worksheet –Window to taper sidelobes Open Width to reduce N Basically just waveshaping Same for FilterLAB™ FFT –Filters are not causal so delay (N-1)/2 equals group delay, Tg :-) –Better than Bessel filter –No feedback so better behaved But requires more taps than IIR Window not as flexible as Optfir HP is (1-LP) on filter taps BS is (1-BP) on filter taps tx H(f) in freq domain becomes x h(t) in the time domain
15
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 14 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Overview - FIR Filter Tapped Delay Line Form z -1 x[n] h0h0 h1h1 h2h2 h N-1 y[n] x[n-0]x[n-1]x[n-2]x[n-(N-1)]
16
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 15 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Common FIR examples: Moving Average z -1 X(n) Y(n) 1/8 Input Output + + + + z -1
17
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 16 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Overview of Digital Filters tSince this is somewhat complicated, it’s easier to convert from the frequency to the sampled time domain, then convolve: y[n] = h[k] x[n] The Inverse Discrete Fourier Transform of H( ) is the impulse response of the filter transfer function. –From this, the FIR and IIR filters derive their names. tThe simplest filter utilizes many principles covered so far - and ties together many concepts into one practical application
18
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 17 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Overview of Digital Filters tClassified by the duration of the impulse response –Finite - Finite Impulse Response or FIR filter Rely strictly on feed-forward - delay of previously sampled signals Sometimes called Feed-Forward Filters or Transversal Filter Delay between the input and the output of the filter, while the tap pipeline gets filled –Infinite - Infinite Impulse Response or IIR filter Rely on feedback as well as feed-forward Immediate output, but still has delay while feed-forward portion fine tunes response
19
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 18 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Basic Type 1 Transversal FIR z -1 X(n) Y(n) Input Output + G1G1 - + + z -1 G2G2 G3G3 G2G2 G1G1 - G0G0 + + G0G0 Scale 0 255 -127 +127 24-bit Sum
20
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 19 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Crossover Filter z -1 X(n) Y(n) Input LP Output + G1G1 - + + z -1 G2G2 G3G3 G2G2 G1G1 - G0G0 + + G0G0 - + Y(n) HP Output Basic Type 1 Transversal FIR X(n) = x(n)z -n
21
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 20 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Common FIR examples: PID loop z -1 X(n) Y(n) G I /4 Input Output + + + + G D /3 GPGP - + + + +
22
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 21 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Sampling and Quantization 4-bit (16 level) ADC sampling a sinewave input Input Sinewave ADC Output Quantization Error During 1 period (t) t OUTPUT DIGITAL WORD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
23
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 22 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Over Sampling SNR gain tSNR max = 1.76 + 6.02B + 10 log (R)
24
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 23 DSP Implementation Using PIC18C MCUs Hands-On ‘01 FIR PIC MCU realization X(n) = x(n)z -n where z -n is nT time delay tType I, –Even symmetry –Odd # taps tData Scaling 6dB/bit –8bits inputs with signed 8-bit taps, 16-bit products, 24-bit sum Max -48dB bandstop, quantization and truncation errors Saturation math checking like clipping in Op Amp PIC MCU Macd macro <= 1uSec/tap at 40MHz tFilter analysis-H(z) worksheet –Max tonal values –Filter imperfections
25
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 24 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Optfir.sci Filter Specification tf=f o / f samp tFilter band only up to 0.5 8-bit math limited to s dB tLow ripple and narrow edges --> more taps
26
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 25 DSP Implementation Using PIC18C MCUs Hands-On ‘01 FIR Script: tOpen Optfir.sci as text file in Word –Edit Chdir instruction to reflect current working directory All files must be in same directory as MPLAB project – Edit parameters shown and select Type of filter (lp,bp,hp,sb) Save to re-run tRun Scilab.exe –Choose File->Exec and select Optfir –View resulting scalar plot then hit return to view dB plot tBuild PIC18C project using 18Cfir.asm code –FIR macros in DSP.inc and tap coefficients in eqfir.inc Enable register injection from sine.reg at infeed to ADRESH –Run and view data streaming out to File registers 0x80 on up Cut and paste from hex listing of File register window to MPLABOUT sheet in DSC.XLS to view plot and DFT of data tapnum=31;//number of filter taps; keep it ODD # fsamp=10000;//in Hz fclow=1000;//for any filter fchigh=1000;//just for BP or SB edge = 300;// centered edge width w1=1.0;//low band ripple weighting increase for lower ripple w2=1.0;//middle band ripple weighting ***Start with 1.0 for all w3=1.0;//high band ripple weighting, only used for bp or sb
27
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 26 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Optfir.sci Filter Excercise tAssume sampling at 10ksmps tDerive a LP filter with an edge at 1900Hz to 2100Hz – 40db rejection and 1dB passband ripple –How many taps are needed? Implement filter in 18Cfir.asm and test against fir.reg tHow many poles are needed in the analog antialiasing filter? –Easier to center filter at F samp /4 tHow is this filter performance different from an analog filter? tExtra Credit: filter to find other frequencies
28
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 27 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Unsigned normalized output z -1 X(n) Y(n) Input Base Output + G1G1 + + + z -1 G2G2 G3G3 G2G2 G1G1 + G0G0 + + G0G0 Analog board version of FIR X(n) = G n X(n) = (G n +512)X(n) - 512 X(n) + 0x80 0 255 0 + - +
29
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 28 DSP Implementation Using PIC18C MCUs Hands-On ‘01 FIR vs. IIR tIIR –use less stages for given edge –transformable from analog filter –Oscillator, Clock recovery –Cover wider F range tFIR –Always stable, less fussy –Defined Group delay –Data transmission, interpolators –Echo cancellation, adaptive filters
30
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 29 DSP Implementation Using PIC18C MCUs Hands-On ‘01 IIR from Analog filter tPre-warped Bilinear Transform –Choose a nice analog filter –need to fudge frequency by s/ p p = tan( cutoff /2T) –s --> z -1 plane via (z -1 - 1 )/(z -1 +1) –Or just use Scilab Optiir.sci Left half s-plane maps to interior of unit circle in z -1 plane
31
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 30 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Basic BiQuad IIR z -1 X(n) Y(n) Input Output a1a1 z -1 a2a2 a0a0 -b 1 -b 2 H(z) = (a 0 + a 1 z -1 + a 2 z -2 ) (1 + b 1 z -1 + b 2 z -2 ) 2-pole, 2-zero filter stage
32
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 31 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Z-Transform ts = + j ; z = r e -j tUnstable when poles (x) are on right side of s- plane, outside of unit circle (z-plane) Re Im r Z- Plane r =1 Re S - Plane
33
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 32 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Example - IIR Filter When , output will be infinite –In other words, when the signal frequency is the same as a pole, the filter output will try to drive to infinity. Because of the nature of the IIR filter, it may never return to zero –This is not good, because the filter will eventually encounter frequencies at this frequency ultimately see this frequency applied. tSo why use poles? Their proximity to zeros determines the slope of the transition region. This is what gives IIR filters their power. Recall that FIR filters have only zeros, hence require many terms for an equivalently sharp cutoff Real Imag H[z]
34
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 33 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Example - IIR Filter tMore about poles –Since they guarantee instability when placed on the unit circle, the alternative is move them closer to the origin, on the radial of the desired frequency –A sharp cutoff with a high gain requires the poles to be as close to the unit circle as possible –A highly stable filter requires the poles to be as close to the origin as possible Real Imag H[z]
35
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 34 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Overview - IIR Filter Tapped Delay Line Form z -1 x[n] a0a0 a1a1 a2a2 a N-1 y[n] b N-1 b2b2 z -1 b1b1 tTapped Delay Line Form
36
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 35 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Overview - IIR Filter Bi-Quad Form tCascade (Bi-Quadratic) Form z -1 x[n] z -1 a 11 a 01 a 21 y[n] -b 11 -b 21 z -1 a 1N a 0N a 2N -b 1N -b 2N
37
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 36 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Basic BiQuad IIR PIC form H(z) = (a 0 + a 1 z -1 + a 2 z -2 ) (1 + b 1 z -1 + b 2 z -2 ) z -1 X(n) Y(n) Input 8-bit Output: signed 15-bit a1a1 z -1 a2a2 a0a0 -b 1 -b 2 z -1 From Eqiir: H(z)= C*(a 0 /C +a 1 /C q + a 2 /C q 2 ) (1 + b 1 q + b 2 q 2 ) 0 256 -16K +16K
38
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 37 DSP Implementation Using PIC18C MCUs Hands-On ‘01 IIR Script: tOpen Optiir.sci as text file in Word –Edit Chdir instruction to reflect current working directory –Edit parameters shown Save to re-run tRun Scilab.exe –Choose File->Exec and select Optiir –View resulting scalar plot then hit return to view dB plot tBuild PIC18C project using 18Ciir.asm code –IIR macros in DSP.inc and tap coefficients in eqiir.inc Ping filter by changing ADRESH value –Run and view data streaming out to File registers 0x80 on up Cut and paste from hex listing of File register window to MPLABOUT sheet in DSC.XLS to view plot and DFT of data fsamp=10000;//in Hz fclow1=600;//for any filter iir or eqiir function fclow2=670;//for any filter fchigh1=790;//just for BP or SB iir or eqiir function fchigh2=890;//just for BP or SB deltapass=0.1;//pass band ripple deltastop=0.01;//pass band ripple stab=1.00;//adjust down to 0.98 or so to kill oscillations gain=0.9;//adjust down gain per stage to eliminate overflows order=0;//use 0 to optimize with eqiir (recommended) or set // filter order PER SIDE here (ie 3 with bp is 6th order filter) typefil='lp';// 'lp' or 'bp' or 'hp' or 'sb'
39
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 38 DSP Implementation Using PIC18C MCUs Hands-On ‘01 Optiir.sci Filter Excercise tAssume sampling at 10ksmps tDerive a clock recovery filter/oscillator at 625Hz tDerive a BP filter with a narrow 30Hz peak at 625Hz –Ripple and stopband rejection are not critical –Make sure filter is stable with gain ~ 1.0 tImplement filter in 18Ciir.asm and test by pinging ADRESH How long does it take to ring down to 10% of signal? tWhat other frequencies will this filter ring at? tZeros don’t matter so much here, why not? –Just make sure zeros cancel (LP feed forward terms) Poles of sin( 0 nT) --> 1 - 2*cos ( 0 nT) z -1 + z -2
40
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 39 DSP Implementation Using PIC18C MCUs Hands-On ‘01 DSC Summary t-48dB filters with PIC18C, no FFT –-96dB dsPIC™ tFIR 0.9uSec/tap (typically ~41 taps) –0.1uSec dsPIC™ tIIR 5.9uSec/Biquad (typically ~3 sections) –0.5uSec dsPIC™ tdsPIC™ is simply a PIC MCU-like16-bit uC with DSP stuff tdsPIC™ is 1 year out
41
DSP Implementation Using PIC18C MCUs Hands-On ‘01 © 2001 Microchip Technology Incorporated. All rights reserved. Slide 40 The Microchip name, logo, PIC, PICmicro, PICMASTER, PICSTART, PRO MATE, KEELOQ, SEEVAL, MPLAB and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. Total Endurance, ICSP, In-Circuit-Serial Programming, FilterLab, MXDEV, microID, FlexROM, fuzzyLAB, MPASM, MPLINK, MPLIB, PICDEM, Migratatable Memory, FanSense, ECONOMONITOR and SelectMode are trademarks and SQPT is a service mark of Microchip in the U.S.A. All other trademarks mentioned herein are the property of their respective companies. Information subject to change. Windows is a registered trademark of Microsoft Corporation. SPI is a trademark of Motorola. I 2 C is a registered trademark of Philips Corporation. Microwire is a registered trademark of National Semiconductor Corporation. All other trademarks herein are the property of their respective companies. © 2001 Microchip Technology Incorporated. All rights reserved. “Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by updates. No representation or warranty is given and no liability is assumed by Microchip Technology Inc. with respect to the accuracy of such information, or infringement of patents arising from any such use of otherwise. Use of Microchip’s products as critical components in life support systems is not authorized except with express written approval by Microchip. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights.”
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.