Filtering Problem Goal: design a filter to attenuate the disturbances SIGNAL s[n] x[n] v[n] NOISE y[n] s[n] Filter Define Signal and Noise in the Frequency Domain n n n v[n] NOISE SIGNAL s[n] y[n] s[n] Filter x[n] SIGNAL | S ( ) | NOISE | V ( ) | P Mostly NOISE 0 P Mostly SIGNAL (rad ) Mostly NOISE IDEAL Filter Since the filter has real coefficients, we need only the positive frequencies H ( ) PASS Band p STOP Band Non-IDEAL Filter Since the filter has real coefficients, we need only the positive frequencies | H () | PASS Band p S Trans. Band STOP Band Design a Low Pass Filter: IDEAL Hd ( ) 1 P P First we can determine an infinite length expansion using the DTFT: H d () DTFThd [n] j n h [ n ] e d n 1 hd [n] IDTFTH d () 2 1 hd [n] 2 P j n e d P j n H ( ) e d d sin P n P sinc P n n Design a Low Pass Filter: IDEAL (continued) hd [n] This means the following. If then H d () j n h [ n ] e d n Hd ( ) P sinc P n 1 P P 0.3 hd [n] 0.25 0.2 0.15 0.1 0.05 0 -0.05 -0.1 -60 -40 -20 0 20 40 60 n From IDEAL to FIR Notice that lim hd [n] 0 n Then we can approximate with a finite sum… H d () L j n h [ n ] e d n L … and choose the filter as h[n] hd [n L] 2L H () h[n]e jn n 0 actual 2L hd [n L]e n 0 jn L jn jL hd [n]e e H d ()e jL n L desired Summary of design of Low Pass FIR Given the Pass Band Frequency 0 P The impulse response of the filter: let N 2 L P P h[n] sinc (n L) , n 0,...,N 2L H () h[n]e jn H d ( )e jL n 0 Magnitude and Phase: Magnitude: Phase: H ( ) H d ( ) H ( ) H d ( ) L L in the passband Example of Freq. Response P / 4 h=(wp/pi)*sinc((wp/pi)*(n-L)); freqz(h) Magnitude (dB) 50 20dB 0 -50 -100 0 0.1 0 Phase (degrees) n=0:N; L 20, N 40 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0.9 1 20 -500 -1000 -1500 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) Impulse Response n=0:N; stem(n,h) h[n] 0.3 0.25 0.2 0.15 0.1 0.05 0 -0.05 -0.1 0 5 10 15 20 25 30 35 40 Example with Hamming window 1 1 2n h[n] sinc (n 20) 0.54 0.46 cos , n 0,...,40 4 40 4 hamming window h[n] n=0:N; h0=(wp/pi)*sinc((wp/pi)*(n-L)); h=h0.*hamming(N); stem(n,h) 0.3 0.25 0.2 0.15 0.1 0.05 0 -0.05 0 5 10 15 20 25 30 35 40 Example with Hamming window 1 1 2n h[n] sinc (n 20) 0.54 0.46 cos , n 0,...,40 4 40 4 hamming window Magnitude (dB) 50 0 ~ 50dB -50 -100 -150 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0 Phase (degrees) freqz(h) H ( ) -500 -1000 -1500 Low Pass Filter Design: Analytical Transition Region: depends on the window and the filter length N Attenuation: depends on window only 20 window Rectangular 4 / N Hamming 8 / N Blackman 12 / N attenuatio n 0 -20 attenuation -13dB -40 -43dB -60 -58dB -80 -100 -120 0 0.5 1 1.5 2 2.5 P S transition region 3 Example of Low Pass Filter Design Specs: Pass Band 0 - 4 kHz Stop Band > 5kHz with attenuation of at least 40dB Sampling Frequency 20kHz 40dB Step 1: translate specifications into digital frequency Pass Band 02 4 / 20 2 / 5rad Stop Band 2 5 / 20 / 2 rad Step 2: from pass band, determine ideal filter impulse response hd (n) P 2 2n sinc P n sinc 5 5 4 5 2 5 2 10 10 F kHz Example of Low Pass Filter Design (continued) Step 3: from desired attenuation choose the window. In this case we can choose the hamming window; Step 4: from the transition region choose the length N of the impulse response. Choose an even number N such that: 8 N 10 So choose N=80 which yields the shift L=40. Example of Low Pass Filter Design (continued) Finally the impulse response of the filter 2 2(n - 40) 2 n 0.54 0.46 cos , if 0 n 80, sinc 80 5 h( n) 5 0 otherwise Example of Low Pass Filter Design (continued) The Frequency Response of the Filter: H( ) dB H( ) rad Design Parameters Pass Band Frequency Stop Band Frequency Pass Band Ripple Stop Band Attenuation p rad. S rad. 20log10 A/ B dB 20log10 A/ C dB H () A B C p S Computer Aided Design of FIR Filters Best Design tool for FIR Filters: the Equiripple algorithm. It minimizes the maximum error between the frequency responses of the ideal and actual filter. Step 1: define the desired filter with pairs of frequencies and values. For a Low Pass Filter: f=[0, f1, f2, 1]; A=[1, 1, 0, 0]; ωPASS π ω f 2 STOP π f 1 where Linear Interpolation 1 0 PASS STOP (rad) Computer Aided Design of FIR Filters Step 2: Choose filter length N from desired attenuation as Attenuation (dB) N~ 11 STOP PASS / Step 3: call “firpm” (pm = Parks, McClellan) h = firpm(N, f, A); which yields the desired impulse response h h[0], h[1] h[ N ] Example: Low Pass Filter Passband: 3kHz Stopband: 3.5kHz Attenuation: 60dB Sampling Freq: 15 kHz Then compute: 3,000 2 15,000 5 3,500 7 STOP 2 15,000 15 60 N 82 7 2 11 15 5 PASS 2 h = firpm(82,[0,2/5,7/15,1], [1,1,0,0]); Frequency Response freqz(h) Magnitude (dB) 50 0 50dB, not quite yet! -50 Increase N. -100 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 Phase (degrees) 0 -1000 -2000 -3000 -4000 Increase Filter Length N h=firpm(95, [0, 2/5, 7/15, 1], [1,1,0,0]); freqz(h) Magnitude (dB) 50 0 -50 -100 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 Phase (degrees) 0 -1000 -2000 -3000 -4000 Impulse Response of Example stem(h) 0.6 0.5 0.4 0.3 0.2 0.1 0 -0.1 0 10 20 30 40 50 60 70 80 90 100 Typical Applications In applications such as Radar, Sonar or Digital Communications we transmit a Pulse or a sequence of pulses. For example, we transmit a short sinusoidal burst: x(t ) A sin(2F0t ), 0 t T1 2 F0 500Hz 1.5 1 T1 8 m sec 0.5 0 -0.5 -1 -1.5 -2 0 1 2 3 4 sec 5 6 7 8 -3 x 10 Typical Applications For example in a communications signal we send “0” and “1”. 1 x(t ) 0 x(t ) For example let: 4 Transmitted: 3 2 1 0 -1 -2 -3 -4 0 1 0 0.005 0.01 1 0.015 millisec 0.02 0 0.025 0.03 Received Signal with Noise Suppose you receive the signal with 0dB SNR: 8 6 4 2 0 -2 -4 -6 -8 0 0.005 0.01 0.015 millisec 0.02 0.025 0.03 DTFT of Signal Magnitude of the DFT of the Pulse (in dB): 50 dB 40 30 20 10 0 -10 -20 -30 -40 -50 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 Hz DTFT of Signal Bandwidth of the Signal (take about -20dB from max) is about 1.0kHz 50 40 ~ 20dB 30 20 10 0 -10 -20 -30 -40 -50 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 Design the Low Pass Filter Pass 0 to 1kHz; Stop 1.5kHz to 10.0kHz N=81 Magnitude (dB) Attenuation 40dB 50 0 -50 -100 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 Normalized Frequency ( rad/sample) 0.9 1 0 Phase (degrees) Sampling Freq 20.0kHz -500 -1000 -1500 Filtered Data 10 5 Received signal: 0 -5 -10 0 5 10 15 20 25 30 35 millisec 4 2 … and Filtered: 0 -2 -4 0 5 10 15 20 millisec 25 30 35 4 3 Compare to the original: 2 1 0 -1 -2 -3 -4 0 0.005 0.01 0.015 millisec 0.02 0.025 0.03 40