Professional Documents
Culture Documents
BME 4 52 Bio Medi Cal Signal Processing
BME 4 52 Bio Medi Cal Signal Processing
Processing
Lecture 5
Digital filtering
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 1
Lecture 5 Outline
In this lecture, we’ll study digital filtering
methods
General considerations and filter specifications
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 2
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
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 3
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) 4
Ideal filter and edge frequencies
The edge frequencies are the end
frequencies of passband or
Gain Gain stopband
LPF HPF fc= cut-off frequency
1 1
LPF: passband: 0 f fc
freq
fc
freq fc Stopband: fc f fs/2
fs/2
Last point in
Gain freq axis
Gain HPF: passband: fc f fs/2
BSF
BPF 1
1
Stopband: 0 f fc
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 5
Actual LPF
LPF
Passes all low-
frequency
components below
fp and blocks all
higher frequency
components above
fs
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 6
LPF example
Low-pass filter (LPF) 1.5 1.5 1.5
Hz. 0 0 0
-1 -1 -1
are shown
2 Hz signal 5 Hz signal 11 Hz signal
1.5
0.5
0
3
-0.5
2
1
-1
Only 2 Hz signal remains
-1.5
0 0 200 400 600 800 1000
-1
-2
3
-3
2
0 200 400 600 800 1000
1
Combined signal 0
-3
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 7
HPF
Passes all high-frequency Gain
components above fp and Passband
blocks all higher frequency
components below fs
1 Passband
ripple
Eg.: Consider the same combination Stopband
of 3 sinusoidal signals, 2 Hz, 5 Hz and attenuation
11 Hz. Stopband
2
1.5
1
1
0
0.5
-1
-2 0
-3 -0.5
Only 11 Hz signal remains
0 200 400 600 800 1000
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 8
BPF
Passes all frequency components between
edge passband frequencies,
fp1<freq(allow)<fp2 and blocks all frequencies
below and above edge stopband Gain
Passband
frequencies, freq(block)<fs1; freq(block)>fs2
1
Eg.: Consider the same combination Passband
ripple
of 3 sinusoidal signals, 2 Hz, 5 Hz and
11 Hz. Stopband Stopband
1.5
3
1
2
0.5
1
BPF 0
0
-1 -0.5
-2 -1
-3
-1.5
0 200 400 600 800 1000
0 200 400 600 800 1000
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 9
BSF
Band-stop filter (BSF)
Passes all frequency components
lower and higher than edge passband Gain
frequencies, freq(allow)<fp1; freq(allow)>fp2
and blocks all frequencies between
fs1<freq(block)<fs2 1
Passband Passband
ripple ripple
1
2
1 0.5
0 BSF 0
-1
-0.5
-2
-1
-3
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 10
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
1 1 2
Gain
0.8 0.8
1.5 LPF
0.6 0.6
1
0.4 0.4
+ =
0.5
0.2 0.2
0 0 0
-0.2 -0.2
-0.5
-0.4 -0.4
-1
-0.6 -0.6
-1.5
-0.8 -0.8
-1 -1 -2
0 20 40 60 80 100 0 20 40 60 80 100 0 20 40 60 80 100
123456 96 97 98 99 100
Compute yf=ifft(y,’symmetric’)
And you get the low pass filtered signal!
1.5
1
-1.5
0 20 40 60 80 100
In MATLAB, you have to force conjugate symmetry, else you will get complex values due round-off errors in doing FFT and IFFT
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 11
Direct filtering (in frequency domain) –cont.
How it works:
1
40
0.8
2
20
1.5
0 Real values 0.7
1
-20 0.6
0.5 DFT (or FFT) -40 0.5
0 20 40 60 80 100
0
50 0.4
-0.5
-2
0 20 40 60 80 100 0.1
-50
Original signal X 0 20 40 60 80 100
0
0 20 40 60 80 100
with length=100 1
Hanning
0.9
0.3
sides/ends
0.2
0.1
H=hanning(12);
0
0 20 40 60 80 100
Hf(1:6) =H(7:12);
H(7:95)=0;
20
1.5
15
0.5 5
0
0 0 20 40 60 80 100
-0.5
50
y=fft(x);
-1
0
Imaginary values yy=y.*Hf;
yf=ifft(yy,'symmetric');
-1.5
0 20 40 60 80 100
-50
Low pass filtered 0 20 40 60 80 100
signal plot(yf);
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 12
Filtering in time domain
Two disadvantages of direct filtering
computational time and complexity is high
there is distortion
We will look at
Simple FIR filters
IIR filters using MATLAB codes
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 13
Filtering in time domain (in equation form)
The output from a IIR digital filter in made Time domain
up of previous inputs and previous outputs 2
40
20
M N 0 20 40 60 80 100
y[ n ]
50
-0.5
-1 Imaginary values
0
k 1 j 1
-1.5
-2
0 20 40 60 80 100
-50
Original signal X 0 20 40 60 80 100
with length=100
where B and A are the filter coefficients 0.9
1
Multiply with
and the operation * is convolution
0.8
coefficients A 0.6
window on both
coefficients B 0.5
15
1
IDFT (or IFFT) Real values
filtering
10
0.5 5
0
0 0 20 40 60 80 100
50
-0.5
Imaginary values
Convolution operation will not be
-1
0
-1.5
0 20 40 60 80 100
signal
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 14
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
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) 15
Sum filter (cont.)
The frequency response of the sum filter: not very good as it is far from
the ideal ‘square’ filter
Magnitude is
also known as
gain
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 16
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, 20log10(1)=0 dB
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 17
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
x(n) x(n-1) y(n) y(n) y(n-1) z(n)
z-1 + z-1 +
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 18
Increasing the order of sum filter (cont.)
So for order M, we have
M Q Q!
y ( n ) M Cr x[n r ] where Cr
r 0 r! (Q r )!
Figure from
S.K.Mitra,
DSP 3e
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 19
Simple high pass FIR filter
This is also known as a
difference filter
Similarly, a HPF can be designed using y[n]=x[n]-x[n-1]
For hardware design, the block diagram would look like
y(n)
x(n)
z-1 -
+
x(n-1)
Figure from
The stopband frequency (when gain=0) is at 0 Hz, i.e. there is no stopband S.K.Mitra,
DSP 3e
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
N
y ( n ) ( 1) r N C x[n r ]
r 0 r
As homework, try y[n] for N=3?
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 20
Simple HPF FIR filter example
HPF can be used to remove mean values and low frequency polynomial trends, i.e.
to detrend the data
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) 21
Simple band pass FIR filter
Similarly, a BPF can be designed using a combination of LPF and HPF
Magnitude response
This is known as sum and difference (SD) filter
1
0.9
Different orders, M and N can be chosen to obtain
M=7, N=2
0.8 M=28 N=8
Gain
0.5
N
GM , N ( f ) ( 2 cos fT ) M 2 sin fT
0.4
/ Gain
cf
0.3
0.2
0.1
0
0.00 0.25 0.50 0.75 1.00
Where Gaincf is the gain at centre frequency given
Normalised Frequency (pi rad/sample)
by
f M N
fcentre s cos1
2 M N
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 22
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]
x(n) y(n) z(n)
LPF HPF
M
y ( n ) M C r x[ n r ]
r 0
Solution (can you get the answer?)
N
z ( n ) ( 1) r N C y[n r ]
r 0 r
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) 23
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
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 24
Phase response?
In you refresh your memory, you’ll know 200
that frequency responses have two
Magnitude (dB)
parts: magnitude and phase responses
0
-200
Phase (degrees)
0
Butterworth
But the phase response is not linear, i.e.
not a straight line 0
Magnitude (dB)
-20
reverse
-80
0 0.2 0.4 0.6 0.8 1
Normalized Frequency ( rad/sample)
500
Phase (degrees)
By doing so, the magnitude response is
squared while the phase response 0
becomes zero
-500
0 0.2 0.4 0.6 0.8 1
Normalized Frequency ( rad/sample)
Elliptic
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 25
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
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 26
Butterworth IIR filter
Remember the IIR filter equation
M N
y[ n ] B[k ] * x[n k ] A[ j ] * y[n j ]
k 1 j 1
In MATLAB, we can find the required minimum order for our specification using
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 27
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)
Filtfilt function filter the signal twice and uses convolution operations
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) 28
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)
Next obtain the coefficients, B and A using [B,A]=butter (11, 40/100) which gives
B = 0.0002 0.0026 0.0129 0.0386 0.0772 0.1081 0.1081 0.0772 0.0386 0.0129
0.0026 0.0002
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 29
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) 30
Elliptic filter
The same approach could be used for designing an Elliptic filter.
[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) 31
Advantages and disadvantages of FIR/IIR
Let’s sum up the advantages and disadvantages of FIR/IIR filters
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) 32
Study guide (Lecture 5)
From this week’s lecture, you should know how to perform
filtering using
End of lecture 5
Lecture 5 BME452 Biomedical Signal Processing 2013 (copyright Ali Işın, 2013) 33