ECG Enhancement and R-Peak Detection Based on Window Variability

In ECG applications, the correct recognition of R-peaks is extremely important for detecting abnormalities, such as arrhythmia and ventricular hypertrophy. In this work, a novel ECG enhancement and R-peak detection method based on window variability is presented, and abbreviated as SQRS. Firstly, the ECG signal corrupted by various high or low-frequency noises is denoised by moving-average filtering. Secondly, the window variance transform technique is used to enhance the QRS complex and suppress the other components in the ECG, such as P/T waves and noise. Finally, the signal, converted by window variance transform, is applied to generate the R-peaks candidates, and the decision rules, including amplitude and kurtosis adaptive thresholds, are applied to determine the R-peaks. A special squared window variance transform (SWVT) is proposed to measure the signal variability in a certain time window, and this technique reduces false detection rate caused by the various types of interference presented in ECG signals. For the MIT-BIH arrhythmia database, the sensitivity of R-peak detection can reach 99.6% using the proposed method.


Introduction
The electrocardiogram (ECG) is a random and unstable signal that records the electrical activity of the heart, and the ECG signals obtained in different environments and from individuals are significantly different. The P wave, QRS complex and T wave are the main components in the ECG waveform (see Figure 1) [1], and the accurate detection of them is important to ECG signal analysis. The QRS complex is the dominant feature of the ECG signal and its accurate detection is an important issue in many clinical conditions [2]; for example, the RR interval is used for heart rate measurement and diagnosis of several abnormalities-ventricular hypertrophy [3], conduction abnormalities [4], etc.
The detection of the QRS complex is the first step in all kinds of automatic feature extractions for ECG signals [5]. Unluckily, there are great challenges for automated detection because the morphologies and amplitudes of many normal QRS complexes are like the abnormal QRS complexes. The superimposed noise in the ECG signal makes this problem more severe. Furthermore, the P/T waves with higher amplitude can interfere with the detection of the QRS complex. Therefore, the first step of R-peak detection is signal denoising, and then the QRS complexes are enhanced and detected.
Many denoising methods [6][7][8][9][10][11][12][13] for ECG signals have been proposed, including digital filtering [14], morphological filtering [15,16] and decomposition-based denoising methods [13,17,18]. Among them, the digital filters are widely used to remove the noise of a certain frequency range because of their simplicity and efficiency; morphological filters [16] are often used to smooth the ECG signal, but the size and shape is hard to determine; decomposition-based denoising methods such as wavelet transform [16] and empirical mode decomposition (EMD) [17] methods decompose the signal into a series of modes and set some modes to 0 to remove noise. Thus, most of the noises can be well addressed by the methods mentioned above. For QRS detection, most QRS detection methods [19][20][21][22][23] are researched based on the three steps-denoising, QRS complex enhancement and decision rule creation. The stages of QRS complex enhancement and decision rule creation are different and result better or worse results. In [24], the Pan-Tompkins algorithm was proposed for R-peak detection, which includes five steps, namely, band-pass filtering, derivation, squaring, moving-window integration and adjustment of thresholds. In [25], the wavelet transforms (WT) for detecting QRS complexes is proposed, in which the multiscale feature of WT is used to distinguish QRS complexs from P/T waves or noise. In [26], an EMD-based method for QRS detection was proposed; unlike the wavelet methods, the EMD methods have overcome their mode-mixing problem, having emerged as powerful time-frequency decomposition tools. However, the preprocesses of the above methods need to be carefully designed, and multiple nonlinear transform techniques are necessary to enhance the QRS complex.
In the proposed method, the noisy signal is first denoised by moving-average filtering, and it can be easily replaced by other denoising methods. In the stage of ECG enhancement, multiple methods were usually employed at the same time for the most studies; however, the proposed window variance transform technique is very effective for highlighting Rpeaks and suppressing P/T waves and noise in the ECG signal, and all R-peaks candidates can be well identified. Even if additional techniques are added, the candidates will not be greatly improved. Hence, it is sufficient to use only window variance transform for signal enhancement. Furthermore, two adaptive thresholds related to amplitude and kurtosis are computed for recognizing the locations of R-waves. The experimental results demonstrate that the proposed QRS detection method can accurately locate the R-wave positions. The main contributions of this paper are as follows: (1) A novel QRS detection method is proposed to accurately locate the positions of the R-waves. (2) An efficient QRS-enhancement technique is designed. This paper is organized as follows. In Section 2, we give a brief introduction of the proposed method. In Section 3, we show and discuss the experimental results of the proposed algorithm, in which the evaluation of algorithm is accomplished with the ECG data from the MIT/BIH arrhythmia database. Concluding remarks are given in Section 4.

Data
The MIT-BIH database [27] contains 48 ECG records; each record contains 30 min and was sampled at 360 Hz. There are two leads in each record and the first lead was used in the experiment. Locations of R-peaks have been annotated by two or more cardiologists independently for all records.

Overview of the Algorithms
The proposed method for R-peak detection is divided into three stages, including noise removal, ECG enhancement and decision rules. Those stages are illustrated in Figure 2. In the proposed method, the ECG enhancement stage plays the most critical role; it highlights the QRS complex and suppresses the other components in the ECG signal. A brief description of each stage in Figure 2 is presented in the following subsections.

Noise Removal
Since the signal is often corrupted by various noise-such as the high-frequency noise, baseline wander noise and artifacts-in this section, to maintain the original morphological features of the ECG signal, the moving average filtering is applied to smooth noise, and the formula of moving average filters can be described as follows: where y i and y i represent the amplitudes of the ith sample in the original ECG signal and denoised signal, respectively; M represents the filter length, which is set to 5 samples.

ECG Enhancement
In this section, a novel ECG enhancement technique called window variance transform is proposed. The variance of an signal segment is used to highlight all R-peaks and suppress the other components. The proposed squared window variance transform (SWVT) is summarized as follows: 1.
Non-overlapping window Local peaks P = [p 1 , p 2 , . . . , p N ] are first detected using the denoised signal. Then, P is applied to generate an non-overlapping window N, W P = [w p 1 , w p 2 , . . . , w p N ], and the ith window is determined by using where w is the window size. To avoid including multiple waveforms in one window, the minimum width of wave T is used as a reference to set the range of w from 40 to 60 ms. Next, for the non-peak samples between two peaks, they are divided into M non-overlapping windows: W NP = [w np 1 , w np 2 , . . . , w np M ]; the window size is 2 * w. Finally, the W P and W NP are merged to generate the N + M non-overlapping windows W = [w 1 , w 2 , . . . , w N+M ].

Window variance transform
To detect the R-peaks, the window variance transform is used to enhance the QRS complexes; that is, the denoised signal is transformed to the where j = [1, 2, . . . , N + M] are the indexes of windows; l j is the number of samples in jth window; µ j is the amplitude mean of the jth window samples.

Squaring
To further enhance the QRS complexes, the squaring operator is applied in the WVT, that is, the squared window variance transform (SWVT) is described as follows: As shown in Figures 3 and 4, the result of SWVT significantly suppressed the effect of the P/T wave and enhanced the QRS complex. From Figure 4, it can be seen that SWVT is also beneficial for the an ECG signal with inverse R peaks. Thus, the proposed QRS complex enhancement technique is reasonable for R-peak detection.

R-Peak Detection
Due to R-peaks being different from other waveforms in terms of morphology and amplitude, most existing detection methods use amplitude thresholds for R-peak detection, which is less robust to noisy signals. However, QRS complexes usually have significant differences in amplitude and kurtosis compared with those of the other waveforms in ECG signals; thus, the amplitude and kurtosis thresholds are applied for R-peak detection. The detailed achievement is described as follows: 1.
Generation of the R-peaks candidate set The transformed signal SWVT not only highlights the R-waves but also suppresses the P/T waves, and the screening of the R-peaks candidate set can be completed by setting the amplitude threshold. Considering that the R-peaks which are disturbed by noise may have lower amplitudes during the transformation, the 90th percentile of SWVT and the mean of SWVT are applied to balance the amplitude threshold T c ; that is, where per (SWVT,90) and µ SWVT = 1 N+M ∑ N+M t=0 SWVT t are the 90th percentile value and the mean value of SWVT, respectively; T c denotes the positive threshold determining the transformed amplitude of candidate selection.
Then the amplitude threshold T c is used for the extraction of R-peak candidates set S c : 2.
Generation of the R-peaks with decision rules (1) Adjusting the RR interval: the RR interval is another important feature of R-peaks. In this study, the RR interval is determined by two RR interval averages. One is the average of the eight most-recent beats RR recent ; the other is the average of the detected R-peaks, RR all -that is, For the first RR interval, the lower limit of the RR interval and the higher limit of the RR interval are set to 200 and 360 ms, respectively. However, for irregular heart rates, if an R peak is not found during the interval specified by the 1.66 * RR, a search-back technique is necessary to look back in time for the R peak.
(2) Adjusting the thresholds: To better detect the R-peaks, the amplitude and kurtosis thresholds T a and T k are automatically adjusted to float over R-peak candidate points and detected R-peaks.
where PER A c and PER K c are the 90th percentile values of the amplitude and kurtosis in the candidate set S c , respectively. Similarly, MEAN A R and MEAN K R are mean values of the amplitude and kurtosis in the peak set S R , respectively. α is a hyperparameter, and it is used to control the weight of R-wave amplitude and kurtosis for adjusting thresholds. For irregular heart rates, the lower T a and T k are needed for back searching for missing R-peaks; hence, two thresholds are reduced by half to avoid missing beats: (3) Generation of the R-peaks: according to the thresholds aforementioned, we designed some decision rules to generate the R peaks S R . For each of the peak in S c , if an RR interval is between 0.92 * RR and 1.16 * RR, a judgment is made to determine whether the current peak is the R peak; the decision rules can be described as follows: where C l and y C l represent the l th candidate peak and the amplitude of the l th candidate peak; k C l is the kurtosis of the l th candidate peak; S R last refers to the position of the last R-peak in the current R-peaks set.

Metrics for Performance Evaluation
In this study, three metrics were used to evaluate the performance of R-peak detection in the simulation experiment, sensitivity (SEN), positive predicative value (PPV) and cumulative statistical index (CSI) [28], which are described as follows.
where TP, FN and FP represent true positive (correctly identified R peak), false negative (missing R peak) and false positive (wrongly identified R peak), respectively. FPR is the false positive rate FP/(FP + TP) and FNR is the false negative rate FN/(FN + TP). F d was employed to measure the detection error rate. Larger SEN, PPV and CSI values mean better detection performance; meanwhile, a lower F d is expected.

Experimental Data and Environment
In the experiment, 30 ECG records from the MIT-BIH arrhythmia database were adopted to evaluate the effectiveness of the proposed R-peak detection method. The length of all signals was 30 min. The classic PT [24], XQRS [29] and RSlope [30] methods are introduced for comparison. These methods were implemented with Python and Matlab, and the parameters were set according to the original papers. All experimental results were performed on Linux x86_64, Matlab: version R2018b and Python: version 3.7.6.

Detailed Accuracy of the MIT-BIH Database
In the first experiment, the R-peak detection results were derived with the proposed method when applied to all 30 ECG signals from MIT-BIH dataset. In Table 1, the TP, FP, FN, SEN, PPV and CSI of each record are listed. From Table 1, we find that both SEN and PPV still show very high accuracy for most of the records. The proposed method did not perform well in several records (114, 207, 208, 214, 228, 232), for which the SEN or PPV were lower than 99%. In addition, the CSI values for most records were lower than 98%, because some records showed higher FP and FN. TP-true positive (correctly identified R peak); FN-false negative (missing R peak); FP-false positive (wrongly identified R peak); SEN-sensitivity; PPV-positive predicative value; CSI-cumulative statistical index.

Accuracy Evaluation and Comparison
In the second experiment, the overall performance of the proposed method on MIT-BIH database is shown in Table 2, which also contains three comparison results. In this paper, the detected locations of R-peaks are very close to the reference locations, and some R-peaks were shifted by less than 75 ms compared with the reference R peaks. It can be seen from Table 2 that 99.03% of total beats can be detected correctly by the proposed method; SEN and PPV were 99.65% and 99.39%, respectively. Moreover, the CSI of the proposed method was higher than 99%-better than those of the methods used for comparison.  Table 3 was drawn based on Table 2, which shows the FP and FN comparison results among the four methods. In Table 3, the proposed method shows the least detection errors among the four methods. It is worth noticing that there is a discrepancy between the accuracy reported here and those reported in other methods [5,31]. This is possibly because the different tolerances for counting a detection as a TP are set. In this study, the tolerance is set to ±75 ms of the annotation. In a total of 88,699 beats, there were 482 and 335 beats detected as FP and FN, respectively. Furthermore, the proposed method achieved the minimum detection error of 0.92%. As the proposed method effectively suppressed the P/T wave and noise in the stage of R-peak enhancement, the proposed method significantly reduced the number of FN. Therefore, it is seen that the proposed method has better detection than the other methods.  Figure 5 presents an ECG segment with an obvious waveform distortion caused by large baseline wander, and the R-peak detection results produced by different methods. Compared with the reference labels, there are several obvious errors for PT, XQRS and Rslope methods, which are marked by red rectangles and Xs. However, our method can deal with this case very well. In other words, the proposed method effectively suppressed the noise. Then, R-peak detection results were more carefully analyzed using record 208. Figure 6 shows the original ECG signal from 15 to 30 s and the R-peaks locations detected by different detectors. The proposed method can accurately detect the locations of all R-peaks; however, the XQRS and R slope showed some missing R-peaks. This phenomenon may be caused by irregular beats. Furthermore, XQRS demonstrates the worst performance for the wide ectopic beats. Figure 5. The R-peak detection results of 101 for different detectors on MIT-BIH database (a red X represents the wrongly identified R peak; a red rectangle is the missing R peak). TP-true positive (correctly identified R peak); FN-false negative (missing R peak); FP-false positive (wrongly identified R peak); SEN-sensitivity; PPV-positive predicative value; CSI-cumulative statistical index. Figure 6. The R-peak detection results of 208 record for different detectors (a red rectangle is the missing R-peak). TP-true positive (correctly identified R peak); FN-false negative (missing R peak); FP-false positive (wrongly identified R peak); SEN-sensitivity; PPV-positive predicative value; CSI-cumulative statistical index.

Discussion
In this study, the performance of the proposed R-peak detection method was tested on MIT-BIH database. Based on the results presented in Table 2, all these methods have excellent R-peak detection capacity with sensitivity rates higher than 99%. That is, most existing methods for R-peak detection have shown excellent performance. However, the three stages for R-peak detection, including preprocessing, enhancement and decision rules, are very different. This paper provided a novel QRS-enhancement R-peak detection method. Next, we analyzed the impacts of key steps and parameters on the experimental results.

ECG Enhancement
As shown in Table 4, the proposed method is easy to perform and does not need complex mathematical calculations compared with the other methods, especially for the stage of enhancement of QRS complexes. For instance, the proposed work only needs one to perform a simple window variance transform to achieve QRS complex enhancement and obtain better detection performance.  Figure 7 illustrates the distribution comparison between the QRS complexes and other ECG waveforms, in which QRS, P and T waves were extracted from the QT Database [32], and the number of each wave is 100. In Figure 7, the amplitude and kurtosis distributions of the main waveforms in ECG signal are plotted; it can be seen that there is are significant differences between the QRS complex and P/T wave in amplitude and kurtosis values. In statistics, a higher kurtosis means that the increase in variance is caused by extreme differences in low frequencies that are greater or less than the average. Next, we analyzed the impacts of key steps and parameters on the experimental results. It is well known that most of R-peak detection methods only depend on amplitude thresholds to identify R-peaks, and some mistakes may occur for unobvious R-waves shape. Thus, in this study, two statistical thresholds were used for R-peak identification. In Table 5, we list the performance of using kurtosis threshold on detection results. As shown in Table 5, compared with the performance without using a kurtosis threshold, the proposed method can get fewer FN and more TP, and the improvements to SEN and CSI are 1.21% and 1.08%, respectively. Hence, the kurtosis threshold plays a vital role in R-peak identification, and there is a significant decrease in FN because R-peak features were deeply extracted. As a result, higher CSI and SEN were obtained.

The Effect of Using a Kurtosis Threshold
Some of the signals are affected by stretches of noise, baseline wander and artifacts. For example, 222 has some non-QRS waves with highly unusual morphologies (may lead to false positives); 208 has an irregular RR interval (may lead to missing R-peaks). In Figures 5 and 6, the R-peak detection results are shown; the proposed method can achieve annotations the same as the references. However, for the comparison methods, obvious FP and FN occurred. This was due to improper threshold setting or improper noise processing during QRS complex enhancement. However, the proposed method can deal with this case by introducing the simple window variance transform and decision rules. That is, the two thresholds, including amplitude and kurtosis, can distinguish between R-peaks and non-R peaks which are similar to the R-peaks in morphology and amplitude.  TP is correctly identified R-peaks, FP is wrongly identified R-peaks and FN is missing R-peaks. SEN is sensitivity, PPV is positive predicative value and CSI is cumulative statistical index.

Selection of the Weight
In this work, the performance of the proposed method was influenced by the amplitude and kurtosis thresholds; that is, the superparameter α in Equations (7) and (8) had a large effect on experimental results. The α is a superparameter which represents the weight of R-peaks amplitude or kurtosis. In Figure 8, we plot the R-peak detection performance of the proposed method with different α values. In Equations (7) and (8), larger α means lower thresholds and more FP, which results in lower PPV. On the contrary, smaller α means higher thresholds and can lead to more FN and lower SEN. From 8, we can see that α = 0.5 achieved the best CSI; meanwhile, better SEN and PPV were obtained. However, there are still some limitations to the present study. A signal with unusual morphologies may cause bad performance by the proposed method. The reason is that the R-peak detection result is more relative to the approativate adaptive thresholds, but the amplitude and kurtosis values of the unusual R-peaks are irregular. In addition, the adaptive thresholds depend on the window size, and the selection of window size is another challenge. However, the fixed window may not include all samples in a wave with severe waveform variations, and the results of R-peaks based on window variance transform may not be satisfying. Those limitations have to be solved for future research. Furthermore, we will also consider using other non-curated datasets, such as the NST dataset, and ambulatory ECG dataset obtained by wearable devices, to observe and analyze the preprocessing performance of the proposed method for noisy signals.

Conclusions
In this paper, a simple and reliable ECG enhancement and R-peaks detection method was proposed, which is easy to perform and does not need complex mathematical calculations. The proposed method for R-peak determination is very robust for signals with larger amplitudes or P/T waves. The sensitivity of detecting R-peaks in the MIT-BIH database using the proposed method can reach 99.65%, which is much better than the most of the existing R-peak detection methods. The experimental results and analysis demonstrate that the proposed R-wave enhancement technique and the decision rules, including amplitude and kurtosis factors, are useful for the recognition of R-peaks. It can be conclude that the proposed method is suitable and reliable for ECG R-peak detection.
Author Contributions: Conceptualization, L.W. and X.X.; methodology, L.W.; validation, L.W. and X.X.; formal analysis, Y.W., X.X. and L.W.; investigation, L.W.; resources, Y.W. and L.W.; writingoriginal draft preparation, L.W.; writing-review and editing, X.X. and Y.W.; supervision, Y.W. and X.X.; project administration, Y.W.; funding acquisition, Y.W. and X.X. All authors have read and agreed to the published version of the manuscript. Informed Consent Statement: Written informed consent was obtained from all participants before inclusion in the study.