Digital Filtering
Digital Filtering
Digital Filtering
Instead of filtering continuous data, as we did in the previous chapter, we are now looking to filter data that is sampled in time. The digital filters we design can be implemented in hardware or can be programmed into a computer. There are two broad classes of digital filters, the finite impulse response (FIR) and the infinite impulse response (IIR). The FIR filters utilize convolution of the input signal with a finite number of points to get the output. The IIR filters calculate the output using the input data and previous output points. We will begin with the development of IIR filters. There are also two different approaches to developing a digital filter. One approach is to develop a low pass digital filter at one specific frequency and use this as the bootstrap for the design of other types of filters at other frequencies, similar to what we did in chapter 8. However, the most common is to develop an analog filter and then convert it to a digital filter. This is the approach we will take. Converting an analog filter fundamentally means taking a transfer function from the Laplace or Fourier domain and moving it to the Z domain. Once again, there are different approaches. We will start with an approach that seems the most intuitive, i.e., converting individual Laplace terms to individual z domain terms. 9.1. Impulse Invariant Method. We begin looking for a method to develop IIR filters from s domain filters. We will start with a definition, and then a theorem illustrating the importance of this definition in converting analog transfer functions to the z domain transfer functions. For the sake of clarity, in this section the Laplace domain filter is written as G ( s ) and the corresponding z domain filter is written as H ( z ) . -------------------------------------------------------------------------------------------------------------Definition 9.1.1. A continuous filter G ( s ) and a digital filter H ( z ) are said to be impulse-invariant if the impulse response of H ( z ) is the same as the sampled impulse response of G ( s ) . -------------------------------------------------------------------------------------------------------------Theorem 9.1.1. If H ( z ) is the z-transform of the samples g ( kT ) of a continuous function g ( t ) whose Laplace transform is G ( s ) , then H ( z ) and G ( s ) are impulse invariant. Proof 9.1 The Impulse Invariant Method 1 11/12/2011
G (s) =
i =1
Ri . s si
(9.1.1)
The si could be complex conjugate pairs, but that is not important for our discussion. The inverse Laplace of G ( s ) is
g ( t ) = Ri e sit .
i =1 N
(9.1.2)
The values of the samples of the sampled impulse response are obtained by setting t equal to kT, g ( kT ) = Ri e si kT .
i =1 N
(9.1.3)
i =1
Ri . 1 eTsi z 1
(9.1.4)
which is the same as Eq. (9.1.3). -------------------------------------------------------------------------------------------------------------Example 9.1.1. Find the z-domain transfer function for the following continuous function assuming the sampling rate is T = 0.01 sec 10 . (9.1.5) G (s) = s + 10 Solution The Bode plot of G(s) is given in Fig. (9.1.1). From Table 9.1.1, the corresponding timedomain function is
g ( t ) = 10e 10t u ( t ) .
The sampled version of this function is
h [ k ] = 10e 10 kT u [ k ] = 10e 0.1k u [ k ] .
11/12/2011
Figure 9.1.1. Bode plot of Eq. (9.1.5) In Fig. 9.1.1, the zero-frequency, or dc values, correspond to s = 0 and a magnitude of = sT H ( s ) = 1 . Corresponding to s= 0, z e= 1 . Therefore, we would like
s =0
H (1) = 1 .
At a frequency of zero, z = 1, so
H ( z) = 10 10 = = 105.3 . 0.1 1 1 e z 1 0.905
Therefore, we use
1 10 z 0.095 z . (9.1.6) = 105.3 z 0.905 z 0.905 The Bode plot of Eq. (9.1.6) is shown in Fig. (9.1.2). Actually, the factor of about 0.01 that we added to Eq. (9.1.6) is due to the fact that we sampled at 0.01 seconds. H ( z) =
11/12/2011
Figure 9.1.2. Bode plot of Eq. (9.1.6) The continuous frequency at = 10 corresponds to = T 10 ( 0.01) 0.1 rad 5.7 o . = = c = If we change to a logarithmic plot and zero in on the angle of interest, we get the Bode plot of Fig. 9.1.3 showing that at 5.7 o the attenuation is -3db and the phase shift is 43o , similar to the corner frequency of Eq. (9.1.5)
11/12/2011
Figure 9.1.3. Bode plot of Eq. (9.1.6) showing the behavior near the corner frequency. Note that the angle corresponding to = = = = 10 rad / s in Fig. T 10 ( 0.01) 0.1rad 5.7o -------------------------------------------------------------------------------------------------------------Example 9.1.2. Find the z-domain transfer function for the following continuous function assuming the sampling rate is T = 0.01 sec. 15 . (9.1.7) G (s) = 2 s + 8s + 15 The Bode plot of Eq. (9.1.7) appears in Fig. 9.1.4. Solution Partial fraction expansion gives us the two real poles 15 7.5 7.5 G= 2 = + (s) s + 8s + 15 s + 3 s + 5 The corresponding terms in the z domain are 7.5 7.5 7.5 7.5 + = + H ( z) = 0.03 1 0.05 1 1 1 e z 1 e z 1 0.970 z 1 0.951z 1 ( 0.95 0.97 ) z 1 0.2 z = 7.5 = 2 1 2 1 1.921z + 0.922 z z 1.921z + 0.922
Figure 9.1.4. Bode plot of Eq. (9.1.7) At z = 1 9.1 The Impulse Invariant Method 5 11/12/2011
H (1) =
so we take
0.001z . (9.1.8) z 1.921z + 0.922 The Bode plot appears in Fig. (9.1.5). The point = 10 rad / s corresponds to the H ( z) =
2
angle T 10 rad / s (= 0.1 rad 5.7 o . Note that the amplitude of the z-domain = 0.01s ) = Bode plot is not in complete agreement with that of the sdomain.
Figure 9.1.5. Bode plot of Eq. (9.1.8). The point = 10 rad / s corresponds to the angle T 10 rad / s (= 0.1 rad 5.7 o . = 0.01s ) = --------------------------------------------------------------------------------------------------------Let us take a closer look at the term we added to get the correct magnitude of H(z) in the previous example. In Eq. (9.1.7), we want the magnitude to be one at low frequencies, i.e, as s 0 , = G ( s ) s =0 However, in the Z domain
sT = e= 1 . z s =0
15 15 = = 1. s + 8s + 15 s =0 15
2
This is the reason we take the limit as z goes to one to scale the H(z) function. Notice that we had to scale the H(z) function by a factor of 100, or 1/ T . To get a feel for the this,
11/12/2011
consider the following. Suppose we sample a time-domain function g(t) at time intervals of T seconds.
g (t ) h [k ] T
k =0 N
The Laplace transform of the left side is G(s); the Z transform of the right side is H ( z ) T --------------------------------------------------------------------------------------------------------Example 9.1.3. A second order low-pass Butterworth filter with a cutoff frequency of 1 rad/s is given by 1 . (9.1.9) G (s) = 2 s + 1.41s + 1 Convert this to a z-domain transfer function assuming a sampling time of T = 0.1 second. Solution We can complete the square and write 1 = = G (s) 2 s + 1.41s + 1 which can be written as
G ( s ) = 1.41 0.707
( s + 0.707 ) + ( 0.707 )
2
( s + 0.707 ) + ( 0.707 )
2
If we look at Table 9.1, we see that the corresponding time domain function is
h ( t ) = 1.41e t sin (t ) u ( t )
where 0.707, 0.707 . {But we already knew that because we knew the poles of the = = Butterworth filter were= 0.707 j 0.707 .} at s If we now choose to sample this at T = 0.1 sec, we obtain the discrete time-domain function h [ k ] = 1.41( e T ) sin (Tk ) u [ k ] ,
k
Note that = H (1) So I will use 9.1 The Impulse Invariant Method 7 11/12/2011 1 1 . = 1 1.858 + 0.869 0.011
H ( z) =
(9.1.10)
Figure 9.1.6. Bode plot of Eq. (9.1.10). Note that at 5.7 o the magnitude goes through -3dB and the phase is -90 degrees, as it should be. --------------------------------------------------------------------------------------------------------Example 9.1.4. Find the corresponding z domain function for the following s domain transfer function. The sampling time is 0.1 sec. G (s) = Solution s + 0.5 s + s + 1.25
2
(9.1.11)
11/12/2011
s + 0.5 . s + s + 1.25
2
G (= s)
( s + 0.5)= s + , = 2 2 ( s + 0.5) + 1 ( s + ) + 2
= 1, 0.5
z 2 e T cos (T ) z H ( z) = 2 z 2e T cos (T ) z + e 2T
0.1( z 2 cos (T ) e T z )
= 0.1, T 0.05 T =
H ( z) =
z 2 2e T cos (T ) z + e 2T
(9.1.12)
11/12/2011
z 2 2e T cos (T ) z + e 2T
0.1( z 2 cos (T ) e T z )
-----------------------------------------------------------------------------------------------------------Example 9.1.5. Find the corresponding z domain function for the following s domain transfer function. The sampling time is 0.1 sec.
G ( s ) = 0.2 s+5 s + s + 1.25
2
(9.1.13)
Solution
(9.1.14)
10
11/12/2011
Figure 9.1.9. Bode plots of Eq. (9.1.13) and eq. (9.1.4) Let us see if we get the same result starting with the Sine-Cosine method:
G ( s ) = 0.2 s+5 s + s + 1.25
2
11
11/12/2011
i.
p = +i 0.5 0.1, 1 = =
ii.
= k
= g (t ) iii. =
Solution The Bode plot of G(s) is shown in Fig. 9.1.10a. Using the MATLAB command roots([1 7 20 50 ]),I see that the roots are at p1 =5, so the partial fraction expansion is
G= (s) s +1 A M = + s + 7 s + 20 s + 50 s + 5 ( s p ) ( s p* )
3 2
p =1 + i3,
p* =1 i3,
A=
s +1 4 = = 0.267 s + 2 s + 10 s = 5 25 10 + 10
2
s +1 k= ( s + 5 ) ( s p* )
s= p
I will sample at T =0.02 sec. The corresponding Z domain function is 9.1 The Impulse Invariant Method 12 11/12/2011
H ( z) =
z 2 e 0.02 cos ( 0.06 ) z 0.267 z 2 z e .1 z 2e 0.02 cos ( 0.06 ) + e 0.04 e 0.02 sin ( 0.06 ) z + 0.3 2 z 2e 0.02 cos ( 0.06 ) + e 0.04
(9.1.16)
2 0.02 cos ( 0.06 ) z + 0.3e 0.02 sin ( 0.06 ) z 0.267 z z + e + z e .1 z 2 2e 0.02 cos ( 0.06 ) + e 0.04
Figure 9.1.10a
13
11/12/2011
Figure 9.1.10b Figure 9.1.10. Bode plots of Eq. (9.1.15) and Eq. (9.1.16) ----------------------------------------------------------------------------------------------------------Example 9.1.7. Given the following Laplace domain transfer function, find the corresponding Z domain transfer function if the system is sampled at T = 0.1 msec. G (s) = Solution Equation (9.1.17) is the same as Eq. (9.1.7), except that it has been scaled up to kilaradians per second. Similarly, the sampling rate has been dropped down to milliseconds. Therefore, when this is in the Z domain, the math will be exactly the same as example (9.1.2) 15 106 . s 2 + 8 103 s + 15 106 (9.1.17)
----------------------------------------------------------------------------------------------------------Simulating the response of a digital filter Look back at the transfer function in Eq. (9.1.6). 9.1 The Impulse Invariant Method 14 11/12/2011
H ( z) =
Y ( z ) (1 0.905 z 1 ) = ) , 0.095 X ( z
or = 0.905 z 1Y ( z ) + 0.095 X ( z ) . Y ( z) It is a simple matter to go from the z domain to the sampled time domain
= 0.905 y [ k 1] + 0.095 x [ k ] . y [k ]
Notice how easily this can be implemented into computer code. y(1) = 0 for k=2:NN y(k) = 0.905*y(k-1) + 0.095*x(k) end Suppose we begin with an input signal of
(9.1.18)
(9.1.19)
= 10 Hz , 2 100 Hz , and it is sampled at Ts = 1 m sec , as shown in the top plot 1 = below. We will further suppose that the signal at 2 is distortion that we want to filter out. To low pass filter the 2 frequency, we might choose a corner frequency of 10 rad/s. This removes most of, but not all, of 2 .
15
11/12/2011
Figure 9.1.9. (top) The input signal, Eq. (9.1.14); (bottom) The output y[k] after the input is low pass filtered by a single pole filter with a corner frequency of 10 rad/s. What hardware might be able to implement the above LPF? An example is shown in Fig. 9.1.10.
Figure 9.1.10. Diagram of a digital filter to implement the filter of Eq. (9.1.6). References 1. E. P. Cunningham, Digital Filtering: an Introduction, John Wiley, NY, 1995. Exercises 9.1.1. Write a computer code to implement the transfer function of Eq. (9.1.8). 9.1.2. Design a hardware implementation of Eq. (9.1.8). 9.1.3. Find the z domain transfer function for a 2nd order, monotonic band pass filter with a pass band between 500 and 550 rad/s. The sampling time is 0.1 msec.
% My_zbode.m clear all phi = (0:0.01:2*pi); NN = length(phi); H = zeros(1,NN); % Find H(z) for n=1:NN z = exp(j*phi(n)); % determine z for this angle H(n) = .095*z/(z - .905); end subplot(2,1,1) plot((180/pi)*phi,abs(H),'k') semilogx((180/pi)*phi,20*log10(abs(H)),'k')
16
11/12/2011
%axis( [0 360 0 1 ]) axis( [ 1 100 -20 0 ]) set(gca,'XTick',[ 1 5.7 10 100 ]) grid on set(gca,'fontsize',14) title('My-zbode') ylabel('|H| dB') %xlabel('\theta = \omega x T_n ') aaa = angle(H); for n=1:NN if aaa(n) > 180 aaa(n) = aaa(n) - 2*pi; end end subplot(2,1,2) plot((180/pi)*phi,(180/pi)*aaa,'k') semilogx((180/pi)*phi,(180/pi)*aaa,'k') %plot(phi,(180/pi)*aaa,'k') %axis( [0 360 -90 90 ]) axis( [1 50 -90 0 ]) set(gca,'XTick',[ 1 5.7 10 100 ]) grid on xlabel('\theta = \omega x dt (degrees)') set(gca,'fontsize',14) ylabel('/H (degrees)') saveas(gcf,'plot.png')
17
11/12/2011