Lab 5-Digital Filter
Lab 5-Digital Filter
Objectives:
1. Study the main noise sources that distort the recorded ECG signal.
2. Be familiar with FIR and IIR filters.
3. Understand the basic principle of moving average filters using MATLAB.
4. Understand the basic principle of Butterworth filters using MATLAB.
5. Be able to filter ECG signals using MATLAB.
Physiological Background:
Biopotential signals originate from many physiological processes in the body and can be
measured using electronic equipments. Each of these biopotentials has different
amplitude and frequency characteristics that make them distinct from other
measurements.
Electrocardiograph is one of the simplest, fastest and non-invasive techniques that used to
measure and evaluate the electrical activity of the heart. Electrical activity of heart can be
measured by using electrodes placed in certain points on the body and the output
approximated by dipoles (a vector drawn between two opposite electrical charges) with
time varying amplitude and orientation. The electrical potential and the associated
mechanical events (systole and diastole) that developed in the heart are called an
electrocardiogram (ECG).
ECG signal appears very different from the EOG, EEG, or EMG signals. However,
sometimes the recorded signals may be distorted by other physiological signals and
1
noises due to the proximity of the recording electrodes. ECG signals acquired with
several noise and artifacts, these contamination can be classified into the following
categories:
In order to extract the desired signal from a contaminated one, processing must be
performed. Digital filters can be used to extract meaningful information from recorded
physiological signals and reduce all unwanted signals.
The analog filters are actual hardware circuits. Each time the desired parameters need to
be changed, circuit would have to rebuild or replace parts each time. Therefore, if
changes in the cutoff frequency or filter order are desired, resistors, capacitors or
inductors must be replaced.
Digital filtering can be used to remove the undesired biopotential from the desired one.
These types of filters are based on the mathematical rules of these filters. This allows the
implementation of these filters in software, which give instant flexibility in filter design.
2
In order to design these filters in the computer, we create an array with filter coefficients
to mimic the frequency response of the hardware filter. These filter coefficients are then
convolved with the digitally sampled signal. The result is an array containing values of
the filtered signal.
There are two main types of digital filters: FIR and IIR filters. FIRs, or finite impulse
response filters, are designed without using feedback from the output, i.e., the output of
the filter has no impact on the next sample that is filtered. They have the advantages of
always being sable and having linear phase shifts. However, FIR filters are less efficient
in term of computer time and memory than IIR filters.
Figure 5.2 shows an example of moving average filter in (a) the rectangular pulse is
buried in random noise, in (b) and (c) the signal is filtered with 11 and 51 points moving
average filters, respectively. As the number of points in the filter increase, the noise
becomes lower but the edges become less sharp.
3
Figure 5.2: Moving average filter
IIR filters or infinite impulse response filters, use feedback so that the filtered output has
an effect on the next value. IIR filters do not have linear phase, but they can usually meet
a specific frequency criterion, such as cutoff, sharpness or slop, with a much lower filter
order (i.e. a lower number of filter coefficients). The transfer function of IIR filters
includes both numerator and a denominator term unlike FIR filters that have only a
numerator.
There are many similarities between analog and IIR digital filters are that all well- known
analog filter types can be duplicated as IIR filters. Specially the Butterworth, Chebyshev
Type I and II and Elliptic designs can be implemented as IIR digital filters and can be
supported in the MATLAB Signal Processing Toolbox. Table 5.1 lists some of these
digital filters and figure 5.3 shows the frequency responses of these filters.
4
Figure 5.3: The frequency response of commonly used low pass digital filters
5
Butterworth filter
Butterworth and the other filter are often based on the bilinear transformation method or
match Z-transform method, Butterworth filters are characterized by a magnitude response
that is maximally flat in the passband and monotonic in stop band region. Roll-off is not
very steep compares to other filters. lowpass, bandpass, highpass, and bandstop digital
filter can be design using Butterworth filters.
In our experiment, we will use Butterworth filter and moving average filter using Matlab
to filter ECG signal .
MATLAB Implementation
Matlab Signal Processing Toolbox includes routines that can be used to apply both FIR
filters and IIR filters. While they are not necessary for either the design or application of
FIR filters, they do ease the design of both filter types, particularly for filters with
complex frequency characteristics or demanding attenuation requirements.
One of the useful signals processing routine determines the frequency response of a filter
with a given coefficients. Matlab routine freqz includes frequency scaling and plotting; it
plots or produces both magnitude and phase characteristics of filter’s frequency response:
>> [h , w] = freqz (b,a,n,fs );
Where b and a are the filter coefficients and n is the number of points in the desired
frequency spectra. Setting n as a power of 2 is recommended to speed computation (the
default is 512). The input argument fs is optional and specify the sampling frequency.
Both output arguments are also optional; if freqz is called without the output arguments,
the magnitude and phase plots are produced. If specified, the output vector h is the n-
point complex frequency response of the filter. The magnitude would be equal to abs (h)
while the phase would be equal to angle (h). The second output argument, w , is a vector
the same length as h containing the frequencies of h and is useful in plotting . If fs is
given, w is in Hz and rangers between 0 and fs/2 ; otherwise w is in rad/sample and
ranges between 0 and π.
6
1. First Stage, Designing Digital Filters
a. FIR Filter Design Using fir1
The filter order (the number of coefficients in b(n)), and the cutoff frequency are the
basic requirement during design , otherwise the design procedure is straightforward . The
MATLAB Signal Processing Toolbox used two filter design routines based on the
rectangular filters (ideal frequency responses of the filters) as shown in figure 5.4.
>>b= fir1(n,wn,’ftype’) ;
7
b. FIR Filter Design Using Moving Average Filter:
To Design moving average filter using MATLAB , the coefficient of the numerator must
be determine . This filter operates by averaging a number of points from the input signal
to produce each point in the output signal. In equation form:
The above formula is used to design a highpass, lowpass, or bandstop filter, where the
string 'ftype' is one of the following:
'high' for a highpass digital filter with normalized cutoff frequency wn
'low' for a lowpass digital filter with normalized cutoff frequency wn
'stop' for an order 2*n bandstop digital filter if wn is a two-element
vector, wn= [w1 w2]. The stopband is w1 < ω < w2.
8
2. Second Stage, Applying the Digital Filters
The function
>> y = filter(b,a,x)
Creates filtered data y by processing the data in vector x with the filter described by
numerator coefficient vector b and denominator coefficient vector a. If a(1) is not equal
to 1, filter normalizes the filter coefficients by a(1). If a(1) equals 0, filter returns an error.
Procedure
Part one
1. load the recorded ECG signal in your workspace use the following formula:
>>Load filename.mat
2. Take the transpose of this data to be able to plot it clearly.
3. Plot the ECG signal, what are the types of contaminants that distort this signal?
4. Design bandpass FIR filter using fir1 , try the orders 3 ,6,15 and 30 With cutoff
frequency between 0.9Hz to 59Hz . If the sampling frequency is 1000 Hz ,
Describe the effect of changing the order on the signal after filtration .
5. Filter the recorded ECG signal using filter routine.
6. Change the cutoff frequencies and describe the effect of changing the frequencies
on the signal after filtration.
Part Two
1. Design second order Butterworth bandpass filter with cutoff frequency from
0.9Hz to 59Hz, (Nyquest frequency is 500).
2. Filter the recorded ECG signal in part1 /step 2 using filter Explain in details the
effect of this filter on the ECG signal.
3. Plot the frequency response of this filter using freqz.
9
4. Design 20 point moving average signal.
5. Plot the frequency response of this filter using freqz. What do you note? Does this
filter have a good effect on frequency domain?
6. Apply this filter two time after a Butterworth filter and plot the ECG signal after
Butterworth filter, after first moving average filter and the second moving
average, What do you note ? Compare the results.
Sources
Figures sources:
10