Visualise the classic Pan-Tompkins algorithm for extracting R-peaks (the apex of the QRS complex) from an electrocardiogram. Move the sliders for heart rate, sampling frequency, noise level, bandpass corners and threshold to see detection sensitivity, PPV and processing latency update in real time, and build intuition for Holter, ICU and wearable ECG design.
Parameters
Heart rate HR
BPM
Beats per minute. 30-200 covers rest to exercise
Sampling Fs
Hz
ADC sampling frequency. ≥500 Hz is standard for diagnostic ECG
Noise level
%
Additive noise as a percentage of signal amplitude (mains hum + EMG)
Bandpass lower fL
Hz
Bandpass upper fH
Hz
QRS spectral peak lives in 5-15 Hz (standard)
MWI window
ms
Moving-average window over the squared signal. 150 ms is standard
Threshold factor
factor in THRESHOLD = NPK + factor·(SPK − NPK)
Results
—
RR interval (ms)
—
Samples / beat
—
Detection latency (ms)
—
SNR (dB)
—
Sensitivity (%)
—
PPV (%)
—
ECG waveform & Pan-Tompkins pipeline
Top: synthetic ECG (P-QRS-T plus noise). Bottom: bandpass → derivative → squaring → MWI output. Red dots mark detected R-peaks; the dashed line is the adaptive threshold.
SNR is the signal-to-noise amplitude ratio in dB. Detection latency t_lat is the sum of the bandpass, derivative and half-MWI group delays. Fs: sampling frequency.
ECG QRS detection — the Pan-Tompkins algorithm
🙋
An ECG machine measures the electrical waves of the heart, right? When it goes "beep, beep" what part of the waveform is it actually detecting?
🎓
Yes — and the part it picks out is the "R-peak", the tallest spike of the waveform. Each beat has a P wave (atrial depolarisation), a QRS complex (ventricular depolarisation) and a T wave (repolarisation); of those, the R-peak — the apex of the QRS — is the largest and most stable feature. So measuring heart rate, judging arrhythmias, everything starts with locating the R-peaks.
🙋
But a real ECG is wobbly and has muscle noise, mains hum and all that. Can't I just put a threshold at "the highest point"?
🎓
Sharp question. A fixed threshold misfires the instant the patient moves — a noise spike looks like an R-peak — and if an electrode peels off and the amplitude drops, real R-peaks slip below it. That is exactly why Pan and Tompkins published, in 1985, a 5-stage pipeline: (1) bandpass → (2) derivative → (3) squaring → (4) moving-window integration → (5) adaptive threshold. It hit 99.3% sensitivity on the MIT-BIH database, and it is still the first algorithm in every signal-processing textbook.
🙋
Does every stage have a job? Why "differentiate then square", in particular?
🎓
Every stage has a job. Bandpass 5-15 Hz covers the QRS spectral peak — it strips low-frequency P/T waves and high-frequency EMG/mains at the same time. The derivative extracts the slope of the waveform, so only the steep R-peak upstroke ends up large. Squaring rectifies the sign and turns the trace into "energy" with a non-linear emphasis on big values. Finally moving-window integration (MWI, 150 ms) merges that energy into a single hump that a threshold can grab cleanly.
🙋
What's the difference between "sensitivity" and "PPV" on the right? Both show about 99%.
🎓
Sensitivity is "of the true R-peaks, what fraction did we catch" (low miss rate). PPV is "of the peaks we reported, what fraction are real" (low false alarm rate). Dropping the threshold factor catches more peaks → sensitivity ↑ but also more noise → PPV ↓. Raising it does the opposite. The goal is ≥99% on both, which the Pan-Tompkins defaults reach at clinical ~10% noise. In an ICU or during exercise, engineers typically push the factor to 0.5-0.6 to keep false alarms down.
🙋
It says "detection latency 91 ms" — that affects real-time use, right?
🎓
It does. The FIR group delay of the bandpass and derivative, plus half the MWI window (75 ms when MWI = 150 ms), add up to 80-100 ms between the actual R-peak and the detection event. So for AEDs or pacemakers that have to fire or sync instantly, designers either shrink the MWI window to ~100 ms or run a separate fast detector alongside. Holter post-analysis does not care about latency at all — engineering choices depend entirely on what the device is for.
Frequently Asked Questions
Pan-Tompkins is a classical real-time QRS detection algorithm published in 1985 by Jiapu Pan and Willis J. Tompkins. It is built as a five-stage pipeline — bandpass filter (5-15 Hz) → derivative → squaring → moving-window integration (MWI) → adaptive threshold — and reaches 99.3% sensitivity on the MIT-BIH arrhythmia database. Because it is light to implement, it is still widely used in the firmware of Holter recorders and wearable ECG devices.
The QRS spectral peak sits around 10 Hz, and most of its energy is contained between 5 and 15 Hz. P and T waves live at 1-7 Hz, EMG noise above 20 Hz, and mains hum at 50/60 Hz. Restricting the passband to 5-15 Hz suppresses the non-QRS ECG components and noise at the same time, shaping a signal in which the R-peak is the dominant feature. Lowering the lower corner lets T waves in and triggers false positives; raising the upper corner lets EMG noise drop the PPV.
The standard width is 150 ms. That is slightly wider than the adult QRS duration (typically 80-120 ms) and is optimised to merge the squared energy into a single hump. A shorter window splits the peak in two and counts one beat as two; a longer window merges adjacent beats and misses peaks at high heart rates. Use about 100 ms for paediatric or tachycardic recordings, and up to 200 ms for wide QRS (bundle-branch block, etc.).
Pan-Tompkins maintains running signal-peak and noise-peak averages SPK and NPK and uses THRESHOLD = NPK + factor·(SPK − NPK) as a dynamic decision level. A small factor (0.2-0.3) raises sensitivity but lowers PPV because more noise is admitted; a larger factor does the opposite. 0.4 is the standard, 0.5-0.6 is common in noisy ICU settings, and some wearable implementations drop it to 0.25 during quiet rest. In this tool, lowering the factor raises sensitivity and lowers PPV — the expected trade-off.
Real-world applications
Holter (24-hour) monitors: Outpatient recorders capture 24-48 hours of ECG to spot arrhythmias and ST changes. Pan-Tompkins is the de facto industry standard in this space — it is the basis of the front-end detector in Mortara and Nihon Kohden products, among others. A single day contains roughly 100,000 beats, so even at 99% sensitivity and 99% PPV you are looking at ~1,000 missed or false beats, which is where detection accuracy directly shapes diagnostic quality.
ICU continuous monitoring and telemetry: Bedside monitors derive heart rate and alarm logic from Pan-Tompkins-style detectors. Patient motion, electrosurgery and defibrillator interference are constant, so threshold factors are kept high to suppress false alarms. Multi-lead Pan-Tompkins, fusing detections across leads, has become widespread as a remedy for alarm fatigue.
Wearable ECG and smartwatches: Apple Watch, Fitbit and OMRON HeartGuide-class devices typically run a simplified Pan-Tompkins detector under the hood. Dry electrodes, short records and motion noise are the challenges, so engineers widen the bandpass upper corner to 20-25 Hz and re-learn the threshold frequently. Pushing the noise slider to 30-40% in this tool reproduces the wearable-class regime, in which sensitivity and PPV both drop noticeably.
ICD and pacemaker sensing logic: Implantable cardioverter defibrillators (ICDs) have to detect ventricular fibrillation within 5-10 seconds and deliver a shock. Latency directly affects survival, so designers usually combine a simpler derivative-plus-threshold detector with R-R interval analysis, keeping total latency under 3 seconds. Shrink the MWI window to 50 ms in this simulator to feel the ~25 ms latency that ICD-class fast detectors operate at.
Common misconceptions and pitfalls
The biggest pitfall is treating "99% sensitivity" as a single sufficient number. Clinically, what matters is "how many arrhythmias did we miss in 24 hours" and "how many false alarms did we just hand the nurses" — and even 99% sensitivity is ~1,000 missed beats per Holter day. Worse, missing a premature ventricular contraction (PVC) and missing ventricular fibrillation (VF) are orders of magnitude apart in clinical severity. Algorithm evaluations have to break down sensitivity by arrhythmia sub-class, not just report a global QRS detection score.
Next, assuming "higher sampling frequency = better detection". IEC 60601-2-25 recommends ≥500 Hz for diagnostic ECG, but pushing Fs to 2000 Hz barely moves Pan-Tompkins accuracy. If anything, larger Fs amplifies high-frequency noise after the derivative and makes the bandpass upper corner harder to design. Wearables that need low power deliver clinically sufficient sensitivity at 250 Hz, because "Fs only needs to be 8-10× the QRS spectrum (~40 Hz)" is the right engineering rule.
Finally, the false dichotomy "Pan-Tompkins is obsolete, switch to machine learning". Modern CNN/LSTM QRS detectors do reach 99.9% sensitivity on MIT-BIH, but they cost more in inference compute, power, model-update governance and FDA regulatory paperwork. Field deployments increasingly use a hybrid — "Pan-Tompkins for 99%, deep learning only on the difficult records". Before jumping to ML, you still need to set the bandpass, MWI window and threshold factor correctly.
How to Use
Set heart rate (40-200 bpm) and sampling frequency (250-1000 Hz) using the numeric inputs or range sliders
Adjust noise level (0-50 mV) and bandpass filter cutoff (5-40 Hz) to simulate real ECG signal conditions
Observe R-peak detection markers on the waveform; verify sensitivity (%), PPV (%), and detection latency (ms) against your threshold criteria
Examine RR interval consistency in milliseconds and SNR (dB) to validate algorithm robustness
Worked Example
Configure a 72 bpm patient signal at 500 Hz sampling with 10 mV noise and 5 Hz highpass filter. Pan-Tompkins algorithm produces: RR interval 833 ms (accurate), detection latency 45 ms post-R-peak, sensitivity 98.5%, PPV 99.2%, SNR 18 dB. Increasing noise to 30 mV degrades sensitivity to 94.1% and SNR to 12 dB, demonstrating filter effectiveness in clinical noise rejection (muscle artifact, baseline wander).
Practical Notes
Sampling frequency must exceed 200 Hz Nyquist criterion for QRS complex (60-100 Hz bandwidth); 500 Hz offers safety margin for morphology preservation