Improving R Peak Detection in ECG Signal Using Dynamic Mode Selected Energy and Adaptive Window Sizing Algorithm with Decision Tree Algorithm

R peak detection is crucial in electrocardiogram (ECG) signal analysis to detect and diagnose cardiovascular diseases (CVDs). Herein, the dynamic mode selected energy (DMSE) and adaptive window sizing (AWS) algorithm are proposed for detecting R peaks with better efficiency. The DMSE algorithm adaptively separates the QRS components and all non-objective components from the ECG signal. Based on local peaks in QRS components, the AWS algorithm adaptively determines the Region of Interest (ROI). The Feature Extraction process computes the statistical properties of energy, frequency, and noise from each ROI. The Sequential Forward Selection (SFS) procedure is used to find the best subsets of features. Based on these characteristics, an ensemble of decision tree algorithms detects the R peaks. Finally, the R peak position on the initial ECG signal is adjusted using the R location correction (RLC) algorithm. The proposed method has an experimental accuracy of 99.94%, a sensitivity of 99.98%, positive predictability of 99.96%, and a detection error rate of 0.06%. Given the high efficiency in detection and fast processing speed, the proposed approach is ideal for intelligent medical and wearable devices in the diagnosis of CVDs.


Introduction
Cardiovascular diseases (CVDs) cause 31% of global mortality [1]. The burden of CVDs and shortage of physicians will increase to 57% and 42% worldwide by 2034 [2]. In addition, the median accuracy of ECG interpretation is only 54% for all training levels of practicing physicians and cardiologists [3]. The detection of R peaks is the fundamental step for ECG analysis. ECG signal is a non-linear and non-stationary physiological signal that represents the heart's electrical activities. It is measured by electrodes set on the patient's skin, forming the Einthoven triangle. Six fiducial points in the ECG signal, i.e., the P, QRS, T, and U, represent heart events during the relaxation or contraction [4] and are presented in Figure 1. The QRS wave is the vital feature for ECG analysis, and the R is the tallest peak in the QRS waves [5]. The ECG morphology varies from person to person, even at a different time for the same individual. In addition, the ECG is generally suffering from power line noise, muscular noise, electrode contact noise, and baseline wander [6]. These factors have added difficulties in R peak detection.
This study proposed DMSE, AWS, and RLC algorithms to improve R peak detection efficiency in real-time. The DMSE algorithm is derived from variational mode decomposition (VMD) [29] and Hilbert Transform (HT) [30], and improved in energy and frequency estimation. The AWS algorithm is derived from the peak detection algorithm and enhances the performance of Region of Interest (ROI) estimation. The R location correction (RLC) algorithm is a search algorithm and progresses in the accuracy of the R location. As a result of these improvements, this study achieved competitive accuracy, sensitivity, and specificity in automatic R peak detection.

ECG Signal Dataset Overview
This study used the MIT-BIH arrhythmia database from the physionet site [31]. It was the first generally available set of standard test materials for evaluating arrhythmia detectors, and the arrhythmia database has 48 two-channel ambulatory ECG recordings This study proposed DMSE, AWS, and RLC algorithms to improve R peak detection efficiency in real-time. The DMSE algorithm is derived from variational mode decomposition (VMD) [29] and Hilbert Transform (HT) [30], and improved in energy and frequency estimation. The AWS algorithm is derived from the peak detection algorithm and enhances the performance of Region of Interest (ROI) estimation. The R location correction (RLC) algorithm is a search algorithm and progresses in the accuracy of the R location. As a result of these improvements, this study achieved competitive accuracy, sensitivity, and specificity in automatic R peak detection.

ECG Signal Dataset Overview
This study used the MIT-BIH arrhythmia database from the physionet site [31]. It was the first generally available set of standard test materials for evaluating arrhythmia detectors, and the arrhythmia database has 48 two-channel ambulatory ECG recordings of 30 min each. These signals are sampled at 360 Hz and have a resolution of 11 bits over a spectrum of ten millivolts. This study used the first lead of all 48 records for training, validation, and testing the proposed method's performance in the analysis phase. There are a total of 112,647 annotated labels. Annotations are two types, i.e., beat annotation and non-beat annotation. This study used 18 types of MIT-BIH beat annotations and considered each beat to have an R peak. There is a total of 109,494 QRS beats in the MIT-BIH arrhythmia database.

Methods
This proposed method consists of five significant stages: (a) Data Acquisition, (b) QRS Component Extraction, (c) Region of Interest (ROI) estimation, (d) Feature Processing, and (e) Detection. Figure 2 illustrates the block diagram of the proposed method and its sub-systems. In addition, Graphical User Interface (GUI) software is developed to operate the proposed method in real-time.
non-beat annotation. This study used 18 types of MIT-BIH beat annotations and considered each beat to have an R peak. There is a total of 109,494 QRS beats in the MIT-BIH arrhythmia database.

Methods
This proposed method consists of five significant stages: (a) Data Acquisition, (b) QRS Component Extraction, (c) Region of Interest (ROI) estimation, (d) Feature Processing, and (e) Detection. Figure 2 illustrates the block diagram of the proposed method and its sub-systems. In addition, Graphical User Interface (GUI) software is developed to operate the proposed method in real-time.

Data Acquisition
This study interfaces the AD8232 EKG sensor (SparkFun Electronics, Niwot, CO, USA) module with an Arduino board (Arduino, Scarmagno, Italy). A Jetson Nano microcomputer (Nvidia Corporate, Santa Clara, CA, USA) receives data from Arduino at a configurable sampling rate via serial port.

Data Acquisition
This study interfaces the AD8232 EKG sensor (SparkFun Electronics, Niwot, CO, USA) module with an Arduino board (Arduino, Scarmagno, Italy). A Jetson Nano microcomputer (Nvidia Corporate, Santa Clara, CA, USA) receives data from Arduino at a configurable sampling rate via serial port.

QRS Component Extraction
The QRS Component Extraction section decomposed the ECG signal into modes and computed the Hilbert spectrum using variational mode decomposition (VMD) and Hilbert Transform (HT). Then the dynamic mode selection algorithm determines the QRS components adaptively.

VMD and HT
The VMD decomposed a signal f (t) into K modes u k (k = 1, 2, 3, . . . , K). These modes are called VMFs, and a VMF is defined as amplitude-modulated frequency-modulated (AM-FM) signals, written as Equation (1), where u k (t) is a VMF, A k (t) is a nonnegative envelope, and ϕ k (t) is non-decreasing phase function.
The complex conjugate y(t) of any real-valued function x(t) of L p class (Lebesgue spaces) can be calculated by Titchmarsh theorem defined in Equation (2), in which the PV indicates the principal value of the singular integral.
With the Hilbert Transform, the analytic signal is defined in Equation (3), where a(t) = x 2 + y 2 and θ(t) = arctan y x . Here, a(t) is the instantaneous amplitude and θ is phase function. The instantaneous frequency and energy are given by Equations (4) and (5), respectively, Dynamic Mode Selected Energy (DMSE) QRS waves were observed within a frequency band, i.e., ω B → ( f l , f h ) . The mean and standard deviation of each VMF's instantaneous frequency is used to select the modes dynamically. The upper and lower frequency scale of each VMF were defined by the spread from mean frequency and limited by the standard deviation. If both degrees' intersection is within ω B , those VMFs were considered as QRS components. VMFs' selection process is presented with Equation (6), It was also observed that some data points in the chosen VMFs included frequencies outside the ω B , causing an unexpected rise or fall at the edges. If instantaneous frequency of a data point is out of ω B , then those data points are detected as noisy. It is presented in Equation (7), The elementwise multiplication of ω r with instantaneous energy and frequency of a selected VMF only set the unnecessary data to 0. Then, the integral of all necessary data points of instantaneous energy and frequency is computed by Equations (8) and (9). R e and R ω accommodate energy and frequency of QRS components. where k s is the number of chosen VMF.
where k s is the number of chosen VMF.
To obtain the noise component, this study inverts the ω r and multiplies with instantaneous energy, and finally, the integral of all noise data points is computed with Equation (10).
where k is the number of VMF.
The flow-chart of DMSE algorithm is presented in Figure 3.
where is the number of chosen VMF.
where is the number of chosen VMF. To obtain the noise component, this study inverts the and multiplies with instantaneous energy, and finally, the integral of all noise data points is computed with Equation (10).
where is the number of VMF.
The flow-chart of DMSE algorithm is presented in Figure 3.

ROI Estimation
The ROI was estimated from the integral function of energy in QRS components. The AWS algorithm was developed based on local peaks to find ROI adaptively. A peak finding algorithm detects the local peaks and its height, prominence, and width. To compute the ROI, we kept the peak's location at the center and spread it to the left and right sides up to the peak width. The ROI window is adaptively computed with Equations (11)- (14).

ROI Estimation
The ROI was estimated from the integral function of energy in QRS components. The AWS algorithm was developed based on local peaks to find ROI adaptively. A peak finding algorithm detects the local peaks and its height, prominence, and width. To compute the ROI, we kept the peak's location at the center and spread it to the left and right sides up to the peak width. The ROI window is adaptively computed with Equations (11)- (14).
where Peak width i is the width of i th peak.
where Peak L i is the location of i th peak.
The flow-chart of AWS algorithm is presented in Figure 4.

(11)
where is the width of th peak. (12) where is the location of th peak.
The flow-chart of AWS algorithm is presented in Figure 4.

Feature Processing
Classification features were processed in two steps, i.e., (a) Feature Extraction and (b) Feature Selection.
This study calculated twenty-five features, i.e., four features related to peak, two from a distance, eighteen statistical features from each peak's window, and one from estimated noise. All extracted features are presented in Table 1.

Height
The instantaneous energy at a peak location.

Frequency
The instantaneous frequency at a peak location. Prominence The prominence of a peak Width The width of a peak This study calculated twenty-five features, i.e., four features related to peak, two from a distance, eighteen statistical features from each peak's window, and one from estimated noise. All extracted features are presented in Table 1.
Sequential Forward Selection (SFS) method was used to find relevant features for better classification. SFS is a wrapper-approach method based on the greed search algorithm, reduces N-dimensional feature space to an n-dimensional subspace, where n < N. SFS automatically selects a subset of the most relevant features and removes irrelevant features based on criterion values. Criterion refers to the mean squared error (MSE).

Detection Process
The detection process has an ensemble decision tree (DT) model and RLC algorithm. The model classifies R and False-R. The RLC algorithm finds the exact location of R peaks in the time domain. This study creates a dataset based on detected ROI from the ECG signal database to train, validate, and test the classifier model.

Datasets for Classification
This study found that most of the ROIs contain the QRS complex. The QRS containing ROIs are labeled as R otherwise, False-R. The training dataset has been created with 80% of each type of arrhythmia sample from the estimated ROIs. The samples are chosen randomly. The remaining 20% of the data in the ROIs' dataset is the test dataset. The training dataset is further divided into 10-fold for cross-validation during the training process.

Ensemble Decision Tree
A Decision Tree (DT) is a non-parametric and supervised learning algorithm widely used for classification. It classifies by learning decision rules' reasoning from the features. A tree starts from a root node, splits into branches or sub-trees until it reaches a terminal or leaf node. The root or parent of a leaf node is decided based on the feature approximation constant. This node is called the decision node, and the approximation constant is the rule. The decision tree uses the Gini index to compute the approximation constant. The Gini index computes the relative degree of disparity within the conveyance of classes and ranges from zero to one. Gini Index is computed with Equation (15). where P j is the probability of j class.
Utilizing the Gini index, the most excellent features which contain the foremost information concerning the target are recognized. The dataset and the values of these features are then used to ensure that the target function values at the leaf nodes are as perfect as possible. As a result, a series of decision rules are organized in a tree structure that can distinguish. Therefore, a set of decision rules are arranged in a tree structure capable of classifying. The deeper the tree is, the model is fitter but suffers from an overfitting problem. The ensemble decision tree algorithm has overcome this generalization problem.
This study trained a total of 2 n − 1 decision trees, where n is the number of selected features. Each DT is trained with a different feature set, and 75% of the samples are randomly chosen for training from the training dataset obtained from the ROIs' dataset. Sixteen DTs have been selected based on the highest validation accuracy. Next, those sixteen DTs are ensembled, and finally, the majority votes decide which ROI contains an R peak.

R Location Correction (RLC)
This study observed that some of the detected R peaks are slightly dislocated from the annotated position. This dislocation of peak position creates incorrect results in calculating heart rate. To overcome the dislocation problem, we find the maximum value of a correction window. The position of the maximum value in the signal is considered as an R peak location. It has been observed that the dislocation problem occurs inside a window size of eight. We keep the detected location at the center of the correction window and find the correct location and amplitude as presented in Equation (16).
2.2.6. GUI Software A utility software has been developed with a graphical user interface presented in Figure 5. It can control the power on and off. Lead placement is checkable. Sampling frequency can be configured to 100 Hz, 150 Hz, 200 Hz, and 360 Hz. Time of acquisition is configurable to 5 s, 10 s, 1 min, 10 min, and in monitoring mode. Monitoring mode will be helpful in longtime observation. Realtime ECG with R location marked by an asterisk can be seen. Heart rate, MRR, SDNN, and RMSSD are also presented in the real-time report section. The patient data and model-generated medical information can be saved as a PDF file or printed with this software.

Evaluation Methods
True-Positive (TP), False-Positive (FP), True-Negative (TN), and False-Negative (FN) are the four quantities required to calculate the classification evaluating parameters such as sensitivity, precision, detection error, rate, accuracy, specificity, and recall. TP and TN Figure 5. GUI software. The presented ECG in GUI is taken from patient in real-time.

Evaluation Methods
True-Positive (TP), False-Positive (FP), True-Negative (TN), and False-Negative (FN) are the four quantities required to calculate the classification evaluating parameters such as sensitivity, precision, detection error, rate, accuracy, specificity, and recall. TP and TN represent the total number of correctly classified R and False-R. FP and FN indicate the inaccurately detected R and False-R. The evaluation parameters are computed with Equations (17)- (22).
The Heart Rate Variability (HRV) is the evaluating parameter for R peak location accuracy. Three types of HRV, i.e., mean of R-R intervals (MRR), the standard deviation of normal-to-normal R-R intervals (SDNN), and root mean square of successive heartbeat interval differences (RMSSDs) are computed with the Equations (23)- (25).
where R i n is the R-R interval between n th and n th − 1 peak.

Experimental Tools and Environment
This study used a computer with Intel Core i9-9900K CPU (Intel Corporation, Santa Clara, CA, USA), 16 GB of memory, and Ubuntu 20.04 LTS as OS. MATLAB R2020b, Simulink, signal processing and machine learning toolbox are used for signal analysis, machine learning models, simulations, and system modeling. The algorithm is deployed into a practical instrument with an AD8232 EKG sensor module, Arduino board, and Jetson Nano. The desktop-based graphical user interface (GUI) application is deployed to the Jetson Nano micro-computer. This software and device are used to detect real-time R peaks on persons.

Results and Discussions
The proposed methods' performance was computed using the MIT-BIH arrhythmia database. For a reasonable evaluation of performance, this study accesses section-wise performance and presented in (a) QRS Component Extraction, (b) ROI estimation, (c) Feature Processing, and (d) Performance of RLC. Finally, the proposed approach is evaluated in comparison to alternative methods in Section 3.5.

Performance of QRS Component Extraction
The VMD method decomposes an ECG signal into nine variational mode functions (VMFs), and the Hilbert Transform (HT) converts the modes into time-frequency-energy, shown in Figure 6. In this example signal (Figure 6a), there are nine QRS complexes. It has been observed that in VMF-6, there are nine prominent peaks with low noises. Therefore, the VMF-6 represents the best time, frequency, and energy for this signal, although it can be different for different ECGs. The DMSE algorithm determines the VMFs which contain the best QRS data; otherwise, noises are considered. It suppresses all unnecessary waves like P, T, and U from the ECG signal presented in Figure 7. In this example signal (Figure 6a), there are nine QRS complexes. It has been observed that in VMF-6, there are nine prominent peaks with low noises. Therefore, the VMF-6 represents the best time, frequency, and energy for this signal, although it can be different for different ECGs. The DMSE algorithm determines the VMFs which contain the best QRS data; otherwise, noises are considered. It suppresses all unnecessary waves like P, T, and U from the ECG signal presented in Figure 7.
For estimating the DMSE algorithm's performance, this study counts the real, missing, and false R peaks in the integral function of the QRS component. There are 15 types of heartbeats in the MIT-BIH arrhythmia database and a total of 109,494 beats. Aberrated Atrial Premature, Fusion, Paced, Premature Ventricular Contraction, APC and Normal types suffer a loss of 0.66, 0.12, 0.07, 0.084, 0.039, 0.059%. The rest are nine types of beats and suffer zero loss. Considering the True beat counts, the mean error of the DMSE algorithm is 0.0538%. A total of 22,145 false R peaks were detected. The DMSE algorithm removes 89.887% of P, T, and noise-associated peaks. We present the performance of the DMSE algorithm in Table 2. Considering the True beat counts, the mean error of the DMSE algorithm is 0.0538%. A total of 22,145 false R peaks were detected. The DMSE algorithm removes 89.887% of P, T, and noise-associated peaks. We present the performance of the DMSE algorithm in Table 2.

Performance of ROI Estimation
The purpose of the AWS algorithm is to obtain the accurate feature of QRS. Therefore, a feature set with a lower error represents the performance of the AWS algorithm. This approach used the sequential forward selection (SFS) approach for choosing the best features. The SFS method measures the feature importance or criterion one by one and includes it into the selected subsets. An ensemble decision tree algorithm was chosen for classification based on the feature distribution as shown in Figure 8c. Normal  982  982  0  Unclassifiable Beat  33  33  0  Total 109,494 131,580 59

Performance of ROI Estimation
The purpose of the AWS algorithm is to obtain the accurate feature of QRS. Therefore, a feature set with a lower error represents the performance of the AWS algorithm. This approach used the sequential forward selection (SFS) approach for choosing the best features. The SFS method measures the feature importance or criterion one by one and includes it into the selected subsets. An ensemble decision tree algorithm was chosen for classification based on the feature distribution as shown in Figure 8c. This study has eight subsets of features from twenty-five features. The mean squared error of each subset reveals the performance of each subset. We achieved the lowest MSE This study has eight subsets of features from twenty-five features. The mean squared error of each subset reveals the performance of each subset. We achieved the lowest MSE 0.001 for the eighth subset and the highest 0.0077 MSE for the first subset. In Figure 9, the adaptive feature window and performance of the AWS algorithm are presented. 0.001 for the eighth subset and the highest 0.0077 MSE for the first subset. In Figure 9, the adaptive feature window and performance of the AWS algorithm are presented.

Performance of Detection
The sensitivity, specificity, precision, recall, detection error rate (DER), and accuracy are measured for evaluating the classification performance. The sensitivity and specificity represent the ability to classify R and False-R accurately. Precision reports the rate of accurately classified R among all detected R. On the other hand, recall tells the rate of accurately detected R and False-R. DER notifies the ratio of incorrect and correct classification of R and False-R. Finally, accuracy represents the percentage of correct classification. The overall classification performance of the proposed method is measured by the area under the ROC curve (AUC).
The classifier model is evaluated with a 10-fold cross-validation dataset and test dataset. To analyze the performance in a real-time scenario, 1.5 s long window with 0.5 s overlapping was considered as a sample signal. Thus, R peaks in each signal file of MIT-BIH arrhythmia database are predicted, representing the actual performance of our proposed model.
The confusion matrix and performance evaluating parameters for the 10-fold validation, test, and MIT-BIH are presented in Tables 3 and 4.

Performance of Detection
The sensitivity, specificity, precision, recall, detection error rate (DER), and accuracy are measured for evaluating the classification performance. The sensitivity and specificity represent the ability to classify R and False-R accurately. Precision reports the rate of accurately classified R among all detected R. On the other hand, recall tells the rate of accurately detected R and False-R. DER notifies the ratio of incorrect and correct classification of R and False-R. Finally, accuracy represents the percentage of correct classification. The overall classification performance of the proposed method is measured by the area under the ROC curve (AUC).
The classifier model is evaluated with a 10-fold cross-validation dataset and test dataset. To analyze the performance in a real-time scenario, 1.5 s long window with 0.5 s overlapping was considered as a sample signal. Thus, R peaks in each signal file of MIT-BIH arrhythmia database are predicted, representing the actual performance of our proposed model.
The confusion matrix and performance evaluating parameters for the 10-fold validation, test, and MIT-BIH are presented in Tables 3 and 4.

Performance of RLC
The peaks are detected from an integral function of instantaneous energy. During the signal decomposition, the exact locations of peaks are slightly shifted due to the delay of the Wiener filter used in VMD. The RLC algorithm updates the position of the detected R to the exact place. The output of the RLC algorithm is presented in Figure 10.

Performance of RLC
The peaks are detected from an integral function of instantaneous energy. During the signal decomposition, the exact locations of peaks are slightly shifted due to the delay of the Wiener filter used in VMD. The RLC algorithm updates the position of the detected R to the exact place. The output of the RLC algorithm is presented in Figure 10. This study observed that a few R peaks are detected at inaccurate positions. Despite the correction of the detected position, errors still exist. For measuring the RLC algorithm's performance, this study calculates MMR, SDNN, and RMSSD from R-R intervals This study observed that a few R peaks are detected at inaccurate positions. Despite the correction of the detected position, errors still exist. For measuring the RLC algorithm's performance, this study calculates MMR, SDNN, and RMSSD from R-R intervals extracted from R peaks. A precise "R" position is required to compute R-R distance because it is used to compute the heart rate. The heart rate variability is a vital diagnosis measurement for analyzing CVDs. The mean R-R intervals (MRR), the standard deviation of R-R intervals (SDNN), and the root mean square of R-R intervals are three significant parameters for evaluating the R positional accuracy.
The proposed approach yielded an error of 0.0048, 0.0276, and 0.042 in MRR, SDNN, and RMSSD, comparing physicians.

Performance Comparison
The proposed method has shown significant performance improvement with an accuracy of 99.94%, which has a sensitivity of 99.98%, positive predictability of 99.96%, and a detection error rate of 0.06%. The results are comparable to the best results published in the literature regarding classification performance and HRV. The proposed method's success on the MIT-BIH arrhythmia database compared to other existing methods is in Table 5.

Conclusions
This proposed method's novelties are fourfold: (1) The proposed DMSE algorithm extracts the best integral function of QRS energy from the ECG signal. (2) The proposed AWS algorithm improved in extracting features of the QRS by suppressing the possibility of mixing the P and T waves features and enhancing the QRSs' feature only. (3) The proposed RLC algorithm improves R position accuracy. (4) As a result of the DMSE and AWS algorithm, this method significantly improves classifying "R" peaks in the ECG signal. The VMD and HT are used to extract the frequency and energy adaptively and non-recursively. The accuracy of the power spectrum is very high, except for some edge errors. This study overcomes the mode selection by the proposed DMSE algorithm and edge errors by the dropout method. The feature was extracted from a localized position, and the proposed AWS algorithm fixed the window size adaptively. Then, sixteen decision trees are ensembled to distinguish R peaks from other peaks. Finally, a location correction method is used to update to the actual location. The proposed method achieved better performance in sensitivity of 99.98%, the precision of 99.96%, DER of 0.06%, and accuracy of 99.94% than other existing methods.