Page 1
ECG Parameter Extraction and Motion Artifact Detection
by
Tianyang Li
B.Eng., Dalian University of Technology, China, 2014
A Report Submitted in Partial Fulfillment of the
Requirements for the Degree of
MASTER OF ENGINEERING
in the Department of Electrical and Computer Engineering
c© Tianyang Li, 2016
University of Victoria
All rights reserved. This report may not be reproduced in whole or in part, by
photocopying or other means, without the permission of the author.
Page 2
ii
ECG Parameter Extraction and Motion Artifact Detection
by
Tianyang Li
B.Eng., Dalian University of Technology, China, 2014
Supervisory Committee
Dr. Xiaodai Dong, Supervisor
(Department of Electrical and Computer Engineering)
Dr. T. Aaron Gulliver, Departmental Member
(Department of Electrical and Computer Engineering)
Page 3
iii
Supervisory Committee
Dr. Xiaodai Dong, Supervisor
(Department of Electrical and Computer Engineering)
Dr. T. Aaron Gulliver, Departmental Member
(Department of Electrical and Computer Engineering)
Page 4
iv
ABSTRACT
Cardiovascular disease is the leading cause of death in the world. Long-term
monitoring of heart condition through electrocardiogram (ECG) will provide vital
information for prevention, early warning and detection of fatal heart disease. Re-
cently, we developed a portable real-time ECG monitoring system which patients can
use during their daily activity. The large amount of ECG data recorded needs to
be processed for automatic detection and classification. This report focuses on the
extraction of essential ECG parameters from the ECG waveforms and the detection
of motion artifacts. Algorithms to calculate heart rate, QRS complex and duration,
ST elevation, are designed. Due to body motion, ECG signals are often perturbed
by motion induced artifacts. Motion artifacts (MA) severely affect the detection ac-
curacy of ECG parameters and any meaningful interpretation of ECG waveforms.
Though there have been a number of methods to eliminate motion artifacts, few of
them are suitable for our portable device due to their high computational complexity
or extra reference signal requirement. In this project, a new algorithm to detect mo-
tion artifacts is proposed, which applies Machine Learning based on the features we
extracted. Experimental results show that this algorithm correctly detects over 90%
of the motion artifacts. Finally, parameter extraction and MA detection algorithms
are integrated into an Android application that reads in the raw ECG signal received
by a smartphone from an ECG sensor and processes the real-time signal with these
algorithms to generate an ECG report.
Page 5
v
Contents
Supervisory Committee ii
Abstract iii
Table of Contents v
List of Tables vii
List of Figures viii
Acknowledgements x
1 Introduction 1
1.1 Background and Motivation . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Technical Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Contributions of the Project . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Outline of the Report . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 ECG Parameters Extraction 5
2.1 ECG Parameters Definitions and Measurements . . . . . . . . . . . . 5
2.1.1 Pan-Tompkins Algorithm for QRS Complex . . . . . . . . . . 6
2.1.2 Heart Rate (HR) . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.3 QRS duration . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.4 PR Interval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.1.5 QT/QTc Interval . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.6 ST elevation/depression . . . . . . . . . . . . . . . . . . . . . 13
2.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3 Motion Artifacts Detection via Machine Learning Algorithms 16
3.1 Motion Artifacts Definition and Properties . . . . . . . . . . . . . . . 17
Page 6
vi
3.2 Related Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2.1 Adaptive signal processing . . . . . . . . . . . . . . . . . . . . 18
3.2.2 Discrete wavelet transformation . . . . . . . . . . . . . . . . . 19
3.3 Features extraction for machine learning algorithms . . . . . . . . . . 20
3.3.1 Machine learning introduction . . . . . . . . . . . . . . . . . . 20
3.3.2 Features extraction . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4 Data collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4.1 Feature scaling . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4.2 Data collection from hardware board . . . . . . . . . . . . . . 26
3.4.3 Applying machine learning algorithms . . . . . . . . . . . . . 27
3.4.4 Making data more generic . . . . . . . . . . . . . . . . . . . . 30
3.5 Performance analysis and cross validation . . . . . . . . . . . . . . . . 30
3.5.1 Performance features sensitivity and specificity . . . . . . . . . 30
3.5.2 Cross validation introduction . . . . . . . . . . . . . . . . . . 32
3.5.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4 Java Implementation 38
4.1 MATLAB to Java conversion . . . . . . . . . . . . . . . . . . . . . . 38
4.2 Algorithm structure in Java . . . . . . . . . . . . . . . . . . . . . . . 42
4.3 Java APIs design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5 Conclusions 45
5.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.2 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Bibliography 47
Page 7
vii
List of Tables
Table 2.1 Voltage conversion . . . . . . . . . . . . . . . . . . . . . . . . . 14
Table 3.1 Cross validation of Pocket Algorithm . . . . . . . . . . . . . . . 34
Page 8
viii
List of Figures
Figure 1.1 Sport band vs Holter monitor [3][2]. . . . . . . . . . . . . . . . 2
Figure 1.2 Architecture of the ECG system. . . . . . . . . . . . . . . . . . 2
Figure 2.1 ECG of a heart in normal sinus rhythm [4]. . . . . . . . . . . . 6
Figure 2.2 Detection result of Pan-Tompkins algorithm. . . . . . . . . . . 8
Figure 2.3 Definition of QRS duration [6]. . . . . . . . . . . . . . . . . . . 9
Figure 2.4 Q onset and S end detection. . . . . . . . . . . . . . . . . . . . 11
Figure 2.5 Zoomed-in figure of Q onset point. . . . . . . . . . . . . . . . . 12
Figure 2.6 P onset detection results. . . . . . . . . . . . . . . . . . . . . . 12
Figure 2.7 T wave end definition [7]. . . . . . . . . . . . . . . . . . . . . . 13
Figure 2.8 Final results obtained via MATLAB. . . . . . . . . . . . . . . . 14
Figure 2.9 ST elevation definition [24]. . . . . . . . . . . . . . . . . . . . . 15
Figure 3.1 Motion artifacts (muscle tremors) [25]. . . . . . . . . . . . . . . 17
Figure 3.2 Motion artifacts detected with our board. . . . . . . . . . . . . 17
Figure 3.3 Adaptive filter structure [17]. . . . . . . . . . . . . . . . . . . . 18
Figure 3.4 Three-level wavelet decomposition tree [10]. . . . . . . . . . . . 19
Figure 3.5 Motion artifacts during detection. . . . . . . . . . . . . . . . . . 22
Figure 3.6 MATLAB code to calculate RR energy. . . . . . . . . . . . . . 22
Figure 3.7 MATLAB code to calculate RR energy. . . . . . . . . . . . . . 23
Figure 3.8 RR intervals during motion artifacts period. . . . . . . . . . . . 24
Figure 3.9 Feature: SQ noise definition. . . . . . . . . . . . . . . . . . . . 25
Figure 3.10Data plot example. . . . . . . . . . . . . . . . . . . . . . . . . . 28
Figure 3.11In-sample error with iteration based on Pocket Algorithm. . . . 29
Figure 3.12Beat1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Figure 3.13Beat2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Figure 3.1410th-order target function and 15 noisy data points. . . . . . . . 34
Figure 3.1510th order fit (red curve) versus 2nd order fit (red curve)[18]. . . 35
Page 9
ix
Figure 4.1 FIR filter realization [1]. . . . . . . . . . . . . . . . . . . . . . . 40
Figure 4.2 General structure of ECG signal processing module in Java Ap-
plication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Figure 4.3 Java APIs design and details. . . . . . . . . . . . . . . . . . . . 44
Page 10
x
ACKNOWLEDGEMENTS
I would like to express my special thanks to my supervisor Dr. Xiaodai Dong who
gave me the opportunity to do this wonderful project. I am thankful for her aspiring
guidance and invaluably constructive advice during the project work.
I would also like to thank my parents and friends who always support me and
thank my group members for their help and suggestions.
Page 11
Chapter 1
Introduction
1.1 Background and Motivation
Since heart disease has become one of the leading causes of sudden death all over the
world in recent years, more and more heart monitoring devices are invented to monitor
heart conditions. Some of them are eligible to detect potential heart disease. These
monitors have different functions as well as accuracy. Some of them are only used
to display heart rates during exercise such as Fitbit and Apple Watch, and some can
be used for clinical diagnosis with high accuracy, such as holter monitors. However,
in order to monitor real-time heart conditions, these current devices have different
disadvantages. For example, in Fig. 1.1, the sport bands, like Apple Watch and Fitbit
[2] can be used to record and monitor heart rate changes during activity, but only heart
rate is not enough for potential heart disease detection and heart condition diagnosis.
In clinical diagnosis, holter monitors [3] can record electrocardiogram (ECG) wave
for a long time and the data saved can be analyzed later to detect potential heart
diseases, but holter mornitors are not capable of real-time monitoring.
Our groups objective is to design a portable low-cost wireless heart monitoring
system with real-time ECG analysis, intended for use during ones normal daily life.
The basic system architecture of this ECG system is composed of an ECG sensor
which can sense the ECG data from a human body, and then transmit the ECG data
to an Android smartphone via Bluetooth Low Energy. The raw ECG data will be
processed in an Android Application to display the ECG wave and heart rate in real-
time, and also generate an ECG report, which includes the essential ECG parameters
for diagnosis. In Fig. 1.2, the architecture of this portable ECG system is shown.
Page 12
2
Figure 1.1: Sport band vs Holter monitor [3][2].
Figure 1.2: Architecture of the ECG system.
The hardware board can collect raw ECG data and transmit it via Bluetooth Low
Energy to smartphone. The ECG signal processing module developed in this work,
which includes motion artifacts detection, digital filters and parameter extraction is
integrated into the Android application.
1.2 Technical Challenges
A number of technical challenges have to be addressed during the design and im-
plementation of this ECG signal-processing module. First and foremost, in order to
obtain more accurate parameters extraction results, several interferences, such as sig-
nal spikes that may occur during Bluetooth transmission, baseline wander and signal
fluctuations need to be mitigated as much as possible. Among all of them, the most
challenging one is to eliminate motion artifacts, which commonly occur during users
respiration or other movements. Motion artifacts are versatile, which means they
do not have a specific bandwidth, and hence specific fixed-bandwidth digital filters
Page 13
3
cannot eliminate them.
There are several ways to eliminate motion artifacts in theory. For example, adap-
tive filter in [17] is a popular method to deal with motion artifacts, but it requires
a reference signal according to muscle movements, which is usually collected via a
motion sensor or accelerometer module. Another common method is using discrete
wavelet transformation (DWT) proposed in [10]. However, this method has high com-
putational complexity and not suitable for real-time detection. Our project requires
real-time detection, so low computational complexity and high computational speed
are required.
Considering the requirements and current available solutions above, we need to
propose a new method to eliminate motion artifacts based on our objective and
requirement. Since motion artifacts have inconsistent and unpredictable features,
machine learning is an effective way to solve this kind of problem. However, choos-
ing suitable features to apply machine-learning algorithms is another challenge in
this project because there is nearly no reference about applying machine learning
algorithms into motion artifacts elimination. In order to extract effective features,
we collected and processed thousands of ECG data, and also tried several different
features combinations. Finally, we selected three relevant features and the results of
many examinations proved that this machine-learning algorithm is effective in motion
artifacts elimination.
Finally, another challenge we are facing is to implement the entire ECG signal-
processing module into Android application. During algorithm design stage, the
digital filters module used is from MATLAB built-in module, such as Butterworth
digital filter. However, there is no same module in Java library as these built-in
modules in MATLAB. We tried to search for the similar digital filters open-source
library in Java, but the results are far from satisfaction. In this case, we have to
design our own digital filters based on the knowledge from Digital Signal Processing
course. The details of this implementation will be described later in Chapter 4.
1.3 Contributions of the Project
According to the main objective of the project, this report provides a detailed and
high-level overview of ECG parameters and ECG signal processing techniques. More-
over, I propose an efficient and accurate way to identify and remove motion artifacts
interference. More specifically, the following aspects are featured works and contri-
Page 14
4
bution of this project.
1. Study the ECG parameters and methods to calculate them.
2. Learn how to apply digital filters to eliminate interference.
3. Identify three unique features from motion artifacts to use in Machine Learning
algorithms.
4. Collect and process large amount of ECG data for the training procedure.
5. Verify the accuracy of this motion artifacts elimination algorithm.
6. Implement these algorithms into an Android Application and design the API.
1.4 Outline of the Report
This report is organized as follows:
Chapter 2 gives a brief overview of essential ECG parameters and introduces the
algorithms for ECG parameters extraction. Furthermore, details of the signal pro-
cessing and Pan-Tompkins algorithm for QRS complex detection are also presented.
Chapter 3 introduces a new way to identify motion artifacts in the ECG signal
via Machine Learning algorithms. Cross validation is done to verify the robustness
of the extracted features.
Chapter 4 describes the details about algorithm’s Java implementation and intro-
duces the APIs.
Chapter 5 concludes the report and suggests future work.
Page 15
5
Chapter 2
ECG Parameters Extraction
Typically, a standard heart beat has five deflections, i.e., P, Q, R, S and T, which
are shown in Fig. 2.1 [4]. The QRS complex is the central and most visually obvious
part of a typical electrocardiogram (ECG or EKG). A typical ECG tracing includes
a repeating cycle of the three electrical entities: a P wave (atrial depolarization), a
QRS complex (ventricular depolarization) and a T wave (ventricular repolarization).
All of the waves in an ECG tracing and the intervals between deflections have a
predictable time duration, a range of acceptable amplitudes (voltages), and a typical
morphology. These features can help doctors/clinics to detect potential arrhythmias
and therefore are of clinical significance. In this project, we implemented several
algorithms to extract ECG parameters in real time. These parameters mainly include:
Heart Rate (HR), PR interval, QRS interval, QT/QTc and ST elevations/depressions.
The definition of these parameters and algorithm details will be explained in the
following sections.
2.1 ECG Parameters Definitions and Measurements
Each parameter mentioned above has its own medical definition and different mea-
surement methods. In this project, I mainly refer to a popular and widely cited
algorithm Pan-Tompkins (P-T) algorithm for QRS complex detection [19]. Based
on this algorithm, it is easy to obtain Q, R and S points, which are the basis for the
following features calculations. In the next section, I will briefly introduce this P-T
algorithm, and then present each parameters definition and measurement.
Page 16
6
Figure 2.1: ECG of a heart in normal sinus rhythm [4].
2.1.1 Pan-Tompkins Algorithm for QRS Complex
Pan-Tompkins algorithm is a popular algorithm that is widely cited and applied for
detecting QRS complexes in many portable ECG devices due to its simplicity and
fairly high accuracy. Besides, Pan-Tompkins algorithm is a real-time algorithm for
detecting the QRS complexes of ECG signals. It can reliably recognize QRS complexes
based upon analyses of slope, amplitude, and width. It applies a special digital band-
pass filter to reduce various types of interference present in ECG signals, so that it
can reduce false detections effectively. In the following parts, the implementation
details of P-T algorithm will be prsented.
Band-pass Filter
The band-pass filter can reduce the influence of 60/50 Hz interference, baseline wan-
der, and T-wave interference. Here, we choose the desirable pass band as 0.5 Hz to
45 Hz, which can effectively reduce baseline wander and high frequency noise, and
also maintain the original shape of ECG signals.
Moving Average Filter
During practical detection, signal quality may be unstable, which could be caused by
various reasons, such as unstable power voltage, data transmission interference and
Page 17
7
so on. In these scenarios, some random signal fluctuations may happen in sequential
sampling points that probably have a bad effect on the second-stage detection. In
order to solve this problem, we use moving average filter and set the window size to
three. Here, the window size can’t be set too large, because it will affect the shape
of ECG signal and affect the diagnosis of heart diseases. The first element of the
moving average is obtained by taking the average of the initial fixed subset of the
number series, which has three numbers in this case. Then the subset is modified by
”shifting forward”; that is, excluding the first number and including the next number
following the original subset in the series. This creates a new subset of numbers,
which is averaged. This process is repeated over the entire data series.
Pan-Tompkins Algorithm Overview
1. First, the raw signal passes through a digital band-pass filter in order to atten-
uate noise and interference.
2. Next process is differentiation, followed by squaring, and then moving window
integration. This step can obtain the information about the slope of the QRS.
3. Learning phase 1: requires 2 seconds data to initialize detection thresholds based
upon in-coming signal and noise peaks detected during the learning process.
4. Learning phase 2: requires two heartbeats to initialize RR interval average and
RR interval limit values.
5. Detection phase: complete the recognition process and produces a pulse for
each QRS complex. The thresholds and other parameters of the algorithm are
adjusted periodically to adapt to changing characteristics of the signal.
The main advantage of this algorithm is that it uses two sets of thresholds to
detect QRS complexes. One set provides the thresholds of filtered ECG, and the other
thresholds the signal produced by moving window integration. By using thresholds on
both signals, it improves the reliability of detection compared to using one waveform
alone. The Fig. 2.2 below shows the detection result after running P-T algorithm to
a raw ECG signal. This figure is drawn by MATLAB plot function.
Page 18
8
Figure 2.2: Detection result of Pan-Tompkins algorithm.
2.1.2 Heart Rate (HR)
Heart rate represents the speed of the heartbeats measured by the number of contrac-
tions of the heart per unit of time, typically beats per minute (bpm). For the normal
resting adults, heart rate ranges from 60 to 100 bpm. When the heart is not beating
in a regular pattern, this may be referred to as an arrhythmia. These abnormalities
of heart rate sometimes indicate heart disease.
The calculation of HR is quite straightforward. With the help of P-T algorithm,
we can simply detect each R peak in ECG, as shown in Fig. 2.2. In order to make
the result more accurate, we set an 8-second window and update the HR each eight
seconds. In every 8-second window, R peaks are detected, and then calculate the
average value of RR intervals. From (2.1), HR can be calculated.
HR =60
Avg RR Interval (s)(2.1)
RR intervals calculation is related to the sampling rate of a digital signal. As for
this project, we have 1000 Hz and 250 Hz hardware boards respectively, which means
the interval between each sampling point is 1 ms and 4 ms. After running the P-T
algorithm, the number of sampling points between each R peak can be obtained, and
Page 19
9
Figure 2.3: Definition of QRS duration [6].
then multiplied by 0.001 or 0.004 sec to get each RR interval.
2.1.3 QRS duration
The QRS complex is the most obvious part of a normal ECG. For adults, it normally
lasts 0.06 – 0.10 s and it may be shorter in children or during physical activities. QRS
duration parameter is of clinical significance, such as prolonged duration indicates
hyperkalemia or bundle branch block.
QRS duration is measured from Q wave onset to S wave end [13], rather than
simply measure the time intervals between Q and S point. Fig. 2.3 [6] below shows
the exact definition of QRS duration. S wave end has another terminology called
J-point [12], where the QRS complex meets the ST segment. The J-point is easy to
identify when the ST segment is horizontal and forms a sharp angle with the last
part of the QRS complex. However, in some heart diseases or noisy ECG signals,
the location of J-point is less clear. Therefore, there are two possible definitions of
J-point:
• The first point of inflection of the upstroke of the S wave.
• The point at which the ECG traces becomes more horizontal than vertical.
Page 20
10
Here, we apply the same method to detect Q onset and J-point, which is the
first derivation (slope) of the ECG signal. In practical application, moving average
filter mentioned above solved the potential unstable signal problem, which makes the
calculation more accurate. Besides, from [23], we could set proper thresholds for
searching range to make results more convincing. From applying P-T algorithm, we
have already obtained the Q, S and T peak points. The corresponding onset and
offset values are the points having nearly zero or minimum slope region before the Q
and after the S points.
• The Q onset point is detected as the minimum slope point within a window of
40 ms starting from the Q peak index along the start of the ECG data array.
• The J-point is detected as the minimum slope within a window of 40 ms starting
from S point index towards the end of the ECG data array.
Thus, QRS duration parameter is defined as follows:
QRS Duration = S end−Q onset (2.2)
Fig. 2.4 below shows the detection results after applying the Q onset and S end
detection algorithms via MATLAB. The pink circle indicates the Q onset and black
ones indicate S end (J-point). Fig. 2.5 is the zoomed-in figure, which shows the exact
Q onset point clearly in green circles.
2.1.4 PR Interval
The PR interval is the period that extends from the beginning of the P wave until the
beginning of the QRS complex (Q onset). Its duration is normally between 120 and
200 ms. This parameter is also an essential one for potential heart disease detection.
Variations in the PR interval can be associated with certain medical conditions. For
example: a long PR interval (of over 200ms) may indicate a first-degree heart block.
Since we have already obtained Q onset position, the only work left is to find
P wave beginning, which applies the same idea as mentioned above. From P-T
algorithm, P peak points can be detected directly. And then, search the P onset
point backward in a certain window. From [16], I set the size of search window to be
60 ms. Next, the slope of each point in that window is calculated and the minimum
point is the P onset point.
Page 21
11
Figure 2.4: Q onset and S end detection.
Here, we use the equation below to calculate nth point slope:
slope(n) = −2X(n− 2)−X(n− 1) +X(n+ 1) + 2X(n+ 2) (2.3)
In Fig. 2.6, the black circle shows the P onset points after detection.
2.1.5 QT/QTc Interval
In cardiology, the QT interval is a measure of the time period between the start of
the Q wave and the end of the T wave in the hearts electrical cycle. A prolonged QT
interval is a potential sign for ventricular tachyarrhythmia and a risk factor for sudden
death. Since QT interval is dependent on the heart rate in an obvious way (the faster
the heart rate the shorter the QT interval), modern computer-based ECG machines
can easily calculate a corrected QT, i.e., QTc. The standard clinical correction is
to use Bazetts formula to calculate the heart rate-corrected QT interval. Bazetts
formula [22] is:
QTc =QT√RR
(2.4)
Here, RR is often derived from the heart rate (HR) as 60/HR for easy measure-
Page 22
12
Figure 2.5: Zoomed-in figure of Q onset point.
Figure 2.6: P onset detection results.
ment.
Next problem is how to detect T wave end. The T wave end is where the tangent
line for the steepest part of the T wave intersects with the baseline of the ECG.
Fig. 2.7 [7] illustrates the definition. The accurate measurement of the QT interval
is subjective because the end of the T wave is not always clearly defined and usually
merges gradually with the baseline. QT interval in an ECG complex can be measured
manually by different methods such as the threshold method, in which the end of the
T wave is determined by the point at which the component of the T wave merges
with the isoelectric baseline or the tangent method, in which the end of the T wave
is determined by the intersection of a tangent line extrapolated from the T wave at
the point of maximum downslope to the isoelectric baseline. The isoelectric baseline
was obtained by a line joining the midpoints of the TP segment [20].
The method to detect maximum slope within T offset is also the threshold and
first derivative. This time, the window size is set to 40 ms and the beginning of the
searching range is T peak. Then, we can obtain the point of maximum slope and
Page 23
13
Figure 2.7: T wave end definition [7].
calculate the T wave end based on the intersection of baseline and tangent line.
From now, we can detect all the critical points that we need to calculate the key
parameters introduced above. In order to test the result accuracy, I implement these
algorithms via MATLAB and set one raw digital ECG signal (120-sec) as input. The
average results of each parameter are obtained as:
QT = 369.7705 ms, QTc = 378.7001 ms, QRS = 114.0328 ms,
PR = 179.0820 ms, HR = 63 bps.
In order to visualize the results, Fig. 2.8 plots the ECG signal and the key points
detected. It can be seen that the detected points match well with their definations.
2.1.6 ST elevation/depression
ST elevation/depression refers to a finding on an ECG signal wherein the trace in
the ST segment is abnormally high/low above the baseline. The measurement of
ST elevation and depression is similar. Here, we take ST elevation for example to
introduce its definition and measurement. In Fig. 2.9 [24], red line is tangent to the
signal tracing and the blue arrow points to the J-point, which has been introduced
above for detecting QRS duration. The green line is 60 milliseconds after the J-point
and lower black line is the baseline. Upper black line intersects the tracing where
the green line and ECG signal intersect with each other. In this case, the distance
between these two black lines is the value that we need to calculate.
In addition, ST elevation/depression are the values that related to signal voltages.
Page 24
14
Figure 2.8: Final results obtained via MATLAB.
Table 2.1: Voltage conversionINPUT SIGNAL VIN IDEAL OUTPUT CODE
>= VREF 7FFFh+VREF/(2
15 − 1) 0001h0 0000h
−VREF/(215 − 1) FFFFh
<= −VREF (215/2) 8000h
In order to calculate the ST parameters, we need to know the voltage values. Table 2.1
describes the conversion in our hardware board implementation. (VREF = 2.42 V)
Besides, our board also increased the voltage values by 12, so we need to divided
by 12 to obtain the accurate voltage values. The equation is as following:
STvalue =ST
(65536× 12)× 2.42× 1000(2.5)
Page 25
15
Figure 2.9: ST elevation definition [24].
2.2 Summary
Until now, we have implemented the P-T algorithm as the basis to obtain the essential
peak points for subsequent parameters calculation. In order to make the results more
accurate, we also apply some DSP methods to preprocess the raw ECG signal to
eliminate interferences and noise. Besides, the definition and measurement of each
parameter have been described in details. From many experiments and tests with
our hardware board data as well as the data from online database, the implemented
algorithm works well and has the capability to provide relatively accurate results.
However, during the tests, we notice that when the signal quality is poor, false
detection would often happen. For example, when test subjects move their bodies
during testing, like twisting or stretching, signals shape will be distorted, and hence
false detection would happen. This situation is common in ambulatory ECG detection
and such effect is called motion artifacts. In next chapter, methods to detect and label
motion artifacts will be discussed in detail.
Page 26
16
Chapter 3
Motion Artifacts Detection via
Machine Learning Algorithms
For portable ECG monitor devices, ECG records are often corrupted by various kinds
of noise such as power line interference, motion artifacts and baseline drift with res-
piration. From chapter 2, we see that digital bandpass filters can be used for noise
cancellation in real-time execution. However, motion artifacts are caused by changes
in the electrode-skin impedance with electrode motion. It cannot be simply removed
by the designed digital bandpass filters because the spectrum of motion artifacts com-
pletely overlaps with the ECG signal when the user is walking or running. Therefore,
motion artifacts are the most difficult type of interference to eliminate. Besides, mo-
tion artifacts would affect the accuracy of ECG parameters detection, which may lead
to heart diseases false detection.
Recently, there are several studies on reducing motion artifacts with the filtered or
differential method. Each method may work well for certain scenario or requirement.
However, since motion artifacts often substantially distort the ECG waveform, not
much useful information can be derived from the strongly processed signal. In this
work, we propose a different handling of motion artifacts. Our objective is to identify
and label signals plagued by motion artifacts, so that later analysis and diagnosis of
heart conditions do not use these segments. To detect motion artifacts, we propose
for the first time learning based models to fulfill the task. Here, the idea of machine
learning is implemented. From lots of data testing results, it has been approved that
this machine-learning algorithm has accuracy up to 90%.
Page 27
17
Figure 3.1: Motion artifacts (muscle tremors) [25].
Figure 3.2: Motion artifacts detected with our board.
3.1 Motion Artifacts Definition and Properties
Motion artifacts [26] are basically caused by muscle tremor/noise, which will result in
minute electrode motion and change the electrode-skin impedance. When one’s skele-
tal muscles undergo tremors, the ECG is interfered by seemingly random activities.
Motion artifacts do not manifest under a fixed pattern and instead has a tendency to
suddenly change depending on the users movements and actions. Fig. 3.1 shows the
typical motion artifacts caused by muscle tremors.
In this project, several motion artifacts were recorded during ECG monitoring
with our in-house hardware board. They have various shapes, but they are similar to
Fig. 3.1. Fig. 3.2 displays motion artifacts detected with our board.
In addition, the bandwidth of motion artifacts changes over time, but usually
manifest in the form of low-frequency elements. The useful ECG bandwidth for
patient monitoring or healthcare purpose is between 0.05 Hz and 35 Hz. Therefore,
low frequency noise elements that resulting from respiration and motion artifacts
are known to overlap with the ST segment (0.8 Hz or below) of the ECG signal,
and using a high pass filter to eliminate low frequency noise elements like these can
Page 28
18
Figure 3.3: Adaptive filter structure [17].
lead to distortion of the ST segment, which contains clinical data on heart ischemia
and myocardial infarction. Thus, simple filter sets can’t effectively remove motion
artifacts while maintaining signal integrity. In the following section, some related
work will be introduced.
3.2 Related Work
Among several different methods to detect or eliminate motion artifacts in ECG sig-
nal, there are two most popular methods : adaptive signal processing and wavelet
transformation, which are widely discussed and applied. In this section, the intro-
duction of each method will be given and their advantages and disadvantages will be
discussed.
3.2.1 Adaptive signal processing
In [17], the authors proposed a technique for cancelling motion artifacts in ambulatory
ECG monitoring systems via adaptive signal processing method. The study described
in that paper attempted to develop an ECG signal processing method that uses
adaptive filter technology to eliminate ambulatory motion artifacts in ECG signal
with high efficiency.
The adaptive filter algorithm applied in the paper is the steepest decent method,
which repeatedly calculates the mean squared error-minimizing factor. Fig. 3.3 shows
the adaptive filter structure. Least mean square (LMS) adaptive filter bases on the
given filter factor and relies on a minimum mean-square error algorithm to repeatedly
calibrate the filter factor in eliminating noise, which would reduce motion artifacts
effectively.
Page 29
19
Figure 3.4: Three-level wavelet decomposition tree [10].
However, this system requires a reference input, which is used to generate moving
average signal NR (left bottom of Fig. 3.3). In this case, a motion sensor or ac-
celerometer module is required, which can generate tracking signals that are related
to user’s motion when it is attached near to ECG electrodes. After desired calculation
of a microprocessor, reference input will be generated for adaptive filter. As for this
project, the existing hardware board does not include this motion sensor module, so
that this adaptive signal processing method is not suitable for this situation.
3.2.2 Discrete wavelet transformation
In [10], ambulatory ECG signal analysis for detection of various motion artifacts
using discrete wavelet transform (DWT) approach is addressed. Basically, biomedical
signals can be analyzed in frequency domain, time domain, or time-frequency domain.
Wavelet transform provides the time and frequency information simultaneously, hence
giving a time-frequency representation of the signal.
In DWT, a time-scale representation of the digital signal is obtained using digital
filtering techniques. The DWT is performed by successive high pass and low pass
filtering of a discrete time signal in Fig. 3.4.
In the experiment, a GUI based tool called WAVEMENU from the MATLAB
Wavelet toolbox are used. The key is to perform wavelet coefficients selection such
that the synthesized signal approximates the pure cardiac signal. Here, they per-
formed 9-level signal decomposition of ambulatory ECG signal. As coefficients se-
lection was completed, the synthesized or reconstructed ECG signal, which approxi-
mates the pure cardiac component of an ambulatory ECG and the residual is termed
as motion artifact signal. The motion artifact signal was obtained by subtracting the
Page 30
20
synthesized ECG signal from the ambulatory ECG signal.
However, this wavelet method is also not suitable for our project, because DWT
will change the shape of ECG wave and may affect the heart condition diagnosis.
3.3 Features extraction for machine learning algo-
rithms
From the discussions above, it is obvious that simple digital filters are not suitable for
motion artifact elimination and adaptive filters or wavelet transformation is also not
good choices for real-time detection. Besides, motion artifacts have their own specific
features and certain shapes, which provides basis for applying machine learning algo-
rithms. In this scenario, a new method, which is based on machine learning concept
is proposed in this project. Here, we mainly focused on supervised machine learning
algorithms, which will be described in detail in the following sections.
3.3.1 Machine learning introduction
Machine learning [8] is a type of artificial intelligence (AI) that provides computers
with the ability to learn without being explicitly programmed. The process of machine
learning is using the data to detect potential patterns in data and adjust program
actions accordingly. Simply speaking, machine learning is the method to find hidden
insights from existing data (usually big size data) and facilitate us to infer something
outside these data.
The basic assumption in this machine learning framework is that we have collected
a set of observations called data, and the aim of learning is to uncover the governing
law that is responsible for the production of the data observed. In the literature, this
kind of learning strategy is called “learning from examples”, and the observations
used in the learning process are called training data.
Depending on the structure of the training data, machine-learning methods may
be categorized as supervised or unsupervised learning. These two learning methods
are:
Page 31
21
A. Supervised Learning
A training data in supervised learning assumes the form {(Xn, yn)|n = 1, 2..., N},where Xn are examples from an input space X and yn ∈ γ are produced by a (un-
known) casual system (i.e., a governing law) with Xn as input. The most popular
problems in supervised learning are classification and regression. In a classification
problem, the goal is to assign each input (that is outside the training data) to one
of the finite number of discrete categories. In a regression problem, the learning is
aimed at finding a continuous-valued approximating function to fit a set of training
data.
B. Unsupervised Learning
In unsupervised learning, we are just given a set of samples {Xn, n = 1, 2..., N}, but
the labels yn are not available. Because yn are missing, the available observations
are called unlabeled data. The learning problems in this case are typically related to
pattern classifications and structure identification (such as number of data clusters,
center of clusters, and clusters themselves) of the input data.
There are various machine-learning algorithms. In this project, we only apply
supervised algorithms, such as Pocket Algorithm (PCA), Support Vector Machine
(SVM).
3.3.2 Features extraction
In this project, we focus on supervised learning, so motion artifact features are re-
quired to be extracted as inputs Xn. yn is the corresponding labels, where “-1”
represents no motion artifact and “1” is motion artifact. In this case, finding suitable
features Xn is the key point. From a large amount of data observation and exami-
nation, we extract three related features, which can use to represent motion artifacts
quite well.
A. Feature 1: RR energy
From lots of ECG data recordings, we observe that motion artifacts signal is a com-
bination of normal ECG signal and muscle noise, which is the extra energy recorded
into ECG data. When the users move or twist their body during ECG detection,
Page 32
22
Figure 3.5: Motion artifacts during detection.
Figure 3.6: MATLAB code to calculate RR energy.
the extra energy will be generated and recorded, and hence the energy tends to be-
come larger when motion artifacts occur. Fig. 3.5 shows the typical motion artifacts
that are recorded by our hardware board. From this figure, we can see that lots of
spikes occur during the motion artifacts period. Fig. 3.2 shows the zoomed-in figure
of motion artifacts, from which we can observe that the R peak pattern of ECG is
still observable and also extra energy occurred between R peaks. Thus, the energy
between RR intervals becomes a feature of motion artifacts.
In addition, considering different users may have different ECG voltage ampli-
tudes, we use normalized voltage value to make this feature usable for any condition.
In each RR interval, we pick the largest value and convert it to one. Accordingly,
other points are set to its own value divided by the largest value in this RR interval.
Another issue to be taken into consideration is that different users may have different
heart rate. High heart rate will result in shorter RR interval and vice versa. By
dividing the number of points during RR interval to obtain average normalized RR
energy, we can eliminate the HR impact. Fig. 3.6 is the MATLAB code to obtain
each RR energy. Here, Ri is the R peak point and Ri(i+1)−Ri(i) is the RR interval.
Page 33
23
Figure 3.7: MATLAB code to calculate RR energy.
Next we give an example to calculate RR energy (Fig. 3.7). The data is from
our hardware board and there are 10 R peaks, and hence there will be 9 RR energy
values. After applying the calculation as Fig. 3.6, the RR energy results is obtained
as follows:
[0.0909, 0.0876, 0.1016, 0.1020, 0.1895, 0.0988, 0.1000, 0.1504, 0.1007]
[0.2513, 0.3062, 0.2202, 0.3981, 0.2357, 0.0987, 0.1886, 0.1104, 0.0865]
The first results is corresponding to the left figure in Fig. 3.6, which is a clean ECG
signal. The second results are for the right figure, which contains motion artifacts
signal at the beginning with values shown in red. From the comparison of these
two results, we can observe that signal with motion artifacts usually have larger
normalized RR energy.
B. Feature 2: RR interval
From the example above (Fig. 3.7), we can observe that R peak detection probably
has false detection when motion artifacts occurred. It is because P-T QRS detection
algorithm has certain learning process, which means the detection of upcoming R
peaks depends on the previous data. In this case, the motion artifacts will affect the
accuracy of R peaks detection. For example, in Fig. 3.8, motion artifacts occurred
four times during ECG detection. It is obvious that the R peak detection is not
accurate during these motion artifacts period, so that the RR interval values may
be affected, which can result in extremely large or small RR interval values. From
this circumstance, RR interval can be regarded as another special feature for motion
Page 34
24
Figure 3.8: RR intervals during motion artifacts period.
artifacts.
The calculation of RR interval is relatively straightforward and simple.
RR interval = Ri(i+1) −Ri(i) (3.1)
Here, we also take two ECG data shown in Fig. 3.7 for example. In the left figure,
the RR interval values (in millisecond) are as following:
[772, 688, 692, 676, 640, 644, 692, 716, 718];
In the right figure, the RR interval values are:
[624, 720, 552, 1072, 2408, 752, 796, 816, 832];
From these two results, we can see that the values of RR intervals may have unstable
ups and downs during motion artifacts period.
C. Feature 3: SQ noise
From a large amount of motion artifacts data observation, we extract this feature
as a typical feature of motion artifacts. The definition of this feature is also quite
straightforward, which is shown in Fig. 3.9 as follows.
In Fig. 3.9, left one is one beat with motion artifact, and right one without motion
artifact. This feature focuses on the period between S wave end and following R peaks
Q wave onset (between the first black circle and the second pink circle). If there is a
high spike, which is higher than one of the R peaks, SQ noise parameter will set to
1, otherwise, set to -1. This feature is effective because it is impossible to generate
this high spike in normal ECG. Even if the users have potential heart diseases, which
Page 35
25
Figure 3.9: Feature: SQ noise definition.
sometimes may cause high T wave, R peak will still be the highest point in one beat.
Besides, electrical noise sometimes will involve high spike, but it only occurred in a
very short period and digital filters in signal preprocessing part will eliminate it.
From Fig. 3.7, the SQ noise value of left figure is:
[−1,−1,−1,−1,−1,−1,−1,−1,−1];
SQ noise value of right figure is:
[−1,−1,−1, 1, 1,−1,−1,−1,−1];
In this project, we mainly apply these three typical features to detect motion
artifacts. In the following sections, machine learnings training and testing procedures
will be discussed in details.
3.4 Data collection
Since the features have been extracted and determined, the next significant step
for machine learning algorithms is to collect sufficient data to discover the potential
properties of motion artifacts signal against normal ECG signal. In this project, we
only focus on supervised learning, which means we have fixed outputs. Here, we use
“1” to represent the beats with motion artifacts, and “-1” to represent without motion
artifacts. Next is to collect sufficient data, which includes normal ECG signal and
motion artifacts signal. Besides, there are some rules that we need to pay attention
to when we are doing collection, which will be discussed in details in the following
sections.
Page 36
26
3.4.1 Feature scaling
Feature scaling [11] is a method used to standardize the range of independent variables
or features of data. In data processing, it is also known as data normalization and is
generally performed during the data-preprocessing step. The motivation to do feature
scaling is that when the range of values of raw data varies widely, in some machine
learning algorithms, objective functions will not work properly without normalization
or scaling. For example, if one of the features has a broad range of values, the
classification distance will be governed by this particular feature. Therefore, the range
of all features should be scaled to the similar range so that each feature contributes
approximately proportionately to the final results.
Among the three features mentioned in previous section, the value of RR interval
is around 700 milliseconds. However, the values of RR energy and SQ noise are around
1. In this case, RR interval will become the governing feature and may eliminate the
effects of RR energy and SQ noise. Here, we implement feature scaling to RR interval
feature to make its unit from millisecond to second, so that the range will be around
0.7 second, which makes all three features in the similar range as well as the output
[-1, 1].
3.4.2 Data collection from hardware board
In the first stage, we mainly collected data from our hardware board to testify the
effectiveness of three features. Next, the procedures to collect training data via MAT-
LAB is introduced.
1. Users use our hardware board to do ECG testing. While doing the tests, users
are required to twist their bodies or do some daily activities, such as walking
and sitting up and down on purpose in order to cause some motion artifacts to
be collected.
2. After testing, the data collected via an Android phone can be uploaded to
database which can be downloaded anytime in the future. Then, data will be
read into MATLAB for further processing.
3. The data collected will be filtered first to eliminate baseline wandering and
electrical interference. Next apply the ECG parameter extraction algorithm
that was discussed in Chapter 2 to obtain P, Q, R, S and T critical points.
Page 37
27
4. Based on the critical points from the last step, three feature values can be
calculated and extracted. Here, we use MATLAB command dlmwrite() to save
the feature values into a CSV file.
5. The last step is to determine if the beat contains motion artifact manually and
record the results into the CSV file.
Here is an example to illustrate the whole procedure in detail.Figure 3.10 shows
the data that we need to process.
Firstly, from the MATLAB results, we can obtain the values of three features as
follows.
RR Interval = [0.660, 0.636, 0.676, 0.700, 0.716, 0.644, 0.456, 0.744]
RR Energy = [0.1403, 0.1252, 0.1312, 0.1413, 0.2692, 0.3355, 0.3164, 0.2789]
SQ Noise = [−1,−1,−1,−1,−1, 1, 1, 1]
Secondly, using dlmwrite command to save the data into a CSV file for record:
dlmwrite(′ecgdata.csv′, RRinterval,′−append′,′ roffset′, 1,′ coffset′, 0);
Thirdly, we visually observe the ECG signal in Fig. 3.10 and input the label results
manually. In this example, the results about motion artifacts are:
Result = [−1,−1,−1,−1,−1, 1, 1, 1]
Thus, we finish the whole procedure of collecting the data. Following this method,
we need to process sufficient data and collect the features and results into a CSV file
for further analysis. After the data collection, the machine learning algorithms can
be applied.
3.4.3 Applying machine learning algorithms
There are lots of different machine learning algorithms and each of them has its own
characteristics and suitable conditions. For example, Principal Component Analysis
can decrease data dimensions via singular value decomposition (SVD), which can
improve calculation speed and decrease calculation complexity. Another popular
machine learning algorithm is called Support Vector Machines (SVM), which can
offer bigger margins for separating two sets and has more robust performance against
data uncertainty.
Page 38
28
Figure 3.10: Data plot example.
In this project, considering the number of features and complexity, a simple al-
gorithm called Pocket Algorithm [14], which is a variant of the Perceptron Learning
Algorithm, is chosen to test at the beginning. Pocket Algorithm is a simple and
useful algorithm, which can also deal with non-separable data sets with improved
performance. Here, we apply Pocket Algorithm with the data we collected from our
hardware board to do the initial test and see if these three features can produce
proper results.
In the first stage, the data collected is about 700 beats and was separated into two
sets training data and testing data (427 vs 275). First, we apply Pocket Algorithm to
training data via MATLAB and set the iteration number to 500. The in-sample error
we obtained is around 5.39%. The in-sample error with iteration times is plotted in
Fig. 3.11. It is obvious that the in-sample error decreased as the number of iteration
increased and after around 60 iterations, the in-sample error became stable around
5%. The function interface for calling Pocket Algorithm in MATLAB:
1 f unc t i on [ wt , t , e i n pocke t ] =
2 p o c k e t s e m i c i r c l e (x , y , xp , xn , l r , w0 , st1 , st2 ,K)
Besides, the out-of-sample error is also required to test because when both in-
sample and out-of-sample errors are relatively small, the features we extracted could
be regarded as effective. The process to obtain out-of-sample error is different from
Page 39
29
Figure 3.11: In-sample error with iteration based on Pocket Algorithm.
in-sample error. Firstly, we obtain the weights after applying Pocket Algorithm to
training data. The number of weights is equal to the number of features plus one
because there is a constant number needed for this Pocket Algorithm.
The weights obtained after training process is:
wt = [0.2;−0.044; 0.1815488; 0.2]
which is related to feature = [constantnumber : 1, RRInterval, RREnergy, SQNoise]
respectively. Thus for the testing data, the results are equal to wt .* features. If re-
sult is bigger than zero, which means potential motion artifacts, otherwise no motion
artifacts. The MATLAB code for out-of-sample error is given here:
1 wt = [ 0 . 2 ; −0.044; 0 . 1815488 ; 0 . 2 ] ;
2 SampleResult = [ ] ;
3
4 f o r k = 1 : 1 : l ength ( RRinterval )
5 temp = wt (1) ∗1.0+wt (2 ) ∗( RRinterval ( k ) )+
6 wt (3) ∗( RRenergy ( k ) )+wt (4) ∗( SQnoise ( k ) ) ;
7 i f temp>0
8 f o r i = 1 : 1 : RRsize
9 SampleResult = [ SampleResult , 1 ] ;
Page 40
30
10 end
11 e l s e
12 f o r i = 1 : 1 : RRsize
13 SampleResult = [ SampleResult , 0 ] ;
14 end
15 end
16 end
After calculation, the out-of-sample error is around 4.36%, which means the fea-
tures extracted works well for motion artifacts detection.
3.4.4 Making data more generic
In previous section, the inital testing data used to apply machine-learning algorithms
are from our hardware board and several our group testing results, which has limited
data diversity. In order to make the machine learning results more accurate and
generic, we should include more data from different identities, like people with healthy
hearts and with arrhythmias. In this case, online ECG database is helpful to provide
diverse ECG data.
In this project, the online database we mainly referred to is called MIT-BIH Ar-
rhythmia Database [5]. We downloaded several different arrhythmias ECG data,
which includes: Left/Right bundle branch block beats, Atrial premature beats, Aber-
rated atrial premature beats, Fusion of ventricular and normal beats, and Normal
sinus rhythm. Then, we process these data with the help of MATLAB and collected
their features values and recorded into the CSV file mentioned above.
Finally, the whole data size of our recording is 3153, which includes motion arti-
facts data around 800.
3.5 Performance analysis and cross validation
3.5.1 Performance features sensitivity and specificity
Sensitivity and specificity [9] are two common terms used to evaluate a clinical test.
They are statistical measures of the performance of a binary classification test.
• Sensitivity (also called the true positive rate) measures the proportion of pos-
itives that are correctly identified as such (e.g., the percentage of sick people
Page 41
31
who are correctly identified as having the condition).
• Specificity (also called the true negative rate) measures the proportion of nega-
tives that are correctly identified as such (e.g., the percentage of healthy people
who are correctly identified as not having the condition).
There is usually a trade-off between the measures. In general, the higher the
sensitivity, the lower the specificity, and vice versa.
The sensitivity of a clinical test refers to the ability of the test to correctly identify
those patients with the disease.
Sensitivity =True positives
True positives + False negatives(3.2)
A test with 100% sensitivity correctly identifies all patients with the disease. A
test with 80% sensitivity detects 80% of patients with the disease (true positives) but
20% with the disease go undetected (false negatives). A high sensitivity is clearly
important where the test is used to identify a serious but treatable disease (e.g.
cervical cancer).
Specificity of a clinical test refers to the ability of the test to correctly identify
those patients without the disease.
Specificity =True negatives
True negatives + False positives(3.3)
Therefore, a test with 100% specificity correctly identifies all patients without the
disease. A test with 80% specificity correctly reports 80% of patients without the
disease as test negative (true negatives) but 20% patients without the disease are
incorrectly identified as test positive (false positives).
As discussed above, a test with a high sensitivity but low specificity results in
many patients who are disease free being told of the possibility that they have the
disease and are then subject to further investigation. For clinical diseases, this kind
of results will be more helpful. However, in this project, high specificity and relatively
low sensitivity are desirable, which means some motion artifacts signal may be missed
but most of normal ECG signal will be kept. This approach is conservative but more
safe proof because it can largely eliminate the possibility that some arrhythmias are
detected as motion artifacts due to its irregular shape.
Page 42
32
Figure 3.12: Beat1.
Next, we applied the total generic data and Pocket algorithm to obtain sensitivity
and specificity parameters. The results are as following:
Average sensitivity : 0.7821
Average specificity : 0.9675
From the results, the specificity is high but sensitivity is a bit low, which meets
our requirements. Then we tried to figure out the reasons causing relatively low
sensitivity. We used MATLAB to plot the signal and compare the machine learning
results with the actual ECG signal. After several tests, we found that if the motion
artifacts were not obvious, which means the QRS pattern can still be recognized in
one beat, the algorithm probably regarded it as normal beat. Fig. 3.12 and Fig. 3.13
described this condition.
The beats in red circles have slightly motion artifacts resulting in some irregular
shapes, but they are misclassified as normal beats. Thus, one of the main reasons
for low sensitivity is slightly polluted motion artifacts signal. However, this misclas-
sification would not largely affect detection accuracy in that slightly noise could be
eliminated by our digital filters.
3.5.2 Cross validation introduction
Overfitting [21] is an important issue to understand and address in machine learning:
it is a phenomenon where good fitting of training data does not necessarily imply
satisfactory generalization. And cross validation is a useful technique for estimating
Page 43
33
Figure 3.13: Beat2.
out-of-sample error.
Roughly speaking, overfitting refers to fitting the data more than is warranted.
The main case of overfitting is when the learning model yields a hypothesis with a
low in-sample error but a high out-of-sample error. Overfitting generally occurs when
noise or random error is involved. Here is an example of overfitting.
In Fig. 3.14, it describes a function in black line and its 15 noisy data points, which
deviated from its original function due to random noise interferences. Figure 3.15 [18]
shows the 10th order and 2nd order fits separately. It is obvious that 10th order fit
curve fits the noisy points much better than 2nd order, which also produces a smaller
in-sample error. However, the 2nd order can be expected to generalize better and has
a better prediction.
Here, we apply cross validation technique [15] to estimate out-of-sample error.
The idea of doing cross validation is as follows:
Given a data set C = {(xn, yn), n = 1, 2, ..., N}, we partition it into a training set
of size N − K and a validation set of size K. As expected, training set is used in
learning process like before, except that its size is reduced from N to N−K. The role
of validation set is nearly the same as a test set in the sense that it is independent
of the training data and it is used to compute a validation error Eval(g). Thus, the
Page 44
34
Figure 3.14: 10th-order target function and 15 noisy data points.
Table 3.1: Cross validation of Pocket AlgorithmPart 20% 20% 20% 20% 20%
In-sample error 0.0718 0.0758 0.0694 0.0722 0.0710Out-of-sample 0.0746 0.0603 0.0825 0.0714 0.0825
Sensitivity 0.8027 0.8129 0.7792 0.7674 0.7482Specificity 0.9627 0.9756 0.9622 0.9701 0.9671
estimation of the out-of-sample error is:
Eval(g) =1
K
K∑n=1
e(g(xn), yn) (3.4)
where e(g(xn), yn) represents |g(xn)− yn|. In common, a rule of thumb is to set 20%
of the data (i.e. K = N/5) for validation.
Cross Validation of Pocket Algorithm
Following the procedures described above, we partition the whole data set into 5
subsets. The results are recorded in Table 3.1.
Page 45
35
Figure 3.15: 10th order fit (red curve) versus 2nd order fit (red curve)[18].
wt1 = [−0.2; 0.416054; 1.3156068; 0.6]
wt2 = [−0.2; 0.18695; 0.6831308; 0.2]
wt3 = [−0.2; 0.185028; 0.7744444; 0.2]
wt4 = [−0.4; 0.459082; 1.2509806; 0.4]
wt5 = [0; 0.105012; 1.051255; 0.4]
Average out-of-sample error:7.462%
Average sensitivity:0.7821
Average specificity:0.9675
From the results above, we can see that the average out-of-sample error is around
7%, which shows the effectiveness and accuracy of extracted features and the weights
calculated from this machine-learning algorithm.
Cross Validation of SVM
SVM is a popular machine learning algorithm. Here, we use MATLAB built-in SVM
tool to apply cross validation in order to test the algorithms’s performance under
different machine learning methods. The MATLAB code of this part is as following:
1 k=10;
Page 46
36
2 cvFolds = c r o s s v a l i n d ( ’ Kfold ’ , Y, k ) ; %# get i n d i c e s o f 10−f o l d CV
3 cp = c l a s s p e r f (Y) ; %# i n i t performance
t r a c ke r
4
5 f o r i = 1 : k %# f o r each f o l d
6 t e s t I d x = ( cvFolds == i ) ; %# get i n d i c e s o f t e s t
i n s t a n c e s
7 t r a i n Idx = ˜ t e s t I d x ; %# get i n d i c e s t r a i n i n g
i n s t a n c e s
8 %# t r a i n an SVM model over t r a i n i n g i n s t a n c e s
9 svmModel = svmtrain (X( t ra in Idx , : ) , Y( t r a in Idx ) ) ;
10 %# t e s t us ing t e s t i n s t a n c e s
11 pred = s v m c l a s s i f y ( svmModel , X( te s t Idx , : ) ) ;
12 %# eva luate and update performance ob j e c t
13 cp = c l a s s p e r f ( cp , pred , t e s t I d x ) ;
14 end
15
16 %# get accuracy
17 cp . CorrectRate
18 %# get con fus i on matrix
19 %# columns : actua l , rows : pred ic ted , l a s t−row : u n c l a s s i f i e d
i n s t a n c e s
20 cp . CountingMatrix
We can choose different kernels to test and the results are as following:
1. Linear Kernel:
Average correct rate: 0.9236
Average error rate: 0.0764 = 7.64%
2. Quadratic Kernel:
Average correct rate: 0.9267
Average error rate: 0.0733 = 7.33%
3. rbf Kernel:
Average correct rate: 0.9204
Page 47
37
Average error rate: 0.0796 = 7.96%
From the results above, we can observe that the average correct rates, i.e., accuracy
are closed to each other under different kernels. And the accuracy is about 92%, which
represents the effectiveness of these features.
3.5.3 Summary
In this chapter, the defination of motion artifacts and the approaches to apply machine
learning algorithms are presented in details. The results from in-sample error and out-
of-sample error that are lower than 0.1 can show the effectiveness of three selected
features. In order to verify the accuracy of the results, cross validation is used to
test out-of-sample errors and another machine learning algorithm - SVM is applied
to compare. Besides, we use two performance features: sensitivity and specificity
to evaluate its trade-off. The results with a high sensitivity and low specificity are
secure, conservative and meet our requirements.
Page 48
38
Chapter 4
Java Implementation
The ultimate goal of this ECG signal-processing project is to implement motion ar-
tifacts identification and QRS detection algorithms into our group developed ECG
Android application, which can be connected with the hardware board to gather ECG
signal and process the signal in real time to generate an ECG report. Therefore, there
are mainly two aspects to be considered in order to achieve this function.
Firstly, we need to convert the previous MATLAB code into Java code and testify
the accuracy of algorithms and results. Here, we have to consider the computing
accuracy differences between MATLAB and Java. Also, the previous MATLAB build-
in functions need to be replaced in Java program. Secondly, since these detection
algorithms must be integrated into the whole Android App, the encapsulation of this
signal processing module is important. The module encapsulation can let us modify
the single module later easily without impacting the whole application. In this case,
the APIs are key parts for program inner communication and data transmission.
Thus, we need to design proper APIs for this signal processing module to ensure the
encapsulation and communication.
4.1 MATLAB to Java conversion
The challenge for this part is how to properly convert the build-in functions that have
been used in MATLAB code. For example, in the initial signal processing part in
Chapter 2, the build-in band-pass filter is applied to eliminate noise and interference.
And build-in moving average filter is used for smoothing the raw signal in order to
improve the detection accuracy. In the following section, the Java version of build-in
Page 49
39
functions is presented in details.
A. Band-pass filter design
In MATLAB code, the build-in Butterworth Band-pass filter is used in the signal-
processing step. The function to implement Butterworth filter is as follows.
[B,A] = butter(N,Wn) (4.1)
Which designs an Nth order band-pass digital Butterworth filter and returns the
filter coefficients in length N + 1 vectors B (numerator) and A (denominator). The
coefficients are listed in descending powers of z.
After obtaining the coefficients of filter, we use build-in filter function to realize
the function of digital filter.
Y = filter(B,A,X) (4.2)
Which filters the data in vector X with the filter described by vector A and B to
create the filtered data Y . The filter actually is an implementation of the standard
difference equation:
a(1) ∗ y(n) =b(1) ∗ x(n) + b(2) ∗ x(n− 1) + ...+ b(nb+ 1) ∗ x(n− nb)
− a(2) ∗ y(n− 1)− ...− a(na+ 1) ∗ y(n− na) (4.3)
The MATLAB code for this band-pass filter is simple as the code described below:
1 %Noise c a n c e l a t i o n ( F i l t e r i n g )
2 f 1 =0.5 ; %c u t t o f f low frequency to get r i d o f b a s e l i n e wander
3 f 2 =45; %c u t t o f f f r equency to d i s ca rd high f requency no i s e
4 Wn=[ f1 f2 ]∗2/ f s ; % cutt o f f based on f s
5 N = 3 ; % order o f 3 l e s s p r o c e s s i n g
6 [ a , b ] = butte r (N,Wn) ; %bandpass f i l t e r i n g
7 ecg = f i l t e r ( a , b , ecg ) ;
However, there are no such build-in filter functions in Java library, so we need to
design our own digital filters to replace them and make sure the similar performance.
From Digital Signal Processing aspect, finite impulse response (FIR) filters are easy
to design and implement via programming. Fig. 4.1 [1] describes the basic idea of
Page 50
40
Figure 4.1: FIR filter realization [1].
FIR filter. For this FIR filter of order N, each output value is a weighted sum of the
most recent input values. The equation below describes the implementation of this
FIR filter. The next step is to obtain the weighted value of this N order FIR filter.
y[n] = b0x[n] + b1x[n− 1] + · · ·+ bNx[n−N ]
=N∑i=0
bi · x[n− i] (4.4)
From the Digital Signal Processing course, we can easily design the band-pass
filter given certain bandwidth, filter order and sampling frequency. The code below
is the MATLAB code used to replace the build-in band-pass filter. After lots of
examination, we decide to use N= 21 order FIR filter, which has similar performance
as the build-in one.
1 %bandpass f i l t e r%%%
2 f p a r a = [ 0 . 5∗2 / f s 45∗2/ f s ] ;
3 N=21;
4 M = (N−1) /2 ;
5 n = 0 :M−1;
6
7 % Generate impulse re sponse o f the i d e a l f i l t e r
8 omec1 = pi ∗ f p a r a (1 ) ;
9 omec2 = pi ∗ f p a r a (2 ) ;
10 hd = ( s i n ( ( n−M)∗omec2 ) − s i n ( ( n−M)∗omec1 ) ) . / ( ( n−M)∗ pi ) ;
11 hd = [ hd ( omec2−omec1 ) / p i f l i p l r (hd ) ] ;
Page 51
41
12 %w type == 2 , % Hamming
13 w = 0.54 − 0 .46∗ cos ( p i ∗n/M) ;
14 w = [w 1 f l i p l r (w) ] ;
15
16 % Obtain impulse re sponse
17 h = w.∗ hd ;
18
19 e c g l e n=length ( ecg ) ;
20 e c g f=ecg ;
21 f o r n=(N−1) : e cg l en−1
22 ecg=ecg (n+1) :−1:(n+1−(N−1) ) ) ;
23 e c g f (n+1)=h∗ ecgn ;
24 end
25
26 e c g f=e c g f (N: ecg l en−N) ;
27 ecg=e c g f
B. Moving average filter design
The functionality of moving average filter is to make the signal smoother, so that
the accuracy of QRS detection would be improved. The MATLAB code for moving
average filter is as following:
1 %%%moving window averag ing f i l t e r
2 Nw=3;
3 B=1/Nw∗ones (Nw, 1 ) ;
4 ecg=f i l t e r (B, 1 , ecg ) ;
Where Nw represents the number of points will be averaged. The idea of this filter
is quite straightforward - replace the original point values with the average of its N
surrounding points. The replaced MATLAB code is as following:
1 %moving average f i l t e r in order 3
2 ecg new = [ ecg (1 ) ] ;
3 f o r i = 2 : 1 : l ength ( ecg )−1
4 ecg new = ( ecg ( i −1)+ecg ( i )+ecg ( i +1) ) /3 ;
5 end
Page 52
42
The implementation of above code is quite simple in Java, thus we can get rid of the
MATLAB build-in filter( ) function.
4.2 Algorithm structure in Java
In the section, the general structure of this ECG signal-processing project will be
introduced, which include the connection/interface between each algorithm and the
communication between each algorithm.
Figure 4.2: General structure of ECG signal processing module in Java Application.
In Fig. 4.2, we use flow chart to describe the procedure and basic logic of this
ECG signal processing module. Firstly, the raw signal will pass into the Motion
Artifacts Detection module, which applies machine learning results to detect motion
artifacts signal. If the ratio of detected motion artifacts signal is greater than 50%,
Page 53
43
this sequence of signal is regarded too noisy and give a warning signal instead of
passing to ECG Parameters Extraction module. Otherwise, a list of beat marks will
be generated to represent motion artifacts signal, which then will be passed to ECG
Parameters Extraction module to assit parameters calculation. Finally, with the help
of beat marks, ECG Parameters Extraction module can generate the ECG report
based on the parameters average values.
4.3 Java APIs design
In Java, each function model should be implemented in a Class. And in class, we
can realize module encapsulation and design the interfaces for other programs to call.
In this project, we design two classes to achieve different functions. From previous
chapter, we know that there are two processing units: Motion Artifacts Detection and
ECG Parameters Extraction unit. Therefore, we define two classes MADetec.java
and ECGDetect.java, which include motion artifacts detection algorithm and ECG
parameters extraction algorithm respectively.
Firstly, in order to make these algorithms function well, there must be an API to
let raw ECG data pass into the MADetect class. Therefore, there is a public function
named read( ) in MADetect class to read the raw data in. Then, after processing and
calculation, the results with marks (1-motion artifacts, 0- no motion artifacts) need
to be passed to the second class ECGDetect for further processing. Another public
method marks( ) can be called to return the list of motion artifacts marks. Finally,
the results from ECGDetect should be obtained by a displaying class, so that the
results can be displayed properly in the app. We design five public methods for the
displaying class to call to obtain the paramters values. Fig. 4.3 shows the APIs.
4.4 Summary
In this chapter, we mainly discuss the challenges we met to apply the algorithms into
Java application and the methods we use to conquer these problems. Besides, the
general structure of ECG signal processing system is presented to further explain the
relation and communication between each module. Finally, the APIs are introduceds
with details.
Page 54
44
Figure 4.3: Java APIs design and details.
Page 55
45
Chapter 5
Conclusions
5.1 Conclusion
This project focused on the design and implementation of an ECG signal processing
system for our groups ECG project. Unlike current portable devices in the market,
which could only display the heart rate, our ECG parameters extraction module
provides the essential information about users heart condition that could be used
for potential heart disease detection. One of the advantages of this module is that
it realized real-time detection. The motion artifacts detection module guarantees
the accuracy of ECG parameters extraction module, so that our ECG device could
be used during users daily activity. Besides, it creatively applies machine-learning
algorithms for motion artifacts detection and achieves good performance. In order to
support the Android application and realize module encapsulation, the module APIs
are carefully designed for data transmission and results output. The encapsulation of
these modules guarantees the stability and scalability of the whole project, and also
it would be easier to update these two modules in the future.
5.2 Future work
Currently, the machine learning results come from MATLAB training and then need
to be input to the motion artifacts module manually. Further improvement is needed
to make updates automatically according to new results from data training when
updating the App. The initial idea is that the sever keeps training the data when new
ECG data enters into the database and updates the weights from machine learning
Page 56
46
process. The weights could be saved in a separate file (e.g., txt file), and update this
file each time when updating the App. In this case, the motion artifacts module could
read from this file and update the weight automatically.
Page 57
47
Bibliography
[1] Finite impulse response. https://en.wikipedia.org/wiki/Finite_impulse_
response. Wikipedia.
[2] Heart rate monitor production - Fitbit sports band. https://www.fitbit.com/
ca/flex. Fitbit.
[3] Holter monitor product. http://arrhythmiacenter.idsutcliffe.com/Pt%
20Resources%20Dx%20of%20Arrhythmias.htm. Valley Regional Arrhythmia
Center, Inc.
[4] Ischemia and infarction. https://en.wikipedia.org/wiki/
Electrocardiography#Ischemia_and_infarction. Wikipedia.
[5] MIT-BIH Arrhythmia Database. https://www.physionet.org/physiobank/
database/mitdb/. PhysioNet.
[6] QRS complex. https://en.wikipedia.org/wiki/QRS_complex#/media/File:
QRS_complex.png. Wikipedia.
[7] T wave definition. http://en.ecgpedia.org/index.php?title=Conduction.
ECGpedia.
[8] Ethem Alpaydin. Introduction to machine learning. MIT press, 2014.
[9] Douglas G Altman and J Martin Bland. Diagnostic tests. 1: Sensitivity and
specificity. BMJ: British Medical Journal, 308(6943):1552, 1994.
[10] Dixit V Bhoraniya and Rahul K Kher. Motion artifacts extraction using DWT
from ambulatory ECG (A-ECG). 1:1567–1571, 2014.
[11] Avrim L Blum and Pat Langley. Selection of relevant features and examples in
machine learning. Artificial intelligence, 97(1):245–271, 1997.
Page 58
48
[12] Joseph Brownfield and Mel Herbert. ECG Criteria for Fibrinolysis: Whats Up
with the J Point? Western Journal of Emergency Medicine, 9(1), 2008.
[13] Michael R Gold et al. The effect of QRS duration and morphology on car-
diac resynchronization therapy outcomes in mild heart failure: results from the
REsynchronization reVErses Remodeling in Systolic left vEntricular dysfunction
(REVERSE) Study. Circulation, page 112, 2012.
[14] Stephen I Gallant. Perceptron-based learning algorithms. IEEE Transactions on
Neural Networks, 1(2):179–191, 1990.
[15] Gene H Golub, Michael Heath, and Grace Wahba. Generalized cross-validation
as a method for choosing a good ridge parameter. Technometrics, 21(2):215–223,
1979.
[16] Hilma Holm, Daniel F Gudbjartsson, David O Arnar, Gudmar Thorleifsson,
Gudmundur Thorgeirsson, Hrafnhildur Stefansdottir, Sigurjon A Gudjonsson,
Aslaug Jonasdottir, Ellisiv B Mathiesen, Inger Njolstad, et al. Several common
variants modulate heart rate, pr interval and qrs duration. Nature genetics,
42(2):117–122, 2010.
[17] Do-Un Jeong and Se-Jin Kim. Development of a technique for cancelling motion
artifact in ambulatory ECG monitoring system. 1:954–961, 2008.
[18] Wusheng Lu. Machine Learning for Signal processing lecture notes - Regularized
least-squares polynomial fitting. University of Victoria, 2015.
[19] Jiapu Pan and Willis J Tompkins. A real-time QRS detection algorithm. IEEE
Transactions on Biomedical Engineering, (3):230–236, 1985.
[20] Gopi Krishna Panicker, Dilip R Karnad, Mili Natekar, Snehal Kothari, Dhiraj
Narula, and Yash Lokhandwala. Intra-and interreader variability in QT interval
measurement by tangent and threshold methods in a central electrocardiogram
laboratory. Journal of electrocardiology, 42(4):348–352, 2009.
[21] Juha Reunanen. Overfitting in making comparisons between variable selection
methods. The Journal of Machine Learning Research, 3:1371–1382, 2003.
[22] Tomas Robyns, Rik Willems, Joris Ector, Hein Heidbuchel, Stefan Janssens, and
Dieter Nuyens. Individualized corrected QT interval improves accuracy to predict
Page 59
49
mutation carriage in LQTS families compared to Bazetts formula. Circulation,
130(2):A13751–A13751, 2014.
[23] Deboleena Sadhukhan and Madhuchhanda Mitra. Detection of ECG charac-
teristic features using slope thresholding and relative magnitude comparison.
1:122–126, 2012.
[24] Stephen W Smith, Ayesha Khalil, Timothy D Henry, Melissa Rosas, Richard J
Chang, Kimberly Heller, Erik Scharrer, Mina Ghorashi, and Lesly A Pearce.
Electrocardiographic differentiation of early repolarization from subtle anterior
ST-segment elevation myocardial infarction. Annals of emergency medicine,
60(1):45–56, 2012.
[25] MJ Vidya and Shruthi Sadasiv. A Comparative Study on Removal of Noise in
ECG Signal Using Different Filters. International Journal of Innovative Research
and Development, 2(4):915–927, 2013.
[26] John G Webster. Reducing motion artifacts and interference in biopotential
recording. IEEE Transactions on Biomedical Engineering, (12):823–826, 1984.