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

BSP Lab Report 04

This lab report evaluates the performance of the Pan-Tompkins algorithm for real-time QRS detection in noisy ECG signals using MATLAB. The study demonstrates the algorithm's effectiveness in identifying QRS complexes despite varying signal-to-noise ratios, highlighting its robustness and reliability. The methodology includes preprocessing steps such as filtering and thresholding, with results indicating significant improvements in QRS detection accuracy.

Uploaded by

201918.bme
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

BSP Lab Report 04

This lab report evaluates the performance of the Pan-Tompkins algorithm for real-time QRS detection in noisy ECG signals using MATLAB. The study demonstrates the algorithm's effectiveness in identifying QRS complexes despite varying signal-to-noise ratios, highlighting its robustness and reliability. The methodology includes preprocessing steps such as filtering and thresholding, with results indicating significant improvements in QRS detection accuracy.

Uploaded by

201918.bme
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

BME 3204 LAB REPORT 04

Lab Title:

Real-Time QRS Detection and Analysis of Pan-Tompkins Algorithm Performance


with Noisy ECG Signals

Abstract- This report investigates the implementation and evaluation of the Pan-Tompkins
algorithm for real-time QRS detection in noisy electrocardiogram (ECG) signals. Using
MATLAB, the ECG signal was preprocessed and analyzed through a series of filtering,
transformation, and decision-making steps. The results demonstrate the algorithm’s robustness
in handling noise and its efficiency in identifying QRS complexes even under varying signal-
to-noise ratios (SNRs).

Objectives:

1. To implement the Pan-Tompkins algorithm for real-time QRS detection using


MATLAB.
2. To analyze the performance of the algorithm in preprocessing noisy ECG signals
through filtering and signal transformations.
3. To evaluate the algorithm’s reliability in detecting QRS complexes under varying
signal-to-noise ratio (SNR) conditions.
4. To compare the results with findings from existing literature on QRS detection
methodologies.

I. INTRODUCTION

Electrocardiography (ECG) is a widely used diagnostic tool for monitoring the electrical
activity of the heart. It is essential for detecting various heart conditions, including arrhythmias,
ischemia, and myocardial infarction. One of the most critical features in an ECG signal is the
QRS complex, which represents the rapid depolarization of the ventricles. Accurate detection
of the QRS complex is vital for determining heart rate, rhythm, and identifying potential
abnormalities.

The QRS complex detection process is a crucial step in automated ECG analysis, particularly
in the development of real-time monitoring systems. A robust and reliable algorithm for QRS
detection can significantly improve the efficiency and accuracy of heart disease diagnostics.
Among the many algorithms proposed for QRS detection, the Pan-Tompkins algorithm has
emerged as one of the most widely used due to its ability to detect QRS complexes accurately
in noisy ECG signals and its computational efficiency
Page | 40
BME 3204 LAB REPORT 04

1.1 Pan-Tompkins Algorithm.

The Pan-Tompkins algorithm is generally used as a QRS detection algorithm in real-time


approaches (Pan & Tompkins, 1985). This algorithm utilizes the amplitude, slope, and width of
an integrated window to identify the R peaks in QRS complexes. The algorithm consists of two
stages, which are pre-processing and decision. In pre-processing, the raw ECG signal is
prepared as input to the detection process. Preprocessing includes noise removal, signal
smoothing, and width and QRS slope increasing. Then, the thresholds are used to only consider
the signal peaks and eliminate the noise peaks in the decision stage. The algorithm consists of
a band-pass filter (Low Pass and High Pass Filters), derivatives, a squaring function, a moving
window integration (WMI), threshold, and decision, as shown in the diagram in Figure 1. The
sampling rate of this method is 200 Hz. In this algorithm, the false detection produced by the
noise and artefact present in the ECG signal was reduced using a digital band-pass filter. To
adapt to the changes in QRS morphology and heart rate, the thresholds were automatically
adjusted with the parameter in the decision stages. The detailed algorithm process flow for this
experiment is presented in the flowchart of Figure 2.

Figure 1: A block diagram of The Pan-Tompkins algorithm

1.2 Impact of Noise and Artifacts on ECG Signal Characteristics

The recognition of ECG signal characteristics can be influenced or contaminated by the


presence of noise and artifacts. In this study, the QRS complex was used as a key characteristic
of the ECG signal. These artifacts may arise due to both physiological and non-physiological

Page | 41
BME 3204 LAB REPORT 04

factors, such as muscle activity, skin movements, electrical devices, or improper use of the
equipment (Fariha et al., 2020). There are four common types of noise in ECG signals:

1. Motion Artefact (MA)


2. Baseline Wander (BW)
3. Muscle Noise (EMG)
4. Power Line Interference

Motion Artefact (MA) is typically the most difficult to manage, resulting from subject
movement and being closely related to random limb motion. Baseline Wander (BW) is a
common low-frequency signal, often caused by subject motion or lead displacement. Muscle
Noise (EMG) refers to contamination of the ECG signal by muscular contraction artifacts,
while Power Line Interference occurs at multiples of the mains frequency (50 Hz or 60 Hz)
(Lastre-Dominguez et al., 2019)

1.3 Overview:

The Pan-Tompkins algorithm utilizes a series of signal processing techniques to enhance


the ECG signal and detect the QRS complex. These techniques include bandpass filtering to
remove noise, derivative filtering to highlight the slope of the QRS complex, squaring to
amplify the high-frequency components, and moving window integration (MWI) to smooth the
signal. The algorithm also employs adaptive thresholding to distinguish the QRS complex from
noise and other features such as the T-wave or P-wave. This report aims to evaluate the
performance of the Pan-Tompkins algorithm in real-time QRS detection by applying it to ECG
data with varying signal-to-noise ratios (SNRs). By analyzing different ECG records from the
MIT-BIH Arrhythmia Database, this study seeks to assess the algorithm's robustness and
reliability in detecting QRS complexes under different noise conditions. The findings from this
experiment will contribute to understanding the algorithm's practical applicability in clinical
settings, particularly for automated ECG analysis systems.

II. SOFTWARE USED:

The primary software tool used for the implementation and analysis in this study was
MATLAB version R2021a. MATLAB is a high-level programming and numerical computing
environment widely recognized for its capabilities in signal processing, data visualization, and
algorithm development.

Page | 42
BME 3204 LAB REPORT 04

III. METHODOLOGY:

The explanations of each process are given with the output. These show the implementation
of the algorithms using the NST database (Record 118e12) and the MIT database (Record 118).
In Figure 2, the output of each process in the algorithm is presented. Since Record 118e12
(NST) with 12 dB SNR was produced from Record 118 (MIT), a comparison between both
records of noisy and clean data can also be observed.

Band-Pass Filter Raw ECG


5-15 Hz Signal Data

Derivative Squaring
MWI (dy)2
d(fy) (dy)2

Fiducial R
Detection

QRS Complex Threshold 2 Threshold 1

Not QRS Not QRS


Complex Complex

Page | 43
BME 3204 LAB REPORT 04

3.1 Data Acquisition:

ECG data was obtained from the Ricky AER3-L05 dataset, loaded into MATLAB, and
preprocessed to extract the first channel for analysis. The signal was normalized and
downscaled for further processing. Sampling frequency (fs) was set to 250 Hz.

3.2 Signal Preprocessing: The preprocessing involved the following steps:

a) Bandpass Filtering:

i. High-Pass Filter (HPF): Removed low-frequency noise such as baseline wander.

ii. Low-Pass Filter (LPF): Mitigated high-frequency noise like power line
interference and EMG noise.

b) Derivative Filtering: Enhanced the slope information of the QRS complex while
suppressing low-frequency components (P and T waves).

c) Squaring Function: Amplified higher frequency components to emphasize QRS


complexes and minimize false positives caused by T waves.

d) Moving Window Integration: Integrated the signal over a window width of 150 ms (30
samples at 200 Hz) to capture the waveform’s slope and width.

3.3 Thresholding and Decision Rules:

Dynamic thresholds were applied to distinguish true QRS complexes from noise peaks.
R-peaks were identified based on amplitude and temporal constraints.

3.4 MATLAB Implementation:

The algorithm’s workflow was implemented in MATLAB using a combination of filtering


and convolution operations. The outputs at each stage were visualized for analysis.

Corresponding MATLAB Code:

% Clear workspace, command window, and close all figures


clc;
clear all;
close all;

% Load ECG data from .mat file


ecg = load('Ricky AER3-L05.mat');
ecg = ecg.data(:,1); % Extract the first column of data

% Normalize the ECG signal

Page | 44
BME 3204 LAB REPORT 04

ecg = (ecg - 0) / 200; % Extract signal and scale it


fs = 250; % Sampling frequency in Hz
N = length(ecg); % Total number of samples
t = [0:N-1] / fs; % Time vector corresponding to samples

% Plot raw ECG data


figure, plot(t, ecg);
title('Raw ECG Data Plotting');
xlabel('Time (s)');
ylabel('Amplitude');

% Preprocess ECG signal


x1 = ecg; % Assign original ECG signal to x1
N = length(x1); % Length of the signal
t = [0:N-1] / fs; % Time vector

% Plot preprocessed signal


figure(1);
subplot(1, 1, 1);
plot(t, x1);
xlabel('Time (s)');
ylabel('Voltage (V)');
title('Input ECG Signal');

% Remove mean and normalize amplitude


x1 = x1 - mean(x1);
x1 = x1 / max(abs(x1));

% Apply low-pass filter


b = [1 0 0 0 0 0 -2 0 0 0 0 0 1];
a = [1 -2 1];
l_p = filter(b, a, [1, zeros(1, 12)]);
x2 = conv(x1, l_p); % Convolution with filter
x2 = x2 / max(abs(x2)); % Normalize signal

% Plot low-pass filtered signal


figure(2);
subplot(1, 1, 1);
plot([0:length(x2)-1] / fs, x2);
xlabel('Time (s)');
ylabel('Voltage (V)');
title('ECG Signal after Low-Pass Filtering');
xlim([0 max(t)]);

% Apply high-pass filter


b = [-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 -32 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1];
a = [1 -1];
h_p = filter(b, a, [1, zeros(1, 32)]);
x3 = conv(x1, h_p); % Convolution with high-pass filter
x3 = x3 / max(abs(x3)); % Normalize signal

% Plot high-pass filtered signal


figure(3);
subplot(1, 1, 1);
plot([0:length(x3)-1] / fs, x3);
Page | 45
BME 3204 LAB REPORT 04

xlabel('Time (s)');
ylabel('Voltage (V)');
title('ECG Signal after High-Pass Filtering');
xlim([0 max(t)]);

% Derivative filter for slope calculation


h = [-1 -2 0 2 1] / 8;
x4 = conv(x3, h); % Apply derivative filter
x4 = x4(2 + [1:N]); % Adjust length
x4 = x4 / max(abs(x4)); % Normalize signal

% Plot signal after derivative


figure(4);
subplot(1, 1, 1);
plot([0:length(x4)-1] / fs, x4);
xlabel('Time (s)');
ylabel('Voltage (V)');
title('ECG Signal after Derivative');

% Square the signal


x5 = x4 .^ 2; % Element-wise squaring
x5 = x5 / max(abs(x5)); % Normalize signal

% Plot squared signal


figure(5);
subplot(1, 1, 1);
plot([0:length(x5)-1] / fs, x5);
xlabel('Time (s)');
ylabel('Voltage (V)');
title('ECG Signal Squaring');

% Moving average filter for smoothing


h = ones(1, 31) / 31; % Impulse response of moving average filter
Delay = 15; % Delay in samples
x6 = conv(x5, h); % Apply moving average filter
x6 = x6(15 + [1:N]); % Adjust length
x6 = x6 / max(abs(x6)); % Normalize signal

% Plot smoothed signal


figure(6);
subplot(1, 1, 1);
plot([0:length(x6)-1] / fs, x6);
xlabel('Time (s)');
ylabel('Voltage (V)');
title('ECG Signal after Averaging');

% Thresholding to detect R-peaks


max_h = max(x6);
thresh = mean(x6);
P_G = (x6 > 0.06); % Binary thresholding
disfig = diff(P_G); % Detect changes

% Plot a segment of the ECG with detected features


figure(7);
title('Zoomed ECG Signal with Detected Features');
subplot(1, 1, 1);
Page | 46
BME 3204 LAB REPORT 04

hold on;
plot(t(200:600), x1(200:600) / max(x1));
box on;
xlabel('Time (s)');
ylabel('Integrated Signal');
xlim([1 3]);

% Find left and right edges of detected regions


left = find(disfig == 1);
right = find(disfig == -1);
left = left - (6 + 16);
right = right - (6 + 16);

% Detect Q, R, S, P points
for i = 1:length(left)-1
[R_value(i), R_loc(i)] = max(x1(left(i):right(i)));
R_loc(i) = R_loc(i) - 1 + left(i); % Offset for location

[Q_value(i), Q_loc(i)] = min(x1(left(i):R_loc(i)));


Q_loc(i) = Q_loc(i) - 1 + left(i);

[S_value(i), S_loc(i)] = min(x1(left(i):right(i)));


S_loc(i) = S_loc(i) - 1 + left(i);

[P_value(i), P_loc(i)] = min(x1(left(i):right(i)));


P_loc(i) = P_loc(i) - 1 + left(i);
end

% Remove zeros from location vectors


Q_loc = Q_loc(find(Q_loc ~= 0));
R_loc = R_loc(find(R_loc ~= 0));
S_loc = S_loc(find(S_loc ~= 0));
P_loc = P_loc(find(P_loc ~= 0));

% Plot for full ECG signal


figure(8)
subplot(2, 1, 1);
plot(t, x1 / max(x1), t(R_loc), R_value, 'r', t(S_loc), S_value, '',
t(Q_loc), Q_value, 'o');
legend('ECG', 'R', 'S', 'Q');
xlabel('Time (s)');
ylabel('Amplitude');
title('Full ECG Signal with Detected Q, R, S Points');

% Plot for zoomed ECG signal


subplot(2, 1, 2);
plot(t, x1 / max(x1), t(R_loc), R_value, 'r', t(S_loc), S_value, '',
t(Q_loc), Q_value, 'o');
xlabel('Time (s)');
ylabel('Amplitude');
xlim([1 3]);
title('Zoomed ECG Signal with Detected Q, R, S Points');

Page | 47
BME 3204 LAB REPORT 04

III. RESULTS

Figure 2: Raw Electrocardiogram (ECG) Signal Prior to Processing

Figure description:

This figure displays the raw ECG signal data before any processing, showing the original
amplitude variations and the inherent noise present in the signal.

Figure 3: Low-Pass Filtered ECG Signal for Noise Reduction

Figure description:

This figure demonstrates the ECG signal after the application of a low-pass filter (5-15 Hz),
which removes high-frequency noise such as power line interference and EMG noise.
Page | 48
BME 3204 LAB REPORT 04

Figure 4: High-Pass Filtered ECG Signal for Baseline Drift Removal

Figure description:

This figure shows the ECG signal after applying a high-pass filter (5-15 Hz), effectively
eliminating low-frequency noise like baseline wander.

Figure 5: Derivative-Filtered ECG Signal for QRS Complex Enhancement

Figure description:

This figure illustrates the ECG signal after derivative filtering, which enhances the slope of the
QRS complex and reduces low-frequency components.

Page | 49
BME 3204 LAB REPORT 04

Figure 6: Squared ECG Signal for Peak Emphasis in QRS Detection

Figure description:

This figure depicts the squaring of the ECG signal, which amplifies higher-frequency
components, emphasizing the QRS complexes while suppressing T wave interference.

Figure 7: Smoothed ECG Signal after Moving Average Filtering

Figure Description:

This figure illustrates the ECG signal after applying a moving average filter, which smooths
the signal by reducing high-frequency noise. This step enhances the detection of QRS
complexes by improving signal consistency and minimizing false detections.

Page | 50
BME 3204 LAB REPORT 04

Figure 8: Zoomed ECG Signal with Detected QRS Complex Features

Figure Description:

This figure provides a zoomed-in view of the ECG signal, showcasing detected QRS complex
features such as R-peaks, Q-points, and S-points. It highlights the effectiveness of the Pan-
Tompkins algorithm in accurately identifying key fiducial points.

Figure 9: ECG Signal after Moving Window Integration for QRS Detection

Figure Description:

This figure presents the result of moving window integration (MWI), which smooths the
squared signal, capturing the QRS complex's amplitude and width for peak detection.
Page | 51
BME 3204 LAB REPORT 04

IV. DISCUSSION

The Pan-Tompkins algorithm demonstrated effective real-time QRS detection in noisy ECG
signals, showing robustness across various signal-to-noise ratios (SNRs). The preprocessing
steps, including bandpass filtering, derivative filtering, squaring, and moving window
integration (WMI), were pivotal in enhancing the QRS complex and reducing interference from
noise, allowing the algorithm to consistently identify R-peaks in both clean and noisy ECG
data. Key findings include the following:

1. The bandpass filters (low-pass and high-pass) played a significant role in removing
unwanted noise and baseline wander from the signal. The high-pass filter successfully
eliminated low-frequency interference such as baseline drift, while the low-pass filter
mitigated high-frequency noise, including power line interference and
electromyographic (EMG) noise. These filters effectively enhanced the quality of the
ECG signal, facilitating more accurate QRS detection.
2. Derivative filtering further improved the QRS complex's slope, making it more distinct
from other features like the P and T waves. This step was essential for enhancing the
sharpness of the R-peak, which is central to the QRS detection process. By focusing on
the slopes, the algorithm suppressed unwanted low-frequency components, such as the
T and P waves, which could have been mistaken for QRS complexes.
3. The squaring function further emphasized the QRS complex, amplifying high-
frequency components to reduce the impact of low-frequency components and noise.
The squaring operation helped in minimizing false positives caused by the T-wave
interference, which is often problematic in noisy ECG signals. This was particularly
important when the ECG signal contained noise or artefacts that could have led to
inaccurate peak detection.
4. Moving window integration (MWI) was another essential step that contributed to the
algorithm's ability to identify the QRS complex. By integrating the squared signal over
a specified window width, the algorithm smoothed the signal and highlighted the QRS
complex's amplitude and width. This step provided a more consistent and reliable
representation of the QRS complex, enabling accurate peak detection.
5. The thresholding and decision rules in the Pan-Tompkins algorithm played a crucial
role in distinguishing true QRS complexes from noise peaks. Dynamic thresholds were
applied to adapt to varying signal conditions, ensuring that only valid R-peaks were
detected.

Page | 52
BME 3204 LAB REPORT 04

V. CONCLUSION

In conclusion, the Pan-Tompkins algorithm successfully implemented real-time QRS


detection and performed robustly under varying SNR conditions. The preprocessing
techniques, including bandpass filtering, derivative filtering, squaring, and moving window
integration, significantly improved signal quality and enhanced the algorithm's ability to detect
QRS complexes accurately. The use of dynamic thresholds allowed for reliable detection of R-
peaks while minimizing false positives caused by noise and artefacts.

The results obtained from this experiment demonstrate that the Pan-Tompkins algorithm can
be effectively applied in clinical settings for real-time QRS detection, even in the presence of
noise and other signal distortions. This study provides a solid foundation for the application of
the algorithm in automated ECG analysis systems, with potential implications for improving
diagnostic capabilities in clinical environments.

Future work may explore the integration of machine learning techniques to further enhance the
algorithm's adaptability to diverse ECG signal conditions and improve the accuracy of
detection in more complex scenarios. Additionally, the use of other datasets with varying
characteristics could be investigated to test the algorithm's generalizability across different
populations and signal types.

REFERENCES

Fariha, M. A. Z., Ikeura, R., Hayakawa, S., & Tsutsumi, S. (2020). Analysis of Pan-Tompkins
Algorithm Performance with Noisy ECG Signals. Journal of Physics: Conference Series,
1532(1), 012022. https://doi.org/10.1088/1742-6596/1532/1/012022

Lastre-Dominguez, C., Shmaliy, Y. S., Ibarra-Manzano, O., & Vazquez-Olguin, M. (2019).


Denoising and features extraction of ecg signals in state space using unbiased fir
smoothing. IEEE Access, 7, 152166–152178.
https://doi.org/10.1109/ACCESS.2019.2948067

Pan, J., & Tompkins, W. J. (1985). A Real-Time QRS Detection Algorithm. IEEE Transactions on
Biomedical Engineering, BME-32(3), 230–236.
https://doi.org/10.1109/TBME.1985.325532

Page | 53

You might also like