Random Data 79 1.13 Digital Filters There are two fundamental types of digital filters Non-recursive N w n = c k v n k (1.226) k= N and recursive N M w n = c k v n k + d k w n k (1.227) k= N k=1 Clearly non-recursive filters are a special case of recursive filters where M=0. Non-recursive filters rely only on the values of the inputs, v, whichmayincludefuture values, particularly in the case where we are post-processing the data and hence have access to all values in the record. Recursive filters depend on the input values and previous values of the outputs as well, hence the name recursive. Examples Non-recursive
80 This filter is known as a smoothing by 5s filter. w n = 1 5 (v n 2 + v n 1 + v n + v n+1 + v n+2 ) (1.228) Recursive w n = w n 1 + 1 2 (v n + v n 1 ) (1.229) This is trapezoidal integration a good example of the breadth of digital filters which, although they have their roots in analog filters, are a much more general processing tool than simply modifying amplitude as a function of frequency in the tradition of lowpass, band-pass, and high-pass analog filters. Another example of a recursive filter is an exponential smoothing w n+1 = av n+1 +(1 a)w n 1 (0 <a<1) (1.230) 1.13.1 A Filter by Another Name Depending on the field, recursive and non-recursive filters go byoneofmanynames:
Random Data 81 Non-recursive Finite impulse response (FIR) filter Transversal filter Moving average (MA) filter Recursive Infinite impulse response (IIR) filter Ladder or lattice filter Auto-regressive moving average (ARMA) filter Auto-regressive integrated moving average (ARIMA) filter 1.13.2 Least Square Fitting of Polynomials The example non-recursive filter introduced above is probably familiar (or a variant of it). What exactly is this filter doing? Let s say we desire to smooth a signal by fitting adegreep polynomial to some M-point subset of the signal (which contains N points) and then replacing the central point of those being fitted with themeanvalueofthefit polynomial. Clearly we require N>M. For example, consider the simplest case, the fitting of a constant value through five points. Let t n be our time variable and then our polynomial is simply w n = A (1.231) Now, restricting ourselves to the case n = 2, 1, 0, 1, 2, we wish to minimize, in the least squares sense, f(a) where 2 f(a) = [v n A] 2 (1.232) n= 2
82 thus we differentiate with respect to A and set the result equal to 0. f(a) A = 2 n= 2 (v n A) =0 5A = 2 n= 2 v n (1.233) But recall that we simply want to replace v 0, our center value, by the mean of our polynomial, which is (1/5) 2 n= 2 v n = A. Hencewearedone,wereplacev 0 with w 0 = A or, generalizing, A = 1 5 Thus the impulse response of the filter is 2 n= 2 v n = w n (1.234) h = 1 [1 1 1 1 1] (1.235) 5 A logical question to ask is what is the frequency response function (transfer function) of this filter? Embedding an M point filter into a signal of length N =256andtaking the Fourier transform yields the following: H(f) 1 0.8 0.6 0.4 M=3 M=5 M=7 M=9 0.2 0 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 f/f s Clearly as M gets larger more spectral components are attenuated, and in particular, progressively lower frequencies are attenuated. Notice the ripples in the high frequency regions - clearly this is not a perfect low-pass filter.
Random Data 83 1.13.3 Filter Design - Spectral Approach Consider the following model filters Low-Pass High-Pass Notch Filter (Band Stop) Band Pass
84 It is often easiest to consider the desired filter in the frequency domain and then look at its impulse response function in the time domain. For example, the ideal filter is the rectangle function (or top-hat filter) in the spectral domain butweknowthatthisleads to the sinc function, and hence wiggles, in the temporal domain. Often this is acceptable and we can simply zero-out unwanted frequencies in the spectral domain keeping in mind that we will see ringing in the time domain. If this ringing is aconcernwecansmooth out the harsh edge, either by informally tweaking the coefficients or more formally by applying a smoothing function (say a smooth by 5s) to the rectangle filter. For example, consider the N =256pointrectanglefilterdefinedsuchthatthefirst16pointsare1and the next 112 points are zero (symmetry defines the rest). The inverse Fourier transform of this filter yields the impulse response function, h(t). Now take the same filter function and smooth it with a 7s filter (e.g., [ 1 1 1 1 1 1 1]/7). The results areshownbelow H(f) 1 0.8 0.6 0.4 0.2 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 f/f s
Random Data 85 1 0.8 0.6 h(t) 0.4 0.2 0 0.2 0.4 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 t/t The simple smoothing, which amounts to a linear transition over 7 points, strongly damps the oscillations in the impulse response function with only a modest effect on the attenuation of the spectral components around the filter s edge. 1.13.4 Filter Design - Temporal Approach We can write a general expression for digital filters of causal signals(signals that are zero for all negative times which is typically our case) in the time domainas w n = N M c k v n k + d j w n j (1.236) k=0 j=1 where M = 0 Non-recursive filter (FIR) M 0 Recursive filter (IIR) It can be shown that the transfer function corresponding to Eq 1.236 is H(f) = 1 N c k e i2πkfn t k=0 (1.237) M d j e i2πjfn t j=1
86 where t = T/(N 1) = 1/f s,thetimeincrement. Thusthetransferfunctioncan be represented as a rational fraction of two polynomials. In filter theory the roots of the numerator polynomial are known as the zeros of the filter and the roots of the denominator polynomial are known as the poles of the filter. 1.13.5 z-transform Looking at Eq 1.237 we see a lot of what look like Fourier coefficients. Defining and substituting into Eq 1.237 we have z = e i2πf t (1.238) H(z) = 1 N c k z k k=0 (1.239) M d j z j j=1 Now, if M =0Eq1.239reducestoapolynomialinz while if M 0wegetarational function in z. Inthelattercasethereistheissueofstability. Thisisbeyond the scope of our discussion but is covered in detail in any standard text ondigitalfilters. The z-transform behaves a lot like the Fourier transform. In particular the convolution in z space is N W (z) =H(z)V (z) w n = h n v n = h n k v k (1.240) k= N Thus H(z) isthetransferfunctionofthefilterandiscompletelydetermined by the ratio of two polynomials. Note, we can write (which is essentially the way Matlab defines the polynomial coefficients except the indices start at 1) H(z) = N b k z k k=0 (1.241) M a j z j j=0
Random Data 87 Our spectral approach via windowing in the frequency domain is a non-recursive approach. We can define a polynomial approximation to our window completely defining the transfer function. 1.13.6 Standard Recursive Filters (IIR) There are four fundamental types of Recursive filters 1. Butterworth 2. Chebychev I 3. Chebychev II 4. Elliptic Butterworth Filters The Butterworth filter is maximally flat in the pass band and stop band and is derived by minimizing the least square difference with respect to the ideal filter. The tradeoff is that there is no minimization of the transition band, therefore higher order (more costly) Butterworth filters are required to achieve steep transitions. The great advantage of the Butterworth filter is it is smooth - no ripples in either the stop or pass band.
88 Chebychev Type I Filters Chebychev Type I filters are maximally flat in the stop band (like the Butterworth filter) and minimize the absolute difference, relative to the ideal filter, in the pass band. This latter process is known as equiripple as the process of minimizing the absolute difference leads to ripples, potentially of near equal maximum amplitude. Chebychev Type II Filters Chebychev Type II filters are maximally flat in the pass band and equirippleinthestop band the opposite of the type I filter.
Random Data 89 Elliptical Filters Elliptical filters are equiripple in both the pass and stop band and minimize the transition width. The above three filters can be scene as special cases of Elliptic filters. 1.13.7 Phase Delay Amajorproblemwithdigitalfilterscanbephasedelay.E.g. 1 0.5 original signal forward forward and backward Amplitude 0 0.5 1 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 Frequency (Hz) Here I have applied a 1 st order Butterworth filter, in just the forward direction, and taking the output in the forward direction, flipping it (in Matlab this command is fliplr) and running the output a second time through the Butterworth filter (in Matlab this command is butter). As is apparent, the Butterworth filter introduces both phase delay and amplitude attenuation both of which are a function of frequency, as seen in the figure below:
90 Magnitude (db) Phase (degrees) 0 50 100 0 5 10 15 20 25 30 35 40 45 50 Frequency (Hz) 0 50 100 0 5 10 15 20 25 30 35 40 45 50 Frequency (Hz) In the above case I have used an original sample rate of 100 Hz, the sine wave is 1 Hz, and the filter is designed with a cutoff frequency at 2 Hz. There are design solutions to minimize phase delay with a single pass on the data, which might be necessary in real-time applications, but these generally sacrifice something to achieve smaller phase delay, generally the amplitude response. When the data to be filtered is already in hand there is an easy solution - run the data through the filter twice, once forward and once backward. Recall that in the frequency domain we have directional ambiguity hence the forward and backward frequencies look the same. Thus the possibly frequency dependent lag induced in the forward direction is completely undone in the backward direction. The resultant transfer function is given by H(f) effective = H 2 (1.242) The Matlab code for the above plot is: fs=100; t=0:1/fs:4.05; y=sin(2*pi*t); [b,a] = butter(1,2/fs); y f=filter(b,a,y); y ff=fliplr(filter(b,a,fliplr(y f)));
Random Data 91 plot(t,y, r,t,y f, b-.,t,y ff, g-- ) legend( original signal, forward, forward and backward ) xlabel( Time (s) ) ylabel( Amplitude ) 1.14 The Hilbert Transform The Hilbert transform is a transform from the time domain to the time domain. It is a powerful tool for us as it can be used to extract the instantaneous amplitude, phase, and frequency of a signal. For further discussions on the Hilbert transformseebracewellor Bendat and Piersol. The time domain definition of the Hilbert transform is x(t) =H[x(t)] = x(u) du (1.243) π(t u) where x(t) is the Hilbert transform of x(t) and H is our nomenclature for the Hilbert transform of. We recognize Eq 1.243 as a convolution integral. Thus we can write x(t) =x(t) 1 πt (1.244) We can begin to get an interpretation of the Hilbert transform by considering it in the spectral domain. X(f) =F[ x(t)] (1.245) Note that X(f) isthefouriertransformof x(t) andnotthehilberttransformofx(t) whichisdifferent.now,recallingthatinthespectraldomain the convolution is just a multiplication we need F [ ] 1 = i sgn f = πt i f > 0 0 f =0 i f < 0 (1.246)
92 where sgn is the signum function which returns the sign of its argument if the argument is non-zero and zero if the argument is zero. Therefore we have X(f) = i sgn fx(f) (1.247) Now, let s consider B(f) = i sgn f = e iπ/2 f>0 0 f =0 e iπ/2 f<0 (1.248) Thus we can write B(f) = B(f) e iφ b(f) (1.249) where B(f) =1 f 0andφ b is defined such that π/2 f>0 φ b = π/2 f<0 (1.250) which looks like Now, let X(f) = X(f) e iφx(f) (1.251) Therefore Thus X(f) = X(f) e i φ x(f) = X(f) e i[φx(f)+φ b(f)] (1.252) X(f) = X(f) (1.253) φ x (f)+π/2 f>0 φ x (f) (1.254) φ x (f) π/2 f<0
Random Data 93 Hence we see that for positive frequencies the Hilbert transform simply applies a frequency shift of 90 whereas for negative frequencies a frequency shift of -90 is applied. How do we extract the instantaneous information? From the analytic signal, defined as z(t) =x(t)+i x(t) (1.255) which we can write as z(t) =A(t)e iθ(t) (1.256) where A(t) is the instantaneous envelope signal of x(t) and θ(t) is the instantaneous phase angle of x(t), which are defined as A(t) = [ x 2 (t)+ x 2 (t) ] 1/2 = z(t) (1.257) [ ] x(t) θ(t) = tan 1 =2πft (1.258) x(t) respectively. Note that the instantaneous phase angle must be unwrapped, e.g., 2π must be added each time the signal wraps back around on itself. Now, we can find the instantaneous frequency from Eq 1.258 θ t =2πf f = 1 θ 2π t (1.259) where f is the instantaneous frequency. Example Consider a simple cosine wave, x =cos(2π 60 t) hencethefrequencyis60. Thesignal and its Hilbert transform (real z and imag z, respectively)looklike The amplitude function looks like
94 x(t) and H[x(t)] 1 0.5 0 0.5 x(t) H[x(t)] 1 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 t 1.05 1 0.95 A(t) 0.9 0.85 0.8 0.75 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 t and the unwrapped phase is 40 30 θ (radians) 20 10 0 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 t Differentiating the unwrapped phase yields the instantaneous frequency
Random Data 95 f 70 65 60 55 50 0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 60.2 60.1 f 60 59.9 59.8 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 t Clearly there are transients at the beginning of the signals but these quickly fade away.
96 For reference the Matlab code to develop these signals is t = (0:1/1023:1); x = cos(2*pi*60*t); y = hilbert(x); plot(t(1:150),real(y(1:150))), hold on plot(t(1:150),imag(y(1:150)), : ), hold off A=abs(y); phi=(unwrap(2*atan(imag(y)./real(y))))/2; plot(t(1:150),a(1:150)) plot(t(1:150),phi(1:150)) f=1/(2*pi)*diff(phi)/(t(2)-t(1)); plot(t(1:1024/2),f(1:1024/2)) Note that the Matlab call hilbert( ) returns the analytic signal, z(t), not the Hilbert transform. The Hilbert transform is obtained as the imaginary part of the result returned by hilbert( ).