Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)1 BME 452 Biomedical Signal Processing Lecture 5 Digital filtering
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)2 Lecture 5 Outline In this lecture, we’ll study digital filtering methods General considerations and filter specifications Filtering in frequency domain Filtering in time domain Sum and difference (SD) filter Finite Impulse Response (FIR) filter Infinite Impulse Response (IIR) filter using MATLAB
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)3 General considerations As mentioned in earlier lectures, filtering is the process of keeping components of the signal with certain desired frequencies and removing components of the signal with certain undesired frequencies Very often, we keep the gain of the required frequency components to 1 or close to 1 And the gain of the undesired frequency components will be 0 or close to 0 In general, there are 4 types of filtering: LPF, HPF, BPF, BSF
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)4 Filter specifications Passband the range of frequency components that are allowed to pass Stopband the range of frequency components that are suppressed Passband ripple ripples in the passband the maximum amount by which attenuation in the passband may deviate from gain (which is normally 1) Stopband ripple Ripples in the stopband The maximum amount by which attenuation in the stopband may deviate from gain (which is normally 0) Stopband attenuation the minimum amount by which frequency components in the stopband are attenuated Transition band The band between the passband and the stopband
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)5 Ideal filter and edge frequencies Frequency response of ideal filters The edge frequencies are the end frequencies of passband or stopband fc= cut-off frequency LPF: passband: 0 f fc Stopband: fc f fs/2 HPF: passband: fc f fs/2 Stopband: 0 f fc BPF: passband: fc1 f fc2 Stopband: 0 f fc1 and fc2 f fs/2 BSF: passband: 0 f fc1 and fc2 f fs/2 Stopband: fc1 f fc2
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)6 Actual LPF LPF Passes all low- frequency components below fp and blocks all higher frequency components above fs In reality, you can’t design ‘square’ type of filters So, there needs to be transition betweens the bands LPF: passband: 0f fp Stopband: fsf fs/2 Transition band: fp<f <fs
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)7 LPF example Low-pass filter (LPF) Eg.: Consider a combination of 3 sinusoidal signals - 2 Hz, 5 Hz and 11 Hz. The final output signals after 2 LPF are shown LPF at f p =3 Hz and f s =4 Hz LPF at f p =8 Hz and f s =9 Hz Combined signal LPF, f p =8 Hz, f s =9 Hz Only 2 Hz signal remains Only 2 Hz and 5 Hz signals remain LPF, f p =3 Hz, f s =4 Hz 2 Hz signal5 Hz signal11 Hz signal
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)8 HPF Passes all high-frequency components above fp and blocks all higher frequency components below fs Eg.: Consider the same combination of 3 sinusoidal signals, 2 Hz, 5 Hz and 11 Hz. The final output signals after 2 HPF are shown HPF at fs=3 Hz and fp=4 Hz HPF at fs=8 Hz and fp=9 Hz Combined signal Only 5 Hz and 11 Hz signals remain Only 11 Hz signal remains HPF, f s =3 Hz, f p =4 Hz HPF, f s =8 Hz, f p = 9 Hz From this point onwards, we will use Fs for sampling frequency
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)9 BPF Passes all frequency components between edge passband frequencies, f p1 f s2 Eg.: Consider the same combination of 3 sinusoidal signals, 2 Hz, 5 Hz and 11 Hz. The final output signal after BPF at f p1 =4 Hz, f p2 =6 Hz, f s1 =3 Hz, f s2 =7 Hz is shown Combined signal BPF Only 5 Hz signal remains
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)10 BSF Band-stop filter (BSF) Passes all frequency components lower and higher than edge passband frequencies, freq (allow) f p2 and blocks all frequencies between f s1 <freq (block) <f s2 Eg.: Consider the same combination of 3 sinusoidal signals, 2 Hz, 5 Hz and 11 Hz. The final output signal after BSF at f p1 =4 Hz, f p2 =6 Hz, f s1 =3 Hz, f s2 =7 Hz is shown Combined signal BSF 5 Hz signal is filtered out, only 2 Hz and 11 Hz signals remain
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)11 Direct filtering (in frequency domain) A simple method of doing this Obtain the DFT of the signal (from 0 to fs) Set to zero the values that are not in the required frequency range i.e. apply a RECTANGULAR window Compute the Inverse Discrete Fourier Transform (IDFT) Example: f1=8 Hz and f2=25 Hz with N=100, fs=200 Hz Say, we wish to design a LPF with fp=10 Hz and fs=12 Hz Compute y=fft(x) in MATLAB Set the values y(7:95)=0 WHY? This is important! Compute yf=ifft(y,’symmetric’) And you get the low pass filtered signal! What is the main disadvantage of this technique? High computation and time Another disadvantage?? += In MATLAB, you have to force conjugate symmetry, else you will get complex values due round-off errors in doing FFT and IFFT Low pass filtered signal
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)12 Direct filtering (in frequency domain) –cont. How it works: We can also use a different window say Hanning window, instead of rectangular window to obtain a smoother filtered output H=hanning(12); Hf(1:6) =H(7:12); H(7:95)=0; Hf(96:100)=H(2:6); y=fft(x); yy=y.*Hf; yf=ifft(yy,'symmetric'); plot(yf); Hanning window
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)13 Filtering in time domain Two disadvantages of direct filtering computational time and complexity is high there is distortion To solve, we should filter in time domain There are many types of time domain filtering methods We will look at Simple FIR filters IIR filters using MATLAB codes
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)14 Filtering in time domain (in equation form) The output from a IIR digital filter in made up of previous inputs and previous outputs as well where B and A are the filter coefficients and the operation * is convolution Convolution in time domain is equivalent to multiplying in frequency domain –do you remember that we did some window multiplication for direct filtering Convolution operation will not be discussed in this course The output from a FIR digital filter in made up of previous inputs only, so no feedback
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)15 Simple low pass FIR filter –sum filter Consider, y[n]=x[n]+x[n-1] for every data in the signal This filter is also known as the sum filter This simple addition acts as a LPF! This can be proved by using z=transform but is not needed for the purpose of this course For hardware design, the block diagram would look like Advantages: You just need one adder and one delay circuit – simple and cost effective (i.e. cheap) The filter coefficients are integer values (in this case they are 1), so no round-off errors It is an FIR filter so it is stable – why are FIR filters stable? No feedback Disadvantage: Not very good LPF (see next slide)
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)16 Sum filter (cont.) The frequency response of the sum filter: not very good as it is far from the ideal ‘square’ filter The gain at freq=0 is 1 The stopband frequency (when gain=0) is at rad/sample or at Fs/2 Hz So, there is no stopband So how to define the passband frequency and transition bands? For these cases, we use the 3 dB cut-off as the passband frequency Figure from S.K.Mitra, DSP 3e Magnitude is also known as gain
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)17 3 dB cut-off frequency The 3 dB cut-off frequency is defined as the frequency when the gain drops 3 dB from maximum gain of 1, which is assumed to be 0 dB Gain=1, 20log 10 (1)=0 dB When energy is half, i.e. gain=(1/2) 0.5 =0.7071, we have 20log 10 (0.7071)=-3 dB From the figure We can see that the 3 dB cut-off frequency (when gain=0.7071) is approximately 0.5 rad/sample or Fs/4 Hz This is the passband frequency So, the passband is from 0 to Fs/4 Hz And transition band is from Fs/4 to Fs/2
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)18 Increasing the order of sum filter For order 1, we had y[n]=x[n]+x[n-1] Now assume, this is fed to another same filter in cascade connection Now, we have z[n]=y[n]+y[n-1] Solving, we have z[n]=y[n]+y[n-1] =x[n]+x[n-1]+x[n-1]+x[n-2] =x[n]+2x[n-1]+x[n-2] Verify this using x[1]=3, x[2]=2, x[3]=5 Hint: Compute y[2] and y[3] Compute z[3] using single cascaded filter and two filters and compare You will notice that z[n] will be defined only for n=3 onwards if x[1] is the starting point, i.e. for every order M, you lose M initial data points in filtering Likewise y[n] is defined only from y[2] onwards.
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)19 Increasing the order of sum filter (cont.) So for order M, we have The frequency response for M=3 is given => The passband is about rad or Fs/6 We can see that with increasing order, the passband is becoming smaller without any change in stopband Also, the curve is becoming closer to the ideal ‘square’ So, we can increase/decrease M depending on our needs The 3-dB cut-off frequency is given by fp= cos -1 (2 -1/(2*M) ) *2/pi where Figure from S.K.Mitra, DSP 3e What would be y[n] for N=3?
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)20 Simple high pass FIR filter Similarly, a HPF can be designed using y[n]=x[n]-x[n-1] For hardware design, the block diagram would look like The stopband frequency (when gain=0) is at 0 Hz, i.e. there is no stopband The gain at freq=Fs/2 Hz or rad is 1 The passband frequency is at 0.5 rad or Fs/4 Hz (using 3 dB cut-off) Passband width is from Fs/4 to Fs/2 Hz The orders, N can be increased to obtain a smaller passband width and to obtain a frequency response closer to the ideal ‘square’ filter The 3-dB cut-off frequency is given by fp= sin -1 (2 -1/(2*N) ) *2/pi For order N, we have As homework, try y[n] for N=3? Figure from S.K.Mitra, DSP 3e This is also known as a difference filter
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)21 Simple HPF FIR filter example HPF can be used to remove mean values and low frequency polynomial trends, i.e. to detrend the data As an example, we saw the passenger data plot in Lecture 4 The detrending can be simply done by using, y[n]=x[n]-x[n-1], where x[n] is the data Figures from R.Shiavi, Introduction to applied statistical signal analysis
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)22 Simple band pass FIR filter Similarly, a BPF can be designed using a combination of LPF and HPF This is known as sum and difference (SD) filter Different orders, M and N can be chosen to obtain the required frequency response Where Gain cf is the gain at centre frequency given by Example (As homework, verify these later on your own) For filter orders of M=28 and N=8 gives The centre frequency is 40 Hz when fs=256 Hz Approximate 3 dB bandwidth from 32 to 48 Hz (rounded to the nearest integer) The gain amplification at 40 Hz is approximately
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)23 Simple band pass FIR filter (cont.) Let us compute a band pass FIR filter equation: For orders, LPF, M=4 and HPF, N=1, obtain the band pass FIR equation that expresses z[n] in terms of x[n] and delays of x[n] Solution (can you get the answer?) z[n]=x[n]+3x[n-1]+2x[n-2]-2x[n-3]-3x[n-4]-x[n-5]
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)24 IIR filter The problem with FIR filter is that you do not get ‘square’ passband like in ideal filters unless you use a very high order To solve this problem, we can use IIR filter The disadvantages of IIR filters are The are not stable (due to feedback) The filter coefficients are not normally integer, so can have round-off errors Hardware design is more complicated Most importantly, their phase response is not linear
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)25 Phase response? In you refresh your memory, you’ll know that frequency responses have two parts: magnitude and phase responses In the diagrams, the magnitude and phase responses of Butterworth and Elliptic IIR filters are shown You can see that the gains are relatively stable at 1 for the bandpass range of 0.2 to 0.6 But the phase response is not linear, i.e. not a straight line In MATLAB, this problem is solved by filtering twice, once forward and once reverse By doing so, the magnitude response is squared while the phase response becomes zero Butterworth Elliptic
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)26 IIR filter design It is not possible to design IIR filters digitally So, the approach is to design analogue IIR filters, then use a bilinear method to transform the filter to digital This ‘bilinear’ method will not be discussed in this course as it requires z –transofrm knowledge So, we will use MATLAB functions directly There many types of IIR filters Butterworth Elliptic (Cauer) Chebyshev I Chebyshev II Bessel But we will look at two only: Butterworth and Elliptic filters Because Butterworth filter gives flat magnitude responses in the passband and stopband (i.e. no ripples or very little ripple) while Elliptic filter requires the lowest order among all the IIR filters
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)27 Butterworth IIR filter Remember the IIR filter equation We have to determine the orders M and N and then determine the coefficients A and B Very often, we use M=N In MATLAB, we can find the required minimum order for our specification using buttord (Wp, Ws, Rp, Rs) where Wp is the passband edge frequency, Ws is the stopband edge frequency, Rp is maximum ripple in passband and Rs is the minimum attenuation in stopband Rp and Rs will be in dB, while Ws and Wp will be in normalised radian/sample Low pass Butterworth filter with different orders Figure from S.K.Mitra, DSP 3e
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)28 Butterworth IIR filter (cont.) After finding the order using buttord function, we have to find out the filter coefficients B and A using [B,A]=butter (N,Wp) where N is the filter order chosen earlier Finally to actually do the filtering, we use filtfilt (B,A,x) where x is the signal to be filtered Filtfilt function filter the signal twice and uses convolution operations Example: Design a lowpass filter with Fs=200 Hz Passband = 0 to 40 Hz Passband ripple = less than 3 dB Stopband = 50 Hz to the Fs/2 Stopband attenuation = at least 30 dB Plot the filter's frequency response – use freqz (B,A) function for this purpose.
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)29 Butterworth IIR filter example - solution Fs=200 Hz Rp=3 and Rs=30 Wp=40/100 Ws=50/100 Use buttord (Wp, Ws, Rp, Rs) N=buttord(40/100, 50/100, 3, 30) which gives N=11 Next obtain the coefficients, B and A using [B,A]=butter (11, 40/100) which gives B = A = Using freqz(B,A), we get
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)30 Butterworth IIR filter example applied to reduce 50 Hz noise from ECG Assume we the use the designed Butterworth filter to reduce 50 Hz noise from the ECG below plot(ecg); ecgf=filtfilt(B,A,ecg); plot(ecgf);
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)31 Elliptic filter The same approach could be used for designing an Elliptic filter. The functions are N=ellipord(Wp, Ws, Rp, Rs) [B,A] = ellip(N,Rp,Rs,Wp) The advantage of Elliptic filter as compared to Butterworth filter is that for the same specification, we require a lower order but there are ripples in the passband that is not so evident for Butterworth filter For the bandpass filter example, we need only order 3 for Elliptic filter but have to use order 8 for Butterworth filter As a homework, try the other IIR filter functions – chebyshev I, chebyshev II, etc. using MATLAB help, if necessary
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)32 Advantages and disadvantages of FIR/IIR Let’s sum up the advantages and disadvantages of FIR/IIR filters FIR advantages over IIR Stability (as there is no feedback) Linear phase response Simpler hardware design Desirable numerical properties (less round off/finite precision problem) Can be designed using fractional arithmetic (coefficients are either integers or less than 1.0) FIR disadvantages over IIR: Requires higher order (hence more memory, computation time) Though there are more advantages in using FIR, very often we use IIR as we have powerful computers and software like MATLAB, which solve most of the disadvantages
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013)33 Study guide (Lecture 5) From this week’s lecture, you should know how to perform filtering using Direct filtering in frequency domain Sum and difference (SD) FIR filtering IIR filtering using MATLAB End of lecture 5