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 Signal Noise Frequency Transform Low Pass Filter 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 I n I n-1 z -1 z -1 I n-2 C 0 C 1 C 2 Sum Output Output = (C 0 + C 1 * z -1 + C 2 * z -2 ) Input Where C 0 = C 1 = C 2 = 1/3 Output/Input = (z 2 + z + 1) / (3 z 2 ) 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] Enter the real and imag values for 2 poles: Pole 0: [0 0] Pole 1: [0 0] Enter the real and imag values for 2 zeros: Zero 0: [-0.5 0.866] Zero 1: [-0.5-0.866] Amplitude at 0 Hz: 3.000, phase: 0 degrees Amplitude at 200 Hz: 2.937, phase: -14 degrees Amplitude at 400 Hz: 2.753, phase: -29 degrees Amplitude at 600 Hz: 2.458, phase: -43 degrees Amplitude at 800 Hz: 2.072, phase: -58 degrees Amplitude at 1,000 Hz: 1.618, phase: -72 degrees 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 C 0 C n
IIR Filters A difference equation is a digital approximation for an analog differential equation A typical difference equation (with a 0 =1, m <= n): a 0 Y t + a 1 Y t-1 +... + a n Y t-n = b 0 X t + b 1 X t-1 + + b m X t-m An IIR filter is implemented as the solution for a difference equation The z-transform for the IIR filter: Y/X = (b 0 z n + b 1 z n-1 + + b m z n-m )/(a 1 z n + a 2 z n-1 +... + a n )
IIR Filters A simple example of a moving average IIR filter X X + Y X z -1 1 - Y = X + (1 - ) Y z -1 Where 0 < < 1 Y/X = z / (z (1 - )) Zero at z = 0 + j 0 Pole at 1 -
IIR Filters Pole-Zero Plot and Frequency Response -1 + j0 0 + j1 O Imaginary X Sweep vector points over all frequencies Real 1 + j0 Plot the amplitude Plot the phase shift f 0 j1 f
Program Output IIR Filters Inverse z-transform Calculation Enter the integer sampling rate (samples per second): [5000] Enter number of poles and number of zeros [1 1] Enter the real and imag values for 1 poles: Pole 0: [.75 0] Enter the real and imag values for 1 zeros: Zero 0: [0 0] Amplitude at 0 Hz: 4.000, phase: 0 degrees Amplitude at 200 Hz: 3.020, phase: -34 degrees Amplitude at 400 Hz: 2.008, phase: -47 degrees Amplitude at 600 Hz: 1.460, phase: -49 degrees Amplitude at 800 Hz: 1.148, phase: -47 degrees Amplitude at 1,000 Hz: 0.954, phase: -43 degrees 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 Steady state lag is inversely prop to phase gain Time 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 2 nd 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 ) + z -1 Y(T n ) + F(T n ) z -1
Implement Second Order Filter z-plane Difference Equations F(T n ) = (1 ) F( T n-1 ) + X(T n ) Y(T n ) = (1 ) Y(T n-1 ) + X(T n ) + F(T n ) Stable if all Poles inside unit circle j z = +f Real Y(z) = z-transform z [( + ) z (1 )] [z (1 )] [ z (1 )] X(z) -1 + j0 Two Poles at: z = (1 ) + j 0 z = (1 ) + j 0 Two Zeros at: z = 0 + j 0 z = (1 )/( + ) + j 0 -j 1 + j0 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] Zero 1: [.766 0] Amplitude at 0 Hz: 2.139, phase: 0 degrees 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