EKG QRS Detection Algorithm

About This Notebook

This notebook uses Mathematica to implement the QRS detection algorithm as described in Tompkins (1993). The ability to detect the QRS complex is highly important for a variety of clinical applications, in particular, the arrythmia detectors and  the Holter tape analysis.  In this notebook, a short introduction to electrocardiography is given in the following steps.  First, the recorded EKG is imported and displayed. Second, the functions for low- and high-pass digital filters are implemented and applied to the signal.  Third, the differentiating and integrating functions are implemented and applied. Finally, the QRS complex is detected and displayed.

Introduction to Electrocardiography

An electrocardiogram is a recording of the electrical activity of the heart.The heart can be viewed as a three-dimensional vector. Therefore, its electrical activity can, in theory, be recorded by three orthogonal leads. In practice, however, a standard clinical EKG is recorded with 12 leads: 6 limb leads and 6 precordical leads.

A normal EKG reflects the electrical activity of each of the four heart chambers: left and right ventricles and left and right atria (Fig. 1).

The P wave marks the beginning of atrial depolarization. The onset of the Q wave is produced by the stimulus from the Purkinje system. The depolarization of the ventricle produces the R wave. The S wave is initiated when the excitation reaches the base of the ventricle. The T wave is produced by ventricular repolarization (Tompkins 1993; Goldman 1992).

The frequency range for the signal is 0.05 - 100Hz,  and the signal range is 10μV  (fetal) and 5 mV (adult).

Introduction to the Algorithm

The power spectrum of a recorded EKG (Fig. 2) contains a variety of components. There are the complexes of the cardiosignal:  QRS,  P wave, and T wave. Also,  there are a number of noise artifacts, such as muscle artifacts, the 60Hz powerline interference,  and the noise from motions and other electronic instrumentation (Goldman 1992).

The real-time QRS detection algorithm described in Goldman contains four main steps:

Step 1: The bandpass integer filter isolates the QRS energy in 5-15Hz and attenuates the artifacts and P and T waves.  It is implemented as a cascade of a low-pass filter (the cutoff frequency ~11Hz) and a high-pass filter (the low cutoff frequency ~5Hz).
Step 2: The derivative provides information about the slope of the QRS complex.
Step 3: The squaring function makes all data positive and emphasizes the higher frequencies.
Step 4: The moving window integral extracts additional features of the QRS complex.

Implementation of the Algorithm

Importing the EKG Signal and the `QRSdetection Package

The signal (Tompkins 1993) is sampled at 200 samples per second (amplitude 100 = 0.5 mV; the sample #150 = 0.75 second [the distance between samples is 5msec]).

Bandpass Integer Filter

Low-pass Filter

Here is the transfer function of the 2nd order low-pass filter.

(Subscript[H, lp])(z) =((((1 - (z^(-6))))^2)/(((1 - (z^(-1))))^2))

Here is the difference equation of the filter.

\(TraditionalForm
    \`\(y\_lp\)(n\ T)\  = \ 
      2\ \(y(n\ T\  - \ T)\)\  - \ y(\ n\ T\  - \ 2\ T)\  + \ x(n\ T)\  - \ 
        2\ \(x(n\ T\  - \ 6\ T)\)\  + \ x(n\ T\  - \ 12\ T)\)

The filter has a cutoff frequency of approximatelly 11Hz, and the gain is 36.

Conclusion: The result is attenuation of musle noise and 60Hz powerline interference.

High-pass filter

The high-pass filter is implemented by substracting a first-order low-pass filter from an all-pass filter with delay.  Here is its transfer function.

\(TraditionalForm
    \`\(H\_hp\)(z) = 
      z\^\(-16\) - \(1 - z\^\(-32\)\)\/\(32 \((1 - z\^\(-1\))\)\)\)

Here is its difference equation.

\(TraditionalForm
    \`\(y\_hp\)(n\ T) = \ 
      x(n\ T - 16  T) - 
        \(1\/32\)[y(n\ T\  - \ T)\  + \ x(n\ T)\  - \ x(n\ T\  - 32\ T)]\)

The low frequency cutoff of the filter is appproxmately 5Hz and the gain is 1.

Conclusion: The result is attenuation of the T-wave.

Derivative

Differentiation will provide information about the slope of the QRS complex.

\(TraditionalForm
    \`\(H\_deriv\)(z)\  = \ 
      0.1\ \((\ 2\  + \ z\^\(-1\) - \ z\^\(-3\)\  - \ 2\ z\^\(-4\))\)\)

This derivative is implemented with the following difference equation.

\(TraditionalForm
    \`\(y\_deriv\)(n\ T)\  = \ 
      \(1\/8\)[2\ \(x(n\ T)\)\  + \ x(n\ T\  - \ T)\  - \ 
          x(n\ T\  - \ 3\ T)\  - \ 2\ \(x(n\ T\  - \ 4  T)\)]\)

Conclusion: The derivative further enhances the QRS complex.

Squaring the Signal

Here is the difference equation for the squaring function.

(Subscript[y, square])(n T)  =  (([x(n T)])^2)

Conclusion: Squaring makes all samples positive and enhances the higher frequencies of the signal.

Moving Window Integrator

In case of some abnormalities with QRS complex, the slope of the R wave might not be sufficient criteria to detect the QRS complex. The integration provided additional information. It has the following difference equation:

\(TraditionalForm
    \`\(y\_ntegral\)(n\ T)\  = \ 
      1\/N[\(x(n\ T\  - \ \((N\  - \ 1)\)\ T)\  + \ 
                x(n\ T\  - \ \((N\  - \ 2)\)\ T)\  + \  ... \)\  + x(n\ T)]
          \)

where the number N is the number of samples in the moving window. It has to be chosen depending on the sampling rate and the maximum possible width of the QRS complex. In our case, the rate of 200 samples per second, it is 30.

The QRSdetect Function

The QRSdetect function is the composition of all the functions of the algorithm.

Conclusion: The width of the QRS complex is equal to the width of the segment of the output with the increase of the amplitude (approximately 20 samples in this example).  Note that the result of the algorithm shifted right due to delays in the inner functions.  This is consistent with the goals of the algorithms (which are to detect the QRS complex, show its length and the distance between the complexes). If the algorithm is applied to a signal with multiple QRS complexes, all detected complexes will have the same shift, which will keep the distance between them constant.

Conclusion

The QRS detection algorithm was succesfully implemented in Mathematica. The functions of the low- and high-pass filters and differentiating, squaring, and integrating functions were implemented. The example of the application was given.

References

Tompkins, Willis J., ed. 1993. Biomedical Digital Signal Processing. Englewood Cliffs, N.J.: Prentice Hall. 
Goldman, Mervin J. 1992. The Principles of Clinical Electrocardiography. Los Altos, Calif.: Lange Medical Publications.