Prerequisite For HRV Biofeedback: Real-Time Detection of R Peaks

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Prerequisite for HRV biofeedback: Real-Time

Detection of R peaks
Branko Babusiak, Stefan Borik and Michal Gala
Faculty of Electrical Engineering, Department of Biomedical Engineering
University of Zilina
Zilina, Slovakia
branko.babusiak@fel.uniza.sk

Abstract—Heart rate variability (HRV) reflects the physical individual increase their HRV. The HRVB can also be an
health and ability of the organism to handle stress situations. effective component in asthma treatment.
HRV biofeedback is a real-time training process which helps
reduce stress and asthma severity. The most important There are many methods for HRV analysis which can be
prerequisite for the HRV biofeedback is the detection of R peaks divided into two main groups: time domain methods and
in the ECG signal. This article describes in detail a real-time frequency domain methods. The time-domain analysis is
algorithm capable of R peaks detection. The algorithm is simpler and more commonly used method of HRV
implemented within a standalone C# application and measurement. The RR interval is often denoted as normal-to-
functionality thereof is demonstrated. The results indicate that normal (NN) interval in HRV terminology. Simple time–
the proposed algorithm is immune against various types of domain variables such as the mean NN interval, the mean heart
artefacts. rate, the difference between the longest and shortest NN
interval, etc. can be calculated [1]. The simplest variable is the
Keywords—ECG; peak detection; HRV; biofeedback standard deviation of the NN interval (SDNN):

 ( NN )
I. INTRODUCTION 1 N
2
SDNN = − NN  ()
Electrocardiography (ECG) is one most prominent N i =1
i

diagnostic methods used for complex diagnosis of the


cardiovascular system, including the heart. A normal ECG where N is the number of NN intervals; NNi is the duration of
curve consists of a P wave, QRS complex and a T wave (Fig.
1). The U wave is present after the T wave in some individuals, the NN interval and NN represents the average duration of NN
especially sportsmen. The QRS complex is linked to the heart intervals. The SDNN reflects all cyclic components responsible
systole – that is, when the heart pumps blood into bodily for variability in the recording. The SDNN is dependent on the
vessels. The QRS complex comprises negative Q and S peaks length of recordings – total variance of HRV increases with the
and a dominant positive R peak. The distance between two length of the ECG signal, therefore comparison of SDNN
adjacent R peaks is denoted as RR interval. values from different length recordings is not accurate [1]. The
real-time detection of R peaks and calculation of RR (NN)
intervals is an essential part of the HRVB. This article deals
with a the real-time detection of R peaks as a prerequisite of
the HRVB.[2], [3]

II. MATERIALS AND METHODS


We developed a real-time method for R wave detection,
which is based on the Pan-Tompkins algorithm [4]. We
decided to employ the afore mentioned algorithm because of its
noticeable resistance to signal noise from surrounding sources.
Fig. 1. Two cycles of a typical ECG curve. The ECG signal is captured by our own designed device
described in [5]. We measure only one lead (channel) of ECG
The length of the RR interval is not constant over time. The with the electrodes attached to both wrists. The third electrode,
variations between RR intervals are called heart rate variability also known as the DRL (Driven Right Leg) electrode, is placed
(HRV). The HRV is a physiological phenomenon which on the right ankle. This electrode is used to reduce the mains
reflects the physical health of each individual and ability noise (50 Hz or 60 Hz) [6]. The signal was sampled at 500 Hz.
thereof to react to changes and stress. If the RR intervals are Part of the measured signal is shown in Fig. 2. It is evident that
almost constant, the overall HRV is low. People with low HRV the signal contains mains noise (50 Hz) even though the DRL
are more vulnerable to stress as they are not capable of quick electrode was employed. The proposed method for R peak
adaptation to changing situations. The HRV biofeedback detection consists of the following operations applied on the
process (HRVB) is real-time training process which helps input ECG signal: band-pass filtration, signal differentiation,
squaring process, moving average filtration and peak detection.
All parts of the algorithm are described in detail in the next
subsections.

Fig. 4. Output of band-pass filter (5 – 15 Hz).

Fig. 2. Measured ECG signal. Sampling frequency equal to 500 Hz.

A. Band-pass filter
The band-pass filter is used to maximize the energy of the
QRS complex in the spectrum. The main energy of the QRS
complex is approximately within the range of 5 to 15 Hz [4].
The original Pan-Tompkins algorithm uses a combination of
low-pass and high-pass filters to produce a band-pass filter.
The order and the gain of these filters was very low because of
low computational performance of computers in 1985 with
respect to real-time signal filtering [4]. Fig. 5. Output of signal differentiation.
We designed a FIR filter of order 355 and 40 dB
attenuation in the band-stop areas (Fig. 3). We chose the FIR
filter because of its linear phase characteristics and stability
thereof.

Fig. 3. Magnitude response of designed band-pass filter.


Fig. 6. Output of signal squaring.

The band-pass filter was applied on ECG signal (Fig. 2)


and the filter output is shown in Fig. 4. The distortion at the C. Moving average filter
beginning of signal is caused by the filter start-up. The squared signal contains multiple peaks around the QRS
complex. The purpose of the moving average filter is to smooth
B. Signal differentation and squaring the output of the squaring function and to produce a single
Differentiation of signal represents the slope of the signal peak in the area of the QRS complex. The length of the moving
with respect to time. The differentiated signal is depicted in average window is extremely crucial – length thereof should be
Fig. 5. The squaring process makes all data samples positive approximately the same as the length of the QRS complex. If
and maximizes negative and positive values of the signal. the length of the window is too long, the P wave or T wave will
Differentiation in combination with the squaring process merge with the QRS complex. If the window is too short, the
maximizes the changes between rising and falling edges of the QRS complex will produce several nearby peaks. The length of
signal. The output of the squaring process is depicted in Fig. 6. the QRS complex is about 80 ms (see Fig. 1) [7]. The
mentioned length is equal to 40 samples of signal when 500 Hz
sampling rate is used. The result of the moving average filter is
shown in Fig. 7. The positions of peaks represent the positions After successful detection of R peaks, the length of RR interval
of R waves in the ECG recording. can be calculated (Fig. 1). The instant BMP is computed from
the current RR interval using the following equation:
BPM = 60 / RR  ()

where RR represents the length of the RR interval in seconds.

III. RESULTS
The R peak detection method described in the previous
chapter was implemented within a real-time application. The
application is developed in C# programming language. Data is
received through UART interface. The algorithm works
properly after 2 seconds from the start of data collection
because the threshold value for peak detection is calculated
from two second intervals. Therefore, the first two seconds of
Fig. 7. Output of the moving average filter. the ECG signal are not visible in the application window while
the application is receiving and processing data in the
D. Peak detection background. Once enough input data is collected for the pass-
Proper detection of R waves depends on the peak detection band filter (355 samples = filter order), the filter starts to
algorithm applied at the output of the moving average filter. produce filtered samples which are stored in the pass-band
The peak detection algorithm searches for local signal maxima filter buffer.
which cross the adaptive threshold values. The threshold value
is computed as one third of maximum value of moving average
output in the last 2 seconds and is recomputed every 20
milliseconds after buffering the first 2 seconds. Not all detected
maxima are corresponding to R peaks because of the presence
of noise, slight variations of QRS complex length and
fluctuations of the ECG signal. The T wave is sometimes
detected as false maximum because in specific cases its value
can cross the threshold value. An operative criterion should be
applied to avoid detection of false maxima. This criterion is
based on the physiological properties of the ECG signal. The
heart rate should not be over 200 BPM, meaning that R peaks
cannot be closer than 0.3s (150 samples). If neighboring peaks
are closer than 0.3s then one of the peaks is erroneously
identified and must be discarded. In such case, the lower
maximum is discarded. The peak detection principle is shown
in Fig. 8. The adaptive threshold (orange line) is computed
from 2 second interval starting from the beginning and then
recalculated every 20 milliseconds. The true local maxima are
denoted by red circles and false maxima are denoted by the
black circles – these are discarded using the mentioned
criterion.

Fig. 9. Flowchart scheme for real-time R peak detection.

Once the number of filtered samples is sufficient for the


moving average filter (40 samples), the samples are
Fig. 8. Peak detection in the output (first ten seconds) of the moving average
filter. differentiated, squared and the moving average filter is applied.
When the number of averaged samples is at least 1000 (2
seconds), the threshold value is computed and peaks can be
detected while also applying the criterion for false peak
elimination. The real-time algorithm for R peak detection is
shown in a flowchart scheme in Fig. 9.
The application window is shown in Fig. 10. Real-time
ECG signal of a calm, seated subject is shown in the first row
of the application window. The output of the band-pass filter
and the output of the moving average filter with detected peaks
are shown in the second and the third row, respectively. These
additional signals are displayed only for validation purposes
regarding the algorithm functionality. The instant heart rate
(BMP), calculated from the last RR interval, is displayed in the
lower-right corner of the application. The calculated BPM is
equal to 60 and all peaks are detected correctly.

Fig. 11. Detection of R peaks in ECG with movement artefacts.

IV. CONCLUSION
An algorithm for real-time R peak detection was developed
and presented in this article. The algorithm was implemented
within a standalone C# application and functionality thereof
was confirmed on real-time ECG data from a calm, seated
person and also on ECG data with movement artefacts and
mains noise (50 Hz). The successful detection of R peaks is
crucial for proper computation of instant heart rate (BPM) and
heart rate variability (HRV). Subsequent work will focus on the
development of the HRV biofeedback system where proper
detection of R peaks is the most important input parameter.
ACKNOWLEDGMENT
This work was supported by the Slovak Research and
Fig. 10. Detection of R peaks in ECG of a calm, seated subject.
Development Agency under the contract No. APVV-16-0190.

The subject was then asked to periodically move his hand REFERENCES
with the attached electrode. The mentioned situation is depicted
[1] Task Force of the European Society of Cardiology and the North
in Fig. 11. Even though the ECG signal (first row) has changed
American Society of Pacing and Electrophysiology, “Heart rate
significantly, peaks were successfully detected. Positions of R
waves (signal in first row) and detected peaks (third row) are variability: Standards of measurement, physiological interpretation
not synchronized, as is evident from Fig. 10 and Fig. 11. This and clinical use,” Eur. Heart J., 1996.
is due to the delay of the bandpass filter and the moving [2] A. L. Wheat and K. T. Larkin, “Biofeedback of heart rate variability
average filter. However, synchronization thereof is not and related physiology: A critical review,” Applied
important for successful detection of instant BPM. The instant Psychophysiology Biofeedback, vol. 35, no. 3. pp. 229–242, 2010.
BMP will be used as input parameter for online HRV
[3] P. M. Lehrer, E. Vaschillo, B. Vaschillo, S.-E. Lu, A. Scardella, M.
estimation in ECG recordings with defined length – the SDNN
parameter described by equation (1). Siddique, and R. H. Habib, “Biofeedback Treatment for Asthma,”
Chest, vol. 126, no. 2, pp. 352–361, 2004.
The application for real-time detection of BPM was tested
[4] J. Pan and W. J. Tompkins, “A Real-Time QRS Detection
on a Windows PC with CPU Intel Core i5 3.2 GHz (four cores)
Algorithm,” IEEE Trans. Biomed. Eng., vol. BME-32, no. 3, pp.
and 8 GB RAM. The CPU usage did not exceed 4 % while the
application was running and processing data and RAM usage 230–236, 1985.
was within 12 megabytes. Based on above mentioned facts, we [5] B. Babusiak and S. Borik, “Bio-Amplifier with programmable gain
can conclude that the designed application runs without any and adjustable leads,” in 2013 36th International Conference on
problems on today’s standard personal computers. Telecommunications and Signal Processing, TSP 2013, 2013.
[6] V. Acharya, “Improving Common-Mode Rejection Using the Right-
Leg Drive Amplifier,” 2011.
[7] J. D. Bronzino, Biomedical Engineering Fundamentals (The
Biomedical Engineering Handbook, Third Edition). 2006.

You might also like