Matlab Implementation of Pan Tompkins ECG QRS Detector.: 1 Background
Matlab Implementation of Pan Tompkins ECG QRS Detector.: 1 Background
Detector.
Hooman Sedghamiz
March, 2014
Abstract
This open source code is a complete matlab implementation of Pan Tompkins ECG
QRS detector. This work implements all of the main steps proposed in the original
algorithm. A preliminary comparison of the MIT-BIH indicated similar sensitivity and
accuracy between this implementation and the original proposed work.
1 Background
Pan and Tompkins proposed an accurate real-time ECG beat detector in 1985 that has been
successfully used in many commercial devices [1]. The algorithm was originally implemented
on a microprocessor and later in C programming language [2]. This work implements the
same algorithm in Matlab environment with the hope that it can be more easily accessible
to researchers in the field of biomedical engineering.
2 Method
2.1 Preprocessing:
Steps:
3. Signal is squared.
4. Signal is averaged (MWI) to remove high frequency noise (0.150 seconds length).
5. Depending on the sampling frequency of your signal the filtering, options above are
changed to best match the characteristics of the ECG signal
1
2.2 Decision Rule
At this point in the algorithm, previous steps have generated a roughly pulse-shaped wave-
form at the output of the signal. The decision as to whether a pulse corresponds to a QRS
complex (as opposed to a high-sloped T-wave or a noise artefact) is performed with an
adaptive thresholding operation and other decision rules outlined below;
• FIDUCIAL MARK - The waveform is first processed to produce a set of weighted
unit samples at the location of the MWI maxima. This is done in order to localize the
QRS complex to a single instant of time.
• THRESHOLDING - When analyzing the amplitude of the MWI output, the algo-
rithm uses two threshold values (T HRSIG and T HRN OISE , appropriately initialized
during a brief 2 second training phase) that continuously adapt to changing ECG sig-
nal quality. The first pass through y[n] uses these thresholds to classify the each non-
zero sample (CU RREN T P EAK) as either signal or noise: If CU RREN T P EAK >
T HRS IG, that location is identified as a QRS complex candidate and the signal level
(SIGLEV ) is updated: SIGLEV = 0.125 × CU RREN T P EAK + 0.875 × SIGLEV
If T HRN OISE < CU RREN T P EAK < T HRSIG , then that location is identified as
a noise peak and the noise level (N OISELEV ) is updated: N OISELEV = 0.125 ×
CU RREN T P EAK + 0.875 × N OISELEV . Based on new estimates of the signal
and noise levels (SIGLEV and N OISELEV , respectively) at that point in the ECG,
the thresholds are adjusted as follows: T HRSIG = N OISELEV + 0.25 × (SIGLEV −
N OISELEV ); T HRN OISE = 0.5 × T HRSIG ;These adjustments lower the threshold
gradually in signal segments that are deemed to be of poorer quality.
• SEARCHBACK FOR MISSED QRS COMPLEXES - In the thresholding step
above, if CU RREN T P EAK < T HRS IG, the peak is deemed not to have resulted
from a QRS complex. If however, an unreasonably long period has expired without an
abovethreshold peak, the algorithm will assume a QRS has been missed and perform
a searchback. This limits the number of false negatives. The minimum time used to
trigger a searchback is 1.66 times the current R peak to R peak time period (called the
RR interval). This value has a physiological origin - the time value between adjacent
heartbeats cannot change more quickly than this. The missed QRS complex is assumed
to occur at the location of the highest peak in the interval that lies between T HRS IG
and T HRN OISE. In this algorithm, two average RR intervals are stored,the first RR
interval is calculated as an average of the last eight QRS locations in order to adapt to
changing heart rate and the second RR interval mean is the mean of the most regular
RR intervals . The threshold is lowered if the heart rate is not regular to improve
detection.
• ELIMINATION OF MULTIPLE DETECTIONS WITHIN REFRACTORY
PERIOD - It is impossible for a legitimate QRS complex to occur if it lies within
200ms after a previously detected one. This constraint is a physiological one due to
the refractory period during which ventricular depolarization cannot occur despite a
stimulus [1]. As QRS complex candidates are generated, the algorithm eliminates such
physically impossible events, thereby reducing false positives.
2
• T WAVE DISCRIMINATION - Finally, if a QRS candidate occurs after the 200ms
refractory period but within 360ms of the previous QRS, the algorithm determines
whether this is a genuine QRS complex of the next heartbeat or an abnormally promi-
nent T wave. This decision is based on the mean slope of the waveform at that position.
A slope of less than one half that of the previous QRS complex is consistent with the
slower changing behaviour of a T wave otherwise, it becomes a QRS detection. Extra
concepts: beside the points mentioned in the paper, this code also checks if the occured
peak which is less than 360 msec latency has also a latency less than 0, 5 ∗ meanR R if
yes this is counted as noise!
References
[1] J. Pan and W. J. Tompkins, “A Real-Time QRS Detection Algorithm,” IEEE Transac-
tions on Biomedical Engineering, vol. BME-32, no. 3, pp. 230–236, 1985.