Real-Time ECG QRS Detection
This example shows how to detect the QRS complex of electrocardiogram (ECG) signal in real-time. Model based design is used to assist in the development, testing and deployment of the algorithm.
The electrocardiogram (ECG) is a recording of body surface potentials generated by the electrical activity of the heart. Clinicians can evaluate an individual's cardiac condition and overall health from the ECG recording and perform further diagnosis.
A normal ECG waveform is illustrated in the following figure . Because of the physiological variability of the QRS complex and various types of noise present in the real ECG signal, it is challenging to accurately detect the QRS complex.
The Noise sources that corrupt the raw ECG signals include:
Power line interference (50 Hz or 60 Hz)
Electromyographic (EMG) or muscle noise
Artifacts due to electrode motion
Electrode Contact Noise
ECG Signal Source
The ECG signals used in the development and testing of the biomedical signal processing algorithms are mainly from three sources: 1) Biomedical databases (e.g.,) or other pre-recorded ECG data; 2) ECG simulator; 3) Real-time ECG data acquisition.
In this example, the following pre-recorded and simulated ECG signals are used. The signals all have sampling frequencies of 360 Hz.
one set of recorded real ECG data sampled from a healthy volunteer with a mean heart rate of 82 beats per minute (bpm). This ECG data was pre-filtered and amplified by the analog front end before feeding it to the 12 bit ADC.
four sets of synthesized ECG signals with different mean heart rates ranging from 45 bpm to 220 bpm.  is used to generate synthetic ECG signals in MATLAB.
Here are the settings for generating the synthesized ECG data:
Sampling frequency: 360 Hz;
Additive uniformly distributed measurement noise: 0.005 mV;
Standard deviation of heart rate : 1 bpm.
ECG Signal Pre-processing and Filtering
A real-time QRS detection algorithm, which references [1, lab one],  and , is developed in Simulink with the assumption that the sampling frequency of the input ECG signal is always 200 Hz (or 200 samples/s). However, the recorded real ECG data may have different sampling frequencies ranging from 200 Hz to 1000 Hz, e.g., 360 Hz in this example. To bridge the different sampling frequencies, a sample rate converter block is used to convert the sample rate to 200 Hz. A buffer block is inserted to ensure the length of the input ECG signal is a multiple of the calculated decimation factor of the sample-rate converter block.
The ECG signal is filtered to generate a windowed estimate of the energy in the QRS frequency band. The filtering operation has these steps:
1. FIR Bandpass filter with a pass band from 5 to 26 Hz
2. Taking the derivative of the bandpass filtered signal
3. Taking the absolute value of the signal
4. Averaging the absolute value over an 80 ms window
Real-Time QRS Detection of ECG Signal
The QRS detection block detects peaks of the filtered ECG signal in real-time. The detection threshold is automatically adjusted based on the mean estimate of the average QRS peak and the average noise peak. The detected peak is classified as a QRS complex or as noise, depending on whether it is above the threshold.
The following QRS detection rules reference the PIC-based QRS detector implemented in .
Rule 1. Ignore all peaks that precede or follow larger peaks by less than 196 ms (306bpm).
Rule 2. If a peak occurs, check to see whether the raw signal contains both positive and negative slopes. If true, report a peak being found. Otherwise, the peak represents a baseline shift.
Rule 3. If the peak is larger than the detection threshold, classify it as a QRS complex. Otherwise classify it as noise.
Rule 4. If no QRS has been detected within 1.5 R-to-R intervals, but there is a peak that was larger than half the detection threshold, and that peak followed the preceding detection by at least 360ms, classify that peak as a QRS complex.
Simulate and Deploy
1. Open the example model.
2. Change your current folder in MATLAB® to a writable folder.
3. On the model tool strip, click Run to start the simulation. Observe the HeartRate display and the raw and filtered ECG signal in the scope, which also illustrates the updating of peaks, threshold and estimated mean heart rate.
4. Open the dialog of ECG Signal Selector block. Select the ECG signal mean heart rate in the drop down menu. Click Apply and observe the real-time detection results in the scopes and HeartRate display.
5. Click Stop to end simulation.
6. After selecting target hardware, you can generate code from the ECGSignalProcessing subsystem and deploy it to the target.
 J. Pan and W. Tompkins, A Real-Time QRS Detection Algorithm, IEEE Transactions on Biomedical Engineering, 32(3): 230-236, March 1985
 Patrick S. Hamilton, EP Limited: Open Source ECG Analysis Software, 2002