0% found this document useful (0 votes)
45 views

Lab 5-Digital Filter

This document describes an experiment on digital filtering of ECG signals using MATLAB. It discusses the objectives of understanding different noise sources in ECG signals and various digital filter types. It provides background on ECG signals and common noise types. It then explains concepts of digital filtering using FIR and IIR filters to remove noise. Implementation of moving average and Butterworth filters on ECG signals is demonstrated in MATLAB. Filter design and analysis functions like freqz are also introduced.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Lab 5-Digital Filter

This document describes an experiment on digital filtering of ECG signals using MATLAB. It discusses the objectives of understanding different noise sources in ECG signals and various digital filter types. It provides background on ECG signals and common noise types. It then explains concepts of digital filtering using FIR and IIR filters to remove noise. Implementation of moving average and Butterworth filters on ECG signals is demonstrated in MATLAB. Filter design and analysis functions like freqz are also introduced.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

School of Applied Medical Science

Department of Biomedical Engineering


Medical Signal Processing Lab (BM323)

Experiment 5: Digital Filters

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:

1. Power line interference (50 Hz).


2. Respiration, causing drift in the baseline known as baseline wandering, (0.15-0.3Hz) as
shown in figure 1, this type of noise can be suppressed using high pass filter.
3. Noise coupled from other electronic devices, usually at high frequencies.
4. Electromyography (EMG) noise.

Figure 5.1: Noisy wandering ECG signal

Signal Processing Concepts:

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.

Moving average filter:


The moving average filter is a type of finite impulse response filter; it is the most
common filter in DSP, mainly because it is the easiest digital filter to understand and to
use. It is used to analyze a set of data points by creating an average or different subset of
full data set. This filter can be obtained by first taking the average of the first subset. The
fixed subset size is then shifted forward, creating a new subset of numbers, which is
averaged. This process is repeated over the entire data series. The plot line connecting all
the fixed averages is the moving average. In spite of its simplicity, the moving average
filter is optimal for a common task: reducing random noise while retaining a sharp step
response. This makes it the premier filter for time domain encoded signals. However, the
moving average is the worst filter for frequency domain encoded signals, with little
ability to separate one band of frequencies from another. This filter is a type of
convolution and so it can be viewed as an example of a low pass filter or smoothing filter.

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

Table 5.1: List of commonly used digital filters

Name Advantage Disadvantage

Maximally flat in the Roll-off is not very steep


Butterworth passband (passband ripple compares to other filters
is zero)
Monotonic roll – off , Some passband ripple
Chebyshev (Type I )
steeper than the Butterworth
Monotonic roll – off , Some stopband ripple
Chebyshev (Type II )
steeper than the Butterworth
Steeper roll –off than Non-monotonic roll-off,
Elliptical Chebyshev filter. ripple in both the passband
and stopband.

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.

Figure 5.4: frequency response of rectangular filters

The basic rectangular filter implemented here is fir1 :

>>b= fir1(n,wn,’ftype’) ;

n: The filter order.


wn: Yhe cutoff frequency , it is a number between 0 and 1, where 1 corresponds to the
Nyquist frequency.
ftype : is the type of filter
 'high' for a highpass filter with cutoff frequency wn.
 'stop' for a bandstop filter, if wn = [w1 w2]. The stopband frequency range is
specified by this interval.
 'DC-1' to make the first band of a multiband filter a passband.
 'DC-0' to make the first band of a multiband filter a stopband.

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:

c. IIR Filter Design using Butterworth filter


The following formula is used to design a low pass digital Butterworth filter :

>>[z,p,k] = butter(n, wn)

n: The order of the filter.


wn: The normalized cutoff frequency, it must be must be a number between 0 and 1,
where 1 corresponds to the Nyquist frequency.
If wn is a two-element vector, wn = [w1 w2], butter returns an order 2*n digital bandpass
filter with passband w1 < ω < w2.
This function returns the zeros and poles in length n column vectors z and p, and the gain
in the scalar k.
>>[z,p,k] = butter(n, wn,'ftype')

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

 James H.McClellau , Ranald W.Schafe, Mark A.Yoder , Signal Processing First ,


2003 Pearson Education, Inc.
 John L.Semmlow , Biosignal and Biomedical Image Processing ,MATLAB-
Based Application.
 http://www.mathworks.com/help/techdoc/ref/f16-48518.html#bqw1au_-1.

Figures sources:

Figure 5.1 from http://rspa.royalsocietypublishing.org/content/464/2095/1673.full


Figure 5.2 from http://www.dspguide.com/ch15/2.htm
Figure 5.3 from http://en.wikipedia.org/wiki/File:Electronic_linear_filters.svg
Figure 5.4 from http://www.pling.org.uk/cs/dad.html

10

You might also like