Upload
derick-kelly
View
218
Download
0
Embed Size (px)
Citation preview
DSP Implementation Using PIC18C MCUs Hands-On ‘01
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 1
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 2
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Excel DSP Activity-PlaycenterExcel DSP Activity-Playcenter
Hands-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
Copy all files to C:\DSC from CD – disable read-only attributes on files if set
Open DSC.XLS spreadsheet Open MPLAB®IDE
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 3
DSP Implementation Using PIC18C MCUs Hands-On ‘01
General DSP ModelGeneral DSP Model
Accepts an analog signal Converts this analog signal to digital numbers Performs computations Converts results back into analog signal
Processor
10110101 00100100
Signal Conditioning
Low-pass filter
A to D Converter
Smoothing Filter
D to AConverter
Output Amp
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 4
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Excel DSP Activity-PlaycenterExcel DSP Activity-Playcenter
Sampling-Sine worksheet ,xf1,xf2,xf3– Everything normalized to F/Fc
– 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
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 5
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Excel DSP Activity-PlaycenterExcel DSP Activity-Playcenter
Need to anti-alias filter above 1/2 Fsamp
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 6
DSP Implementation Using PIC18C MCUs Hands-On ‘01Fs = 2000 HzFs = 2000 Hz
dB
650 Hz1950 Hz
3250 Hz
4550 Hz
Which Signals Will Be Aliased Below Fs/2?
2000fs
1000fs/2
40002fs
30003fs/2
50005fs/2
f (Hz)
F aliased = | F signal - (N F sample) |
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 7
DSP Implementation Using PIC18C MCUs Hands-On ‘01Fs = 2000 HzFs = 2000 Hz
dB
650 Hz (50 Hz)1950 Hz (750 Hz)
3250 Hz (550 Hz)
4550 Hz
2000fs
1000fs/2
40002fs
30003fs/2
50005fs/2
N=0 N=1 N=2
f (Hz)
F aliased = | F signal - (N F sample) |
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 8
DSP Implementation Using PIC18C MCUs Hands-On ‘01Fs = 2000 HzFs = 2000 Hz
f (Hz)
dB
(650 Hz)(1950 Hz) 50 Hz (3250 Hz)
750 Hz(4550 Hz)550 Hz
500
1000fs/2
N=0
750
250
F aliased = | F signal - (N F sample) |
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 9
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Windowing – Windowing – RectangularRectangular
Continuous Sine Wave Samples
Time
Time
DTFT of a Finite-LengthSinusoidal 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
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 10
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Windowing – Windowing – HanningHanning
1
HanningWindow
SampleInterval
)/2cos(5.05.0Hanning)( Nnnw
By removing the starting and ending abrupt transitions, the side-lobe leakage is greatly reduced.
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 11
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Windowing – Windowing – ResponseResponse
Rectangular
Hanning
Hamming
Frequency
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 12
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Excel DSP Activity-PlaycenterExcel DSP Activity-Playcenter
Fourier Transform-DFT worksheet– Square wave to Sinc function pair– 0<n<N time samples with T period -> 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=0N-1 x(n)e-j2knk = 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)
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 13
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Excel DSP Activity-PlaycenterExcel DSP Activity-Playcenter
Windowing 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
x H(f) in freq domain becomes x h(t) in the time domain
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 14
DSP Implementation Using PIC18C MCUs Hands-On ‘01Overview - FIR Filter Tapped Delay Overview - FIR Filter Tapped Delay
Line FormLine Form
z-1 z-1 z-1x[n]
h0 h1 h2 hN-1
y[n]
x[n-0] x[n-1] x[n-2] x[n-(N-1)]
knxkhnyN
k
1
0
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 15
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Common FIR examples: Moving AverageCommon FIR examples: Moving Average
z-1 z-1 z-1X(n)
Y(n)
1/8
Input
Output
++
+
+
z-1 z-1z-1 z-1
Moving Average
-50
-40
-30
-20
-10
0
10
20
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61
Frequency 0-0.25
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 16
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Overview of Digital FiltersOverview of Digital Filters
Since 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.
The simplest filter utilizes many principles covered so far - and ties together many concepts into one practical application
knxkhnyN
k
1
0
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 17
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Overview of Digital FiltersOverview of Digital Filters
Classified 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
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 18
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Basic Type 1 Transversal FIRBasic Type 1 Transversal FIR
z-1X(n)
Y(n)
Input
Output
+
G1
-
+
+z-1
z-1
z-1
z-1
G2
G3
G2
G1
-
z-1 G0
+
+
G0
Scale0
255-127
+127
24-bit Sum
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 19
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Crossover FilterCrossover Filter
z-1X(n)
Y(n)
Input
LP Output
+
G1
-
+
+z-1
z-1
z-1
z-1
G2
G3
G2
G1
-
z-1 G0
+
+
G0
-
+
Y(n)
HP Output
Basic Type 1 Transversal FIRBasic Type 1 Transversal FIR
X(n) = x(n)z-n
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 20
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Common FIR examples: PID loopCommon FIR examples: PID loop
z-1 z-1 z-1X(n) Y(n)
GI/4
Input Output
++
+
+
GD/3
GP
-
+
+ +
+
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 21
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Sampling and QuantizationSampling and Quantization
4-bit (16 level) ADC sampling a sinewave input
Input Sinewave
ADC Output
Quantization ErrorDuring 1 period (t)
t
OUTPUTDIGITALWORD
0000000100100011010001010110011110001001101010111100110111101111
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 22
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Over Sampling SNR gainOver Sampling SNR gain
SNR max = 1.76 + 6.02B + 10 log (R)
OversamplingRatio ( R)
4 16 64 256
SNRimprovement(dB)
6.02 12.04 18.06 24.08
Increased bitresolution
1 2 3 4
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 23
DSP Implementation Using PIC18C MCUs Hands-On ‘01
FIR PIC MCU realizationFIR PIC MCU realization
X(n) = x(n)z-n where z-n is nT time delay Type I,
– Even symmetry– Odd # taps
Data 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
Filter analysis-H(z) worksheet– Max tonal values– Filter imperfections
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 24
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Optfir.sci Filter SpecificationOptfir.sci Filter Specification f=fo/ fsamp
Filter band only up to 0.5 8-bit math limited to sdB Low ripple and narrow edges --> more taps
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 25
DSP Implementation Using PIC18C MCUs Hands-On ‘01
FIR Script:FIR Script: Open 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
Run Scilab.exe– Choose File->Exec and select Optfir– View resulting scalar plot then hit return to view dB plot
Build 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 Hzfclow=1000; //for any filterfchigh=1000; //just for BP or SBedge = 300; // centered edge widthw1=1.0; //low band ripple weighting increase for lower ripplew2=1.0; //middle band ripple weighting ***Start with 1.0 for allw3=1.0; //high band ripple weighting, only used for bp or sb
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 26
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Optfir.sci Filter ExcerciseOptfir.sci Filter Excercise Assume sampling at 10ksmps Derive 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 How many poles are needed in the analog antialiasing
filter?– Easier to center filter at Fsamp/4
How is this filter performance different from an analog filter?
Extra Credit: filter to find other frequencies
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 28
DSP Implementation Using PIC18C MCUs Hands-On ‘01
FIR vs. IIRFIR vs. IIR
IIR – use less stages for given edge– transformable from analog filter– Oscillator, Clock recovery– Cover wider F range
FIR– Always stable, less fussy– Defined Group delay– Data transmission, interpolators– Echo cancellation, adaptive filters
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 29
DSP Implementation Using PIC18C MCUs Hands-On ‘01
IIR from Analog filter IIR from Analog filter
Pre-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 tointerior of unit circle in z-1 plane
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 30
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Basic BiQuad IIRBasic BiQuad IIR
z-1
X(n) Y(n)Input
Outputa1
z-1 a2
a0
-b1
-b2
H(z) = (a0 + a1 z-1 + a2 z-2) (1 + b1 z-1 + b2 z-2 )
2-pole, 2-zero filter stage
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 31
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Z-TransformZ-Transform s = + j ; z = r e-j
Unstable when poles (x) are on right side of s-plane, outside of unit circle (z-plane)
Re
ImIm
r
Z- Plane
r =1
Re
S - Plane
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 32
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Example - IIR FilterExample - 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.
So 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]
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 33
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Example - IIR FilterExample - IIR Filter
More 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]
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 34
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Overview - IIR Filter Tapped Delay Line Overview - IIR Filter Tapped Delay Line FormForm
z-1 z-1 z-1x[n]
a0 a1 a2 aN-1
y[n]
bN-1 b2
z-1 z-1 z-1
b1
knykbknxkanyN
k
N
k
1
1
1
0
Tapped Delay Line Form
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 35
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Overview - IIR Filter Bi-Quad FormOverview - IIR Filter Bi-Quad Form
Cascade (Bi-Quadratic) Form
N
k kk
kkk
nbnxb
nxanxaany
1 21
210
)2()1(1
)2()1(][
z-1
x[n]
z-1
a11
a01
a21
y[n]
-b11
-b21
z-1
z-1
a1N
a0N
a2N
-b1N
-b2N
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 36
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Basic BiQuad IIR PIC formBasic BiQuad IIR PIC form
H(z) = (a0 + a1 z-1 + a2 z-2) (1 + b1 z-1 + b2 z-2 )
z-1
X(n) Y(n)Input 8-bit
Output:signed15-bita1
z-1 a2
a0
-b1
-b2
z-1
z-1
From Eqiir:H(z)= C*(a0/C +a1/C q + a2 /C q2) (1 + b1 q + b2 q2 )
0
256
-16K
+16K
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 37
DSP Implementation Using PIC18C MCUs Hands-On ‘01
IIR Script:IIR Script:
Open Optiir.sci as text file in Word– Edit Chdir instruction to reflect current working directory – Edit parameters shown
• Save• to re-run
Run Scilab.exe– Choose File->Exec and select Optiir– View resulting scalar plot then hit return to view dB plot
Build 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 Hzfclow1=600; //for any filter iir or eqiir functionfclow2=670; //for any filterfchigh1=790; //just for BP or SB iir or eqiir functionfchigh2=890; //just for BP or SBdeltapass=0.1; //pass band rippledeltastop=0.01; //pass band ripplestab=1.00; //adjust down to 0.98 or so to kill oscillationsgain=0.9; //adjust down gain per stage to eliminate overflowsorder=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'
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 38
DSP Implementation Using PIC18C MCUs Hands-On ‘01
Optiir.sci Filter ExcerciseOptiir.sci Filter Excercise Assume sampling at 10ksmps Derive a clock recovery filter/oscillator at 625Hz Derive 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
Implement filter in 18Ciir.asm and test by pinging ADRESH
How long does it take to ring down to 10% of signal? What other frequencies will this filter ring at? Zeros don’t matter so much here, why not?
– Just make sure zeros cancel (LP feed forward terms)
Poles of sin(0nT) --> 1 - 2*cos (0nT) z-1 + z-2
© 2001 Microchip Technology Incorporated. All rights reserved. Slide 39
DSP Implementation Using PIC18C MCUs Hands-On ‘01
DSC SummaryDSC Summary
-48dB filters with PIC18C, no FFT– -96dB dsPIC™
FIR 0.9uSec/tap (typically ~41 taps)– 0.1uSec dsPIC™
IIR 5.9uSec/Biquad (typically ~3 sections)– 0.5uSec dsPIC™
dsPIC™ is simply a PIC MCU-like16-bit uC with DSP stuff
dsPIC™ is 1 year out
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.
I2C 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.”