7  Peak Detection: the Pan-Tompkins Algorithm

Both heart rate and HRV start from detecting R waves (or pulse waves). In this chapter we study the Pan-Tompkins algorithm, published in 1985 and still used as a reference method for QRS detection, by visualizing it stage by stage.

7.1 Why a simple threshold does not work

A simple rule such as “an R wave occurs when the ECG exceeds a fixed value” breaks down quickly.

  • When baseline wander moves the whole signal up and down, beats sometimes exceed the threshold and sometimes do not
  • In people with tall T waves, T waves are mistaken for R waves
  • Spikes from noise (EMG, motion) are falsely detected
  • Depending on the lead, electrode placement and body build, R-wave amplitude varies between people and over time

The Pan-Tompkins algorithm addresses these problems with preprocessing that emphasizes the “shape features” of the QRS complex (steep, with a nearly constant width) and an adaptive threshold that follows the signal amplitude.

7.2 Algorithm overview

flowchart LR
  A["ECG"] --> B["① Band-pass<br/>5–15 Hz"]
  B --> C["② Derivative<br/>emphasize slope"]
  C --> D["③ Squaring<br/>make positive,<br/>emphasize large slopes"]
  D --> E["④ Moving-window integration<br/>150 ms"]
  E --> F["⑤ Adaptive threshold<br/>+ refractory period 200 ms<br/>+ T-wave discrimination"]
  F --> G["⑥ Locate R-wave peak<br/>on the original signal"]

Stage Purpose
① Band-pass 5–15 Hz Keep the band where QRS energy is concentrated; reduce baseline wander and T waves (low frequency) and EMG and power-line noise (high frequency)
② Derivative Emphasize the steep slopes of the QRS. T and P waves have gentle slopes, so they become small
③ Squaring Make both upward and downward QRS complexes positive and further emphasize large slopes
④ Moving-window integration (150 ms) Average over a window about the width of a QRS, turning each “QRS-like lump of energy” into a single hump
⑤ Adaptive threshold Successively update the signal level (SPKI) and noise level (NPKI) from the heights of detected humps; threshold = NPKI + 0.25 × (SPKI − NPKI)
Refractory period 200 ms Physiologically, the next QRS cannot occur within 200 ms
T-wave discrimination A candidate within 360 ms of the previous QRS is regarded as a T wave if its slope is less than half that of the previous QRS

The original algorithm also includes other steps, such as “searching again with a lower threshold when no QRS is found for a certain time (search-back)”, which are omitted here.

7.3 Demo: signals at each stage and detection results

This is a 20-second synthetic ECG sampled at 200 Hz (the same as the original paper). Because the true R-wave positions are known, detection accuracy can be evaluated.

○ True R wave ● Detected R wave - - Adaptive threshold

TipTry it
  1. With the defaults, compare the shape of the signal at each stage. How much smaller does the T wave become at stage ② (derivative)?
  2. Confirm that detection does not break down even with baseline wander at 2 mV (it is removed by the band-pass in ①)
  3. Set the T-wave height to 1.2 mV and toggle “Use T-wave discrimination” on and off
  4. If you raise the EMG noise to 0.3 mV, at which stage do QRS complexes and noise become indistinguishable?
  5. Add some motion spikes. Can this algorithm distinguish spikes that are as steep as a QRS?
  6. At a heart rate of 180 bpm, how do the T-wave discrimination (360 ms) and the refractory period (200 ms) behave?
  1. The T wave has a much gentler slope than the QRS, so differentiation and squaring greatly widen the ratio between them. This is the central idea of the Pan-Tompkins algorithm.
  2. A tall T wave still produces a sizeable hump after moving-window integration and may exceed the threshold. T-wave discrimination (within 360 ms and slope less than half) prevents this.
  3. EMG also has components in the 5–15 Hz band, so band-pass filtering cannot remove it completely, and it gets emphasized along with the QRS by differentiation and squaring.
  4. Steep motion spikes resemble the QRS in their “shape features”, so this method is prone to false detections. Practical algorithms deal with this by combining multiple leads, template matching of QRS morphology, using an accelerometer, and so on.
  5. At 180 bpm the RR interval is about 333 ms, so the next QRS falls inside the 360 ms T-wave discrimination window. Because of the slope condition this rarely causes major problems, but algorithms with fixed time constants need to be validated over the intended heart-rate range (children, during exercise, etc.).

7.4 Evaluating detection accuracy

To evaluate an R-wave detection algorithm, detections are compared with reference R-wave positions (annotated by experts); a detection counts as a true positive if it falls within a tolerance window (e.g. ±75–150 ms).

\mathrm{Se} = \frac{\mathrm{TP}}{\mathrm{TP} + \mathrm{FN}}, \qquad \mathrm{PPV} = \frac{\mathrm{TP}}{\mathrm{TP} + \mathrm{FP}}

  • The MIT-BIH Arrhythmia Database (PhysioNet) is the standard public database. The original Pan-Tompkins paper reported a detection rate of over 99% on this database
  • For HRV analysis, not only whether beats are detected but also the timing accuracy of R waves (error in ms) must be evaluated

7.5 Peak detection in the pulse wave (PPG)

PPG differs from the ECG in that

  • the waveform is smoother than a QRS and the peak position is ambiguous → the onset (foot) or the point of maximum slope is often used as the fiducial point
  • the waveform changes with heart rate, and the dicrotic notch (a small bump due to aortic valve closure) is easily misdetected
  • the influence of motion is far greater

so different algorithms are used than for the ECG. There are also methods that estimate heart rate from spectral peaks in the frequency domain (for example, removing motion components by comparing with the accelerometer spectrum).

7.6 Summary

  • Pan-Tompkins = band-pass → derivative → squaring → moving-window integration → adaptive threshold
  • Preprocessing emphasizes the “steepness” and “width” of the QRS, and the adaptive threshold copes with inter-individual and temporal variation in amplitude
  • The refractory period and T-wave discrimination remove physiologically impossible detections
  • Evaluate with sensitivity (Se) and positive predictive value (PPV); for HRV, timing accuracy as well