pan–tompkins algorithm pan–tompkins algorithm the pan–tompkins algorithm is commonly used to detect qrs complexes in electrocardiographic signals (ecg). the qrs complex represents the ventricular depolarization and the main spike visible in an ecg signal (see figure). this feature makes it particularly suitable for measuring heart rate, the first way to assess the heart health state. in the first derivation of einthoven of a physiological heart, the qrs complex is composed by a downward deflection (q wave), a high upward deflection (r wave) and a final downward deflection (s wave). ecg beat the pan–tompkins algorithm applies a series of filters to highlight the frequency content of this rapid heart depolarization and removes the background noise. then, it squares the signal to amplify the qrs contribution, which makes identifying the qrs complex more straightforward. finally, it applies adaptive thresholds to detect the peaks of the filtered signal. the algorithm was proposed by jiapu pan and willis j. tompkins in 1985, in the journal ieee transactions on biomedical engineering. the performance of the method was tested on an annotated arrhythmia database (mit/bih) and evaluated also in presence of noise. pan and tompkins reported that the 99.3 percent of qrs complexes was correctly detected. pre-processing block diagram of the pre-processing phase of the pan–tompkins algorithm. noise cancellation as a first step, a band-pass filter is applied to increase the signal-to-noise ratio. a filter bandwidth of 5-15 hz is suggested to maximize the qrs contribute and reduce muscle noise, baseline wander, powerline interference and the p wave/t wave frequency content. in the original algorithm proposed in 1985, the band-pass filter was obtained with a low-pass filter and a high-pass filter in cascade to reduce the computational cost and allow a real-time detection, while ensuring a 3 db passband in the 5–12 hz frequency range, reasonably close to the design goal. for a signal sampled at a frequency of 200 hz, pan and tompkins suggested the filters with the following transfer functions h ( z ) in an updated version of their article: h ( z ) = ( 1 − z − 6 ) 2 ( 1 − z − 1 ) 2 )^ \over (1-z^)^ for a second-order low-pass filter with a gain of 36 and a processing delay of 5 samples; h ( z ) = ( − 1 / 32 + z − 16 − z − 17 + z − 32 / 32 ) ( 1 − z − 1 ) -z^+z^/32) \over (1-z^)}} for a high-pass filter with a unity gain and a processing delay of 16 samples. derivative step as a third step, a derivative filter is applied to provide information about the slope of the qrs. for a signal sampled at 200 hz, pan and tompkins suggested the following transfer function: h ( z ) = 0.1 ( − z − 2 − 2 z − 1 + 2 z 1 + z 2 ) -2z^+2z^+z^)} for a 5-point derivative filter with gain of 0.1 and a processing delay of 2 samples. squaring and integration the filtered signal is squared to enhance the dominant peaks (qrss) and reduce the possibility of erroneously recognizing a t wave as an r peak. then, a moving average filter is applied to provide information about the duration of the qrs complex. the number of samples to average is chosen in order to average on windows of 150 ms. the signal so obtained is called integrated signal. decision rules example of pan–tompkins processing. fiducial mark in order to detect a qrs complex, the local peaks of the integrated signal are found. a peak is defined as the point in which the signal changes direction (from an increasing direction to a decreasing direction). after each peak, no peak can be detected in the next 200 ms (ie. the lockout time). this is a physiological constraint due to the refractory period during which ventricular depolarization cannot occur even in the presence of a stimulus. thresholds each fiducial mark is considered as a potential qrs. to reduce the possibility of wrongly selecting a noise peak as a qrs, each peak amplitude is compared to a threshold (thresholdi) that takes into account the available information about already detected qrs and the noise level: t h r e s h o l d i = n o i s e l e v e l i + 0.25 ( s i g n a l l e v e l i − n o i s e l e v e l i ) =noiselevel_+0.25(signallevel_-noiselevel_)} where noiseleveli is the running estimate of the noise level in the integrated signal and signalleveli is the running estimate of the signal level in the integrated signal. the threshold is automatically updated after detecting a new peak, based on its classification as signal or noise peak: s i g n a l l e v e l i = 0.125 p e a k i + 0.875 s i g n a l l e v e l i =0.125peak_+0.875signallevel_ (if peaki is a signal peak) n o i s e l e v e l i = 0.125 p e a k i + 0.875 n o i s e l e v e l i =0.125peak_+0.875noiselevel_ (if peaki is a noise peak) where peaki is the new peak found in the integrated signal. at the beginning of the qrs detection, a 2 seconds learning phase is needed to initialize signalleveli and noiseleveli as a percentage of the maximum and average amplitude of the integrated signal, respectively. if a new peaki is under the thresholdi, the noise level is updated. if peaki is above the thresholdi, the algorithm implements a further check before confirming the peak as a true qrs, taking into consideration the information provided by the bandpass filtered signal. in the filtered signal the peak corresponding to the one evaluated on the integrated signal is searched and compared with a threshold, calculated in a similar way to the previous step: t h r e s h o l d f = n o i s e l e v e l f + 0.25 ( s i g n a l l e v e l f − n o i s e l e v e l f ) =noiselevel_+0.25(signallevel_-noiselevel_)} s i g n a l l e v e l f = 0.125 p e a k f + 0.875 s i g n a l l e v e l f =0.125peak_+0.875signallevel_ (if peakf is a signal peak) n o i s e l e v e l f = 0.125 p e a k f + 0.875 n o i s e l e v e l f =0.125peak_+0.875noiselevel_ (if peakf is a noise peak) where the final f stands for filtered signal. search back for missed qrs complexes the algorithm takes into account the possibility of setting too high values of thresholdii and thresholdif. a check is performed to continuously assess the rr intervals (namely the temporal interval between two consecutively qrs peaks) to overcome this issue. the average rr is computed in two ways to consider both regular and irregular heart rhythm. in the first method rraverage1 is computed as the mean of the last rr intervals. in the second method rraverage2 is computed as the mean of the last rr intervals that fell between the limits specified as: r r l o w = 92 % r r a v e r a g e 2 r r h i g h = 116 % r r a v e r a g e 2 if no qrs is detected in a window of 166% of the average rr (rraverage1 or rraverage2, if the heart rhythm is regular or irregular, respectively), the algorithm adds the maximal peak in the window as a potential qrs and classify it considering half the values of the thresholds (both thresholdii and thresholdif). this check is implemented because the temporal distance between two consecutive beats cannot physiologically change more quickly than this. t wave discrimination the algorithm takes particularly into consideration the possibility of a false detection of t waves. if a potential qrs falls up to a 160 ms window after the refractory periody from the last correctly detected qrs complex, the algorithm evaluates if it could be a t wave with particular high amplitude. in this case, its slope is compared to the one of the precedent qrs complex. if the slope is less than half the previous one, the current qrs is recognized as a t wave and discarded, and it also updates the noiselevel (both in the filtered signal and the integrated signal). application once the qrs complex is successfully recognized, the heart rate is computed as a function of the distance in seconds between two consecutive qrs complexes (or r peaks): h r ( bpm ) = 60 r r ( s ) \ ()=\ ()}} where bpm stands for beats per minute. the hr is often used to compute the heart rate variability (hrv) a measure of the variability of the time interval between heartbeats. hrv is often used in the clinical field to diagnose and monitor pathological conditions and their treatment, but also in the affective computing research to study new methods to assess the emotional state of people.