What is a Filter? Input Signal Amplitude Output Signal Frequency Time Sequence Low Pass Filter Time Sequence What is a Filter Input Signal Amplitude Output Signal Frequency Signal Noise Frequency Transform Signal Low Pass Filter Noise Frequency Transform FIR and IIR Filters • A finite impulse response (FIR) filter produces an output that will go to zero when the input goes to zero and stays zero • Its response to an impulse function input is finite • An infinite impulse response (IIR) filter produces an output that may continue indefinitely even after its input goes to zero and stays zero • Its response to an impulse function input is infinite FIR and IIR Filters • An FIR filter’s z-Transform has zeroes and poles only at 0 + j0, so it is always stable • An IIR filter’s z-Transform has pole(s) and may also have zeroes, so it may be unstable if any of the poles are outside the unit circle • To get the same sharpness of filter shape, an FIR filter may require more delay elements and more computations than an equivalent IIR filter • But an IIR filter won’t have linear phase FIR Filters • A simple example of a moving average FIR filter Input C0 In z-1 C1 In-1 z-1 C2 In-2 Sum Output Output = (C0 + C1 * z-1 + C2 * z-2) Input Where C0 = C1 = C2 = 1/3 Output/Input = (z2 + z + 1) / (3 z2 ) Two zeros at -1/2 + j sqrt(3)/2 and -1/2 - j sqrt(3)/2 Two poles at 0 + j0 FIR Filters • Pole-Zero Plot and Frequency Response http://en.wikipedia.org/wiki/Finite_impulse_response FIR Filters • Program Output Enter the integer sampling rate (samples per second): [5000] Enter number of poles and number of zeros [2 2] Amplitude at 0 Hz: 3.000, phase: 0 degrees Enter the real and imag values for 2 poles: Amplitude at 200 Hz: 2.937, phase: -14 degrees Pole 0: [0 0] Amplitude at 400 Hz: 2.753, phase: -29 degrees Pole 1: [0 0] Amplitude at 600 Hz: 2.458, phase: -43 degrees Enter the real and imag values for 2 zeros: Amplitude at 800 Hz: 2.072, phase: -58 degrees Zero 0: [-0.5 0.866] Amplitude at 1,000 Hz: 1.618, phase: -72 degrees Zero 1: [-0.5 -0.866] Amplitude at 1,200 Hz: 1.126, phase: -86 degrees Amplitude at 1,400 Hz: 0.625, phase: -101 degrees Amplitude at 1,600 Hz: 0.148, phase: -115 degrees Amplitude at 1,650 Hz: 0.037, phase: -119 degrees Amplitude at 1,700 Hz: 0.072, phase: -302 degrees Amplitude at 1,800 Hz: 0.275, phase: -310 degrees Amplitude at 2,000 Hz: 0.618, phase: -324 degrees Amplitude at 2,200 Hz: 0.860, phase: -338 degrees Amplitude at 2,400 Hz: 0.984, phase: -353 degrees Amplitude at 2,500 Hz: 1.000, phase: 0 degrees FIR Filters • How do we get values for FIR filter coefficients? • We find the impulse response of the system either by calculation or experiment and sample it • The sample values are the coefficients T System C0 … Cn IIR Filters • A difference equation is a digital approximation for an analog differential equation • A typical difference equation (with a0=1, m <= n): a0Yt + a1Yt-1 + . . . + anYt-n = b0Xt + b1Xt-1 + … + bmXt-m • An IIR filter is implemented as the solution for a difference equation • The z-Transform for the IIR filter: Y/X = (b0zn + b1zn-1 + … + bmzn-m)/(a1zn + a2zn-1 + . . . + an) IIR Filters • A simple example of a moving average IIR filter X X + Y X z-1 1Y = X + (1 - ) Y z-1 Where 0 < Y/X = z / (z – (1 - )) Zero at z = 0 + j 0 Pole at 1 - <1 IIR Filters • Pole-Zero Plot and Frequency Response Plot the amplitude 0 + j1 -1 + j0 O Imaginary X Sweep vector points over all frequencies Real 1 + j0 f Plot the phase shift f 0 – j1 IIR Filters • Program Output Inverse z-Transform Calculation Enter the integer sampling rate (samples per second): [5000] Amplitude at 0 Hz: 4.000, phase: 0 degrees Enter number of poles and number of zeros Amplitude at 200 Hz: 3.020, phase: -34 degrees [1 1] Amplitude at 400 Hz: 2.008, phase: -47 degrees Enter the real and imag values for 1 poles: Amplitude at 600 Hz: 1.460, phase: -49 degrees Pole 0: [.75 0] Amplitude at 800 Hz: 1.148, phase: -47 degrees Enter the real and imag values for 1 zeros: Amplitude at 1,000 Hz: 0.954, phase: -43 degrees Zero 0: [0 0] Amplitude at 1,200 Hz: 0.825, phase: -38 degrees Amplitude at 1,400 Hz: 0.736, phase: -33 degrees Amplitude at 1,600 Hz: 0.674, phase: -27 degrees Amplitude at 1,800 Hz: 0.630, phase: -21 degrees Amplitude at 2,000 Hz: 0.600, phase: -15 degrees Amplitude at 2,200 Hz: 0.582, phase: -9 degrees Amplitude at 2,400 Hz: 0.573, phase: -3 degrees Amplitude at 2,500 Hz: 0.571, phase: 0 degrees First Order Filters • A first order IIR filter has only one delay z-1 • It tracks well against a step function input (constant level after t=0), but not against a ramp function (constant first derivative) input Steady State Error is zero Time Steady state lag is inversely prop to phase gain Time First Order Filters • Coefficient is a compromise between: – Large value for small steady state error, but with wider bandwidth letting in more noise – Small value to exclude more noise, but larger steady state error with a constant derivative input • A second order filter can be used to avoid this compromise Second Order Filters • A second order IIR filter learns frequency offset • It tracks well against a ramp function input, but may ring around a step or ramp function input unless coefficients are “critically damped” Steady State Error is zero, but may ring Steady State Error is zero One way to implement 2nd Order Filter • Coefficients and are usually selected so that phase difference is learned faster than frequency offset is learned • Coefficient relationships: 0 < < < 1 X(T n) Y(T n) + z -1 + F(T n) z -1 Implement Second Order Filter z-Plane Difference Equations F(T n) = (1 – ) F( T n-1) + Y(T n) = (1 – ) Y(T n-1) + X(T n) X(T n) + F(T n) Stable if all Poles inside unit circle j z = +f Real -1 + j0 1 + j0 z-Transform Y(z) = z [( + ) z – (1 – )] [z – (1 – )] [ z – (1 – )] X(z) Two Poles at: z = (1 – ) + j 0 z = (1 – ) + j 0 Two Zeros at: z=0+j0 z = (1 – )/( + ) + j 0 -j z = -f Critically damped since both poles are on the real axis so it won’t ring Implement Second Order Filter • Program Output for = 7/8 and = 1/8 Inverse z-Transform Calculation Enter the integer sampling rate (samples per second): [1000] Enter number of poles and number of zeros [2 2] Enter the real and imag values for 2 poles: Pole 0: [.875 0] Pole 1: [.125 0] Enter the real and imag values for 2 zeros: Zero 0: [0 0] Amplitude at 0 Hz: 2.139, phase: 0 degrees Zero 1: [.766 0] Amplitude at 100 Hz: 1.105, phase: -135 degrees Amplitude at 200 Hz: 0.984, phase: -197 degrees Amplitude at 300 Hz: 0.904, phase: -251 degrees Amplitude at 400 Hz: 0.854, phase: -303 degrees Amplitude at 500 Hz: 0.837, phase: -354 degrees Second Order Filters • Compromise between noise exclusion and steady state error for constant derivative is not needed • However, it has a constant steady state error for a constant second derivative input function Steady State Error is zero Steady state lag • If that is a problem, use a cascade of second order filters to get a higher order filter