VIEWS: 8 PAGES: 22 POSTED ON: 5/10/2010
DSP Basics 1.0 DSP SYSTEMS 2 Example 1 Delay 3 Example 2 Moving average filter 4 Example 3 Differentiator 5 Example 4 Integrator / Accumulator 6 Example 5 A 3 term Controller 6 Example 6 Audio Tone Control 7 Example 7 Sine wave Generator 8 Example 8 AC measurement 9 2.0 TRANSFER FUNCTIONS 10 3.0 SAMPLING 11 3.1 Aliasing 12 3.2 Reconstruction (Spectral Images) 13 3.3 Reconstruction filtering 14 3.4 A 2 times Oversampling DSP System 16 3.5 DSP interface hardware 17 4.0 DIGITAL SIGNALS 18 4.1 The impulse (n) 18 4.2 The unit step u(n) 18 4.3 The ramp r(n) or n.u(n) 18 4.4 Digital sine wave 19 5.0 CORRELATION 20 5.1 Cross correlation 20 5.2 Auto Correlation 21 6.0 CONVOLUTION 22 1 1.0 DSP systems (a) Analog input / output X(t) X(n) 0 T 2T 3T nT Anti alias Sampling Processing Reconstruction X(t) X(n) Y(n) Y(t) Filter ADC DSP DAC Filter X(t) The analog (continuous) input signal X(n) The sampled (discrete) input signal Y(n) The sampled (discrete) output signal Y(t) The analog (continuous) output signal Sampling theorem An analog signal may be completely represented by sampling (and completely recovered ) provided Sampling frequency (Fs) = 2. max. input frequency (b) Digital input / output X(n) Y(n) Sequence of Numbers DSP Sequence of Numbers 2 Example 1 Delay (a) One Sample Delay Simulation Diagram Difference Equation Y(n) = X(n-1) X(n) Notation X(n) The present input sample D X(n-1) The previous input sample X(n+1) The next input sample Y(n) Y(n) The calculated output sample n = 0, 1, 2, 3, 4, …… Input = X(n) = 5, 3, 7, 9, 3, …… Output = Y(n) = 0, 5, 3, 7, 9, …… (a) Two Sample Delay Difference Equation Y(n) = X(n-2) Input = X(n) = 5, 3, 7, 9, 3, …… Output = Y(n) = 0, 0, 5, 3, 7, 9, …… 2 sample delay at frequency 250Hz (Fs = 16 KHz) Simulation Diagram X(n) 1.50 1.00 D 0.50 Input 0.00 D output 13 17 21 25 29 33 37 41 45 49 1 5 9 -0.50 Y(n) -1.00 -1.50 2 sample delay at frequency 1000Hz (Fs = 16 KHz) 1.50 1.00 0.50 Input 0.00 output 1 5 9 13 17 21 25 29 33 37 41 45 49 -0.50 -1.00 -1.50 3 Example 2 Moving average filter Gets the average of a number of input values A two term moving average filter gets the average of the present value and the previous value of the input. (A 5 term averager will calculate the average of 5 samples) Difference equation: Y(n) = 0.5X(n) + 0.5X(n-1) Operates as a smoothing or low pass filter Simulation Diagram Frequency response 0.5 X(n) + Y(n) 1 D 0.5 Apply this filter to records of daily temperature data given below Day: n 1 2 3 4 5 6 7 8 9 Temp: X 15 14 16 13 12 15 14 16 15 At day 4 i.e. n = 4 Y(4) = 0.5 X(4) + 0.5X(3) = 0.5 (13) + 0.5 (16) = 14.5 i.e. the output for day 4 is 14.5 Note the first day calculation has incomplete data. Standard practice is to use zero for samples not available. If using Exel input data needs to be filled with leading zeros. Result of a 7 point moving average on Wind speed data 25 20 15 input data 10 Filtered data 5 0 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 4 Example 3 Differentiator x Δx/ Δt The change in x divided by the time difference Δx Y(n) = [X(n) - X(n-1)]/T A differentiator is a high pass filter Δt i.e. d/dt (sin 2ft) = 2f Cos 2ft Gain is proportional to frequency. T t The 1/T multiplier can be removed resulting in a simpler equation which still acts as a differentiator but with less Gain Difference Equation Y(n) = X(n) - X(n-1) Simulation Diagram Frequency response 1 X(n) + Y(n) 2 D -1 Apply this filter to records of daily temperature data given below Day: n 1 2 3 4 5 6 7 8 9 Temp: X 15 14 16 13 12 15 14 16 15 At day 4 i.e. n = 4 Y(4) = X(4) - X(3) = 13 - 16 = 3 o The output for day 4 is 3 . i.e. the change in temp between day 3 and day 4. The Differentiator detects sudden changes in data trends as shown below 2.50 2.00 1.50 1.00 0.50 Input 0.00 output 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 -0.50 -1.00 -1.50 -2.00 -2.50 5 Example 4 Integrator / Accumulator Piecewise Integration is calculated by adding the next rectangle to the previously accumulated result i.e. new area = old area + new rectangle x y(n) = Tx(n) + y(n-1) where T is the sampling period Tz Transfer Function z 1 T T Simulation Diagram X(n) + Y(n) D 1 This process is also used as an accumulator without the T i.e. y(n) = x(n) + y(n-1) Not usually used as a standalone process as the output is unbounded for constant input. Cannot be used if the average input is not zero i.e. no DC, marginally stable. Can be used as part of a feedback system e.g. AGC or 2 or 3 term controllers see below Example 5 A 3 term Controller PID controller Sampling time is 0.01 secs 5.56 Proportional Gain Load Load Speed output 0.1 Vin In1 Out1 15 Tacho Speed Integ Gain DAC DC motor Required speed Integerator In1 Out1 1 Diff Gain Differentiator Integrator reduces the error between required speed and actual speed. Differentiator increases system reaction time i.e. motor speeds up quicker. The Motor and tacho are the only analogue blocks everything else is digital. There should be an ADC after the tacho. 6 Example 6 Audio Tone Control Shelving Filter The amount of boost/cut is controlled by the coefficients. (0.5 in the example below) Positive values for bass boost and negative for treble boost. The middle frequency is unchanged. The effect is increased as the coefficient approaches 1 and decreased as the coefficient approaches 0. Example Bass boost Difference Equation: y(n) = x(n) + 0.5x(n-1)+ 0.5y(n-1) z 0. 5 Transfer Function: z 0. 5 Simulation Diagram 1 X(n) + + Y(n) D D 0.5 0.5 Frequency Response 3 2.5 2 1.5 1 0.5 0 0 1000 2000 3000 4000 5000 6000 7000 8000 7 Example 7 Sine wave Generator If 2 values of a sine wave are known the next one can be calculated. The first 2 values are initialised then all values can be calculated using the preceding 2 values. i.e. start with 2 values then calculate 3rd value, wait one sample then calculate 4th value using 2nd and 3rd values, discard 1st value. 5th value is calculated using 3rd and 4th values. Etc. The coefficient in the Difference equation determines the frequency. The initial values determine the voltage. Difference Equation for a 500Hz sine wave if Fs is 16KHz. Y(n) = 1.96 Y(n-1) – Y(n-2) First value = 0 1*sin(0) Second value= 0.195 ( for a peak of 1) 1*sin(pi/16) Third value = 1.96*0.195 – 0 = 0.382 Forth value = 1.96*0.382 - 0.195 = 0.554 output 1.50 1.00 0.50 0.00 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 -0.50 -1.00 -1.50 The above equation uses only one multiply and a subtraction per sample. Other options: High level function: y = sin(n*angle) Disadvantage: slow Lookup table: Preload a full cycle into memory, frequency can be changed by skipping some values. Disadvantage: Only a fixed no of different frequencies may be produced Theory A Filter is created with a pole on the unit circle at an angle corresponding to the frequency of oscillation. See poles and zeros later. 8 Example 8 AC measurement AC Rectifier Smoothing Scale Read signal Filter Value Before an AC signal can me measured it must be converted to a steady value. Shown above is one method of AC measurement. The signal is first rectified (lose the sign) Converted to an average value using a digital smoothing or low pass filter (same idea as in a circuit for a power supply), The value is scaled to give RMS or peak value, this is a simple multiply. The rectifier is needed in the above system since the average value of an AC signal is 0. The effectiveness of the smoothing filter is crucial to the overall accuracy of the measurement. Considerations: Will there be a ripple after smoothing? For what range of frequencies will the system work. Will there be a significant delay in the filter before we can read the value? The example below shows a slight measurement error of 0.006V and a delay of about 200 samples, which is 0.0125 Seconds at 16KHz sampling. input 1 0.5 0 -0.5 -1 0 50 100 150 200 250 300 350 400 450 500 output 1 0.8 0.6 0.4 0.2 0 0 50 100 150 200 250 300 350 400 450 500 9 2.0 Transfer functions Definition TF = Output / Input Output is calculated by simple multiplication of the transfer function by the input. Systems can be created easily by combining Blocks. TF Output = TF. Input TF1 TF2 TF = TF1. TF2 TF1 + TF = TF1 + TF2 TF2 (a) Convert a difference equation to a Transfer Function Note : Multiply by z is a time shift of one sample Difference equation y(n) = x(n) + 0.5y(n-1) Convert to z Y(z) = X(z) + 0.5 z-1 Y(z) Collect y’s to left Y(z) - 0.5 z-1 Y(z) = X(z) Y(z)(1 - 0.5 z-1 ) = X(z) Y 1 z Transfer Function = 1 X 1 0.5 z z 0.5 (b) Convert a transfer function to a difference equation Y ( z) z 0.5 Shelving filter H ( z ) X ( z) z 0.5 Multiply out any factors in the transfer function and cross multiply Y(z) [z – 0.5] = X(z) [z+0.5] z Y(z) – 0.5 Y(z) = z X(z) + 0.5 X(z) Divide by highest power of z to ensure all powers of z are negative Y(z) –0.5 z-1 Y(z) = X(z) + 0.5 z-1X(z) Y(z) = X(z) + 0.5 z-1X(z) +0.5 z-1 Y(z) Convert to difference equation using time shift theorem Y(n) = X(n) + 0.5X(n-1) +0.5Y(n-1) 10 3.0 Sampling 11 3.1 Aliasing 12 3.2 Reconstruction (Spectral Images) When a 20 KHz sine wave is sampled at 40 KHz the Reconstructed signal will be a Square wave (without any reconstruction filtering) 13 3.3 Reconstruction filtering 14 Calculating the filter order Example CD player reconstruction filter. Sampling frequency = 44.1 Khz Music bandwidth B = 20 Khz. Attenuation required at first image = 60 dBs (a) no oversampling first image = start of stop band = 44K - 20K = 24 KHz. 60 = 20 n log(24/20) => n = 37 (b) 2X oversampling first image = 88K - 20K = 68 KHz. 60 = 20 n log(68/20) => n = 5.6 (c) 8X oversampling first image = 352K - 20K = 332 KHz. 60 = 20 n log(332/20) => n = 2.5 note: A digital interpolation filter is required in all oversampling cases to remove spectral images caused by the system sampling frequency of 44 Khz. Digital filter order is 37 as calculated in (a) for this example. 15 3.4 A 2 times Oversampling DSP System 16 3.5 DSP interface hardware 17 4.0 Digital signals 4.1 The impulse (n) 1 (n) = 1 at n = 0 (n) = 0 at n 0 -2 -1 0 1 2 n What are the following signals (n-1), (n+3), (2), (0). 4.2 The unit step u(n) 1 u (n) = 1 at n > 0 u (n) = 0 at n < 0 -2 -1 0 1 2 3 4 n 4.3 The ramp r(n) or n.u(n) r(n) = 0, 1, 2, 3, 4, 5, .... etc. . -2 -1 0 1 2 3 4 n Exercise 5.0 find expressions for the signals below (see example 1.1 p 15 Lynn) 1 1 0 1 2 3 4 5 6 7 8 9 n -4 -3 -2 -1 0 1 2 3 4 5 6 n 4 -4 -3 -2 -1 0 1 2 3 4 5 6 n -6 -5 -4 -3 -2 -1 0 1 2 3 4 n -2 18 4.4 Digital sine wave Methods of specifying frequency in DSP Examples are for a 2KHz sine wave if Fs is 16KHz. (a) Actual Frequency Sin(2 f nT) f is between 0 and 8000 Example: f = 2000 Sin(2 2000 n 0.0000625) (b) Normalised Frequency Sin(2 f n ) f is between 0 and 0.5 Example: f = 0.125 Sin(2 0.125 n ) (c) Angle Sin(n ) is between 0 and Example: = /4 Sin(0.25 n) x = Sin n/4 = 0, 0.7, 1, 0.7, 0, -0.7, -1, -0.7, 0, etc. 1 0.5 0 0 1 2 3 4 5 6 7 8 -0.5 -1 n The above signal has period = 8 samples = 8T => frequency 1/8T = Fs/8 Relationship between frequency and f 2 and f Fs Fs 2 = 2 corresponds to a frequency of Fs or 16KHz for our example. = corresponds to a frequency of Fs/2 or 8KHz etc. 2 Fs Number of samples /period or f Periodic signals See p18 Lynn A digital signal will be periodic if it repeats after an integer number of samples. e.g. if Fs = 16 KHz : 2 KHz ( = /4) will repeat after 8 samples (one period) 3 KHz ( = 0.375 or 3/8 ) will repeat after 16 samples (3 periods) A digital signal is periodic if the no of samples in a period is a rational number. m f m is a rational number i. e. or 2 N Fs N where m is any integer (no of periods) and N is the repetition time(no of samples) i.e. = 0.125 (500Hz) /2 = 0.0625 = 1/16 => repeats after 16 samples but = 0.5 (636.619Hz) /2 = 0.0795774 does not repeat 19 5.0 Correlation 5.1 Cross correlation The correlation of two signals provides a statistical comparison between the two signals and is calculated as the average cross product of the signals as a function of the time shift between them. The output will reflect any components held in common. 1 N 1 x ( n) y ( n k ) rxy ( k ) N n0 output is a function of k not n. where N is the length of the signal. if the signal is periodic we only need to average over a period, ( i.e. N = the period ) note x(n)y(n-k) is often used as it contains the same information i.e. rxy(-k) or ryx(k) Example 5.1 Calculate the crosscorrelation of the two signals x(n) = 0.5,1,1,1 and y(n) = 1,0.5,0.2,0.1 x(n) y(n) 1 1 .5 .5 .2 .1 0 1 2 3 n 0 1 2 3 n n X(n) 1 .5 0 1 2 3 1 n at k = 3 we get Y(n+3) .5 shift .2 .1 Crosscorrelation = .05, 0.2, 0.55, 1.3, 1.7, 1.5, 1 as k goes from +3 to -3 Example 5.2 A noisy FSK signal (top) is first correlated with 1650Hz representing logic 1(middle) and then correlated against 1850 Hz representing logic 0 (bottem) noisy fsk signal 2 1 0 -1 -2 0 20 40 60 80 100 120 140 160 180 200 correlation with 1850Hz 100 50 0 -50 -100 -100 -80 -60 -40 -20 0 20 40 60 80 100 correlation with 1650Hz 100 50 0 -50 -100 -100 -80 -60 -40 -20 0 20 40 60 80 100 20 5.2 Auto Correlation The average product of a signal with a time shifted version of itself 1 N 1 rxx ( k ) x ( n) x ( n k ) N n0 Used to identify repetitive signals in noise A random signal will produce a output only at time shift = 0 while a repetitive signal will come through the correlation The shift (k) is often kept to about 15 -20% of the signal length since the number of products decreases as the shift increases and the results are less reliable. i.e. if the sample size is 100 use k = -15 to +15 for analysis. Examples Original Signal Autocorrelation 2 200 0 0 -2 -200 0 50 100 150 200 -100 -50 0 50 100 2 200 0 0 -2 -200 0 50 100 150 200 -100 -50 0 50 100 2 200 0 0 -2 -200 0 50 100 150 200 -100 -50 0 50 100 2 200 0 0 -2 -200 0 50 100 150 200 -100 -50 0 50 100 Plot 1: Sine Wave. Repetitive patterns come through the correlation unchanged Plot 2: DC. Same for constant levels. Plot 3: Random noise is only correlated at zero shift so produces a single spike at 0. Plot 4: Noisy signal. The repetitive pattern is more obvious after correlation since random noise is correlated into a single spike. 21 6.0 Convolution x ( n ) * y ( n) x ( k ) y (n k ) k One signal is reversed and shifted through the other with the sum of the cross products calculated at each shift. Note Convolution output is a function of n (time). Either signal may be shifted since X * Y = Y * X Calculation method: set a value for n then calculate the sum for all the k values, this gives the result for the n value chosen, repeat for all other values of n. We will see later how convolution is used to implement FIR filters by getting the convolution of the input signal with the filters impulse response. Example 6.1 Calculate the convolution of the two signals x(n) = 1,0.5,0.2,0.1 and y(n) = 0.5,1,1,1 X(n) Y(n) 1 1 .5 .5 .2 .1 0 1 2 3 n 0 1 2 3 n Y(n) is reversed and shifted through X(n) 1 X(k) .5 .2 .1 0 1 2 3 k n=0 1 .5 Output(0) = 0.5 Y(-k) only k= 0 produces an output -3 -2 -1 0 k 1 n=1 .5 Y(1-k) Output(1) = 1+0.25=1.25 only k=0 and k=1 -2 -1 0 1 k produce outputs 1.758 1.6 1.25 0.8 Output(n) .5 0.3 0.1 0 1 2 3 4 5 6 n 22