Fast T Wave Detection Calibrated by Clinical Knowledge with Annotation of P and T Waves

Background There are limited studies on the automatic detection of T waves in arrhythmic electrocardiogram (ECG) signals. This is perhaps because there is no available arrhythmia dataset with annotated T waves. There is a growing need to develop numerically-efficient algorithms that can accommodate the new trend of battery-driven ECG devices. Moreover, there is also a need to analyze long-term recorded signals in a reliable and time-efficient manner, therefore improving the diagnostic ability of mobile devices and point-of-care technologies. Methods Here, the T wave annotation of the well-known MIT-BIH arrhythmia database is discussed and provided. Moreover, a simple fast method for detecting T waves is introduced. A typical T wave detection method has been reduced to a basic approach consisting of two moving averages and dynamic thresholds. The dynamic thresholds were calibrated using four clinically known types of sinus node response to atrial premature depolarization (compensation, reset, interpolation, and reentry). Results The determination of T wave peaks is performed and the proposed algorithm is evaluated on two well-known databases, the QT and MIT-BIH Arrhythmia databases. The detector obtained a sensitivity of 97.14% and a positive predictivity of 99.29% over the first lead of the validation databases (total of 221,186 beats). Conclusions We present a simple yet very reliable T wave detection algorithm that can be potentially implemented on mobile battery-driven devices. In contrast to complex methods, it can be easily implemented in a digital filter design.

database [20,21]. Moreover, a new fast robust algorithm consisting of two moving averages that are calibrated by a clinical knowledge base is presented.

Data Used
Several standard ECG databases are available for the evaluation of QRS detection algorithms for ECG signals. Most of these databases contain annotated files for R peaks but not for T waves. To demonstrate the applicability of the algorithm presented in this paper, two databases are used in this study: one self-annotated database and one standard annotated database.

Database Annotated Under This Study
An expert manually annotated the P and T peaks of the MIT-BIH Arrhythmia database [20,21] to be used in evaluation for the following reasons: 1. The MIT-BIH database contains 30-min recordings for each patient, which is considerably longer than the records in many other databases, such as the Common Standards for Electrocardiography database, which contains 10-s recordings [22]. 2. The MIT-BIH Arrhythmia database contains records of normal ECG signals and records of ECG signals that are affected by non-stationary effects, low SNR, premature atrial complexes, premature ventricular complexes, left bundle blocks, and right bundle blocks. This provides an opportunity to test the robustness of T wave detection methods. 3. The database contains 23 records (the "100 series") that were chosen at random from a set of more than 4000 24-h Holter tapes, and 25 records (the "200 series") that were selected from the same set, including a variety of rare and clinically important ECG segments [20]. Several records in the 200 series have abnormal rhythms and QRS morphologies and they suffer from a low SNR. These issues are expected to present significant difficulties for any ECG signal analysis algorithm [20].  demonstrate examples for annotation of T waves for different beats in the MIT-BIH Arrhythmia database. There was no automated aid provided during the annotation process and only channel one (Lead I) was annotated. For special cases, such as biphasic T waves, the middle point of the wave was considered as a T wave. The annotation file of P and T waves can be downloaded from [23].

Standard Annotated Database
As the MIT-BIH database is self-annotated, the validation of the detector must be carried out using a standard annotated database. For this purpose, the easily-available QT database [24] is used. This database was annotated by two cardiologists and includes different morphologies such as ST change, supraventricular arrhythmia, normal sinus rhythm, sudden death, and long-term ECG signals. The two cardiologists annotated only selected beats (3542 beats in a file called ".q1c") in all recordings except two recordings: "sel35" and "sel37". However, the automatic annotation of the whole database was carried using ecgpuwave software, which is saved in the ".pu" file. In this work, the T peaks of Lead I of the whole QT database are used for validation as they are more salient and certain compared to the onset and offset. In this work, the T peaks of the whole QT database are used for validation as they are more salient and certain compared to the onset and offset. Moreover, once the T peak is detected correctly, searching for the onset and offset is a relatively easy step; however, this is not the focus of this study. 178 Figure 1. Annotation of P and T waves in normal beats. Here, "+" represents the P wave and "*" represents the T wave. Note that the higher peaks represent the QRS complex.  Figure 3. Annotation of P and T waves in unusual beats. Each row contains two different morphologies for a certain type of unusual beats: (a) unclassified beats; (b) nodal premature beat; (c) nodal escape beat; (d) fusion of ventricular and normal beat; (e) fusion of paced and normal beat. Here, "+" represents the P wave and "*" represents the T wave, while the green circle with asterisk represents merged P and T waves.

T Wave Detection Algorithm
In this study, a fast robust knowledge-based T wave detection algorithm is discussed and evaluated. The algorithm is based on the framework proposed by Elgendi for detecting QRS complexes in ECG signals [25,26], for detecting systolic waves in photoplethysmogram signals [27], detecting a waves [28], and detecting c, d, e waves [29] in acceleration photoplethysmogram (PPG) signals. We build upon this approach to detect T waves. The method consists of three main stages: pre-processing (clinical knowledge, bandpass filtering, squaring, and QRS removal), feature extraction (generating potential blocks using two moving averages), and classification (thresholding). The structure of the algorithm is given in Figure 4.   Figure 4. Structure of the T waves detection algorithm. The algorithm consists of three main stages: pre-processing (clinical knowledge, bandpass filtering, squaring, and QRS removal), feature extraction (generating potential blocks using two moving averages), and classification (thresholding).

Bandpass Filter
Most of the energy of T waves lies below 10 Hz [30,31]; thus, a zero-phase second-order Butterworth filter, with a 0.5-10 Hz bandpass, is implemented to remove the baseline wander and high frequencies that do not contribute to the T waves. The output of the zero-phase Butterworth filter applied to the ECG signal produces a filtered signal x[n].

QRS Removal
The QRS removal step based on the relative distance of the T wave to its associated R peak. Removing the QRS complex has two advantages: (1) T waves become the dominant feature in the processed signal; and (2) it simplifies the search for T waves relative to the position of R peaks. In this study, as proof of concept, the R peaks provided in the MIT-BIH Arrhythmia and QT databases are used. Removing the QRS complex is performed by setting the signal to zero for the duration of the QRS complex.
The limits (thresholds) of the RT distance are determined using ECG clinical knowledge. The signal y[n] is initialized as equal to the filtered ECG x[n] signal. The QRS removal length thresholds are determined based on the clinical phases of the RR intervals. As the duration of the QRS complex varies with the heart beat type, a clinical database is required to remove the QRS complex, according to its type. Roskamm and Csapo divided the ECG into four phases: compensation, reset, interpolation, and reentry [32]. Based on their analysis, in the compensation phase (Figure 5a), the second beat (850 ms) is followed by a prolonged beat (1150 ms) to compensate the two beats duration (2000 ms). During the reset phase (Figure 5b), the second beat (650 ms) is followed by a prolonged beat (1150 ms), while in the interpolation (Figure 5c), the second beat (400 ms) is followed by an irregular beat (600 ms). Finally, in the reentry phase (Figure 5d), the second beat (300 ms) is followed by a rapid irregular beat (400 ms); however, an extra category is added to capture complex arrhythmias (repetitive, bigeminy, or trigeminy). The output of this stage will produce an updated y[n] signal.  Figure 5. Types of sinus node response to atrial premature depolarization (adapted from [32]).
Based on the clinical information presented in Figure 5, the ratio of the second beat duration to the first and third beat duration generates a rule-based knowledge representation, as shown in Figure 6. For example, in the case of reentry (left branch of the flowchart in Figure 6), the first threshold is calculated by dividing the duration between the third and the fourth beat (R i+2 − R i+1 = 400 ms) by the duration between the two and the third beat (R i+1 − R i = 300 ms) resulting as 1.33; however, this value was calculated with reference to the duration between first beat and second beat (R i − R i−1 = 1000 ms)-this generates the first rule: The second threshold is the expected total duration of the second and third beats (300 ms + 400 ms), which equals 0.7 s-this generates the second rule: (RR1 + RR2) ≤ 0.7f s . On the right side the flowchart in Figure 6, in the case of reentry, the first threshold is calculated by dividing the duration between the third and the fourth beat (R i+1 − R i = 300 ms) by the duration between the first and the second beat (R i − R i−1 = 1000 ms) resulting as 0.3-this generates the first rule: RR2 ≤ 0.3 RR1, while the second threshold is based on the duration between the first and the second beat (R i − R i−1 = 1000 ms) which is 1 s (or f s )-this generates the second rule: RR1 ≤ f s . Similarly, all other rules were created for the other four ECG phases.  During the QRS removal, the RR interval that satisfied each category is saved and referred to as RR k , where k is the category type (compensation, reset, interpolation, reentry, and complex arrhythmias). The normalized RR intervals average in each category is calculated as M c = ( l j=1 RR c,j )/(lf s ), where l is the number of RR intervals saved in category c, and f s is the sampling frequency. Dividing M c by f s is sufficient as it is equivalent to the average RR interval in healthy subjects.

Generating Blocks of Interest
Blocks of interest are generated using two event-related moving averages that demarcate the areas of T waves, a method which was first introduced in [33]. The particular method used to generate blocks of interest has been mathematically shown to detect a waves [28], QRS complexes [25], and systolic waves in PPG signals [27]. In this procedure, the first moving average (MA peak ) is used to emphasize the peak of the T wave area, as the dotted signal shown in Figure 7, and is given by where W 1 represents the window size of approximately the peak duration of the T wave in ECG signals. The initial value for W 1 of 70 ms is determined by Trahanias [34]. However, as the ECG signals may contain different arrhythmias the value of W 1 will be calculated relative to the most frequent RR intervals in all five categories (k = max c M c ). Then, the value of W 1 = (70 ms × f s ) × k, and the result is rounded to the nearest odd integer. The second moving average (MA Twave ) is used to emphasize the T wave area to be used as a threshold for the first moving average, shown as a dashed signal Figure 7, and is given by where W 2 represents the window size of approximately the T wave duration. The initial value for W 2 of 140 ms is determined by Laguna et al. [35]. However, as the ECG signals may contain different arrhythmias, the value of W 2 will be calculated relative to the most frequent RR intervals in all five categories (k). Then, the value of W 2 = (140 ms × f s ) × k, and the result is rounded to the nearest odd integer. For example, the total values of W 1 and W 2 for detecting T waves in record 100 from MIT-BIH Arrhythmia database were 20 samples (55.6 ms) and 40 samples (111.2 ms); respectively.  . Demonstrating the effectiveness of using two moving averages to detect T waves. The dotted line is the first moving average, while the dashed line is the second moving average. The red circle is the detected T wave peak.

Thresholding
In this stage, the blocks of interest are generated by comparing the MA peak signal with MA Twave . Many blocks of interest will be generated, some of which will contain the T wave and others will contain P waves, U waves, and noise. Therefore, the next step is to reject blocks that result from noise. Rejection is based on the relative positions of P and T waves to R peaks and anticipated peak width.
To determine whether the detected blocks contain T waves or not, the number of blocks in each consecutive RR interval is counted. A threshold based on the distance of the maximum point within a block to the R peak is applied to distinguish P waves from T waves and noise, as shown in Figure 8. The search regions for T waves in terms of time occurrence with respect to the current R peak (R i ) and the next R peak (R i+1 ) are calculated as where R i T min represents the minimum dynamic interval between the T wave and the current R peak, R i T max represents the maximum dynamic interval between the T wave and the current R peak, while R i R i+1 represents the interval between R i and R i+1 . The exact values for D min and D max are 170 ms and 800 ms, respectively, as determined by Schimpf et al. [36] to represent the minimum RT durations for subjects with arrhythmia and maximum RT duration for healthy subjects. All detected blocks go through a durational threshold to reject the undesired blocks called THR 1 , which rejects the blocks that contain P wave, U wave, and noise. By applying the THR 1 threshold, the accepted blocks will contain T peaks only, After applying the relative-position thresholds, there are three possibilities for the number of detected blocks within the area of interest: 1. Zero: if there is no block detected, it means the algorithm failed to detect a T wave in the current RR interval. 2. One: if there is one detected block, it means the algorithm succeeds in detecting T wave, P and T waves are most likely merged within one block, which is marked as a circle with a black asterisk inside (see Figure 9i,j). 3. More than one: if there are multiple detected blocks, as shown in Figure 7, it means one of the detected blocks contains T waves. However, in this work the nearest block to the current R peak is considered a T wave.
The last stage is to find the maximum absolute value within each block to detect the peak of T wave. The detected T wave peaks are compared to the annotated T wave peaks to determine whether they were detected correctly. The search range for the true T wave peak is fixed to ±30 ms for both databases, to ensure consistency of comparison. The search region of 30 ms is good enough for diagnostics as it is less than W 1 . Figure 8. Search regions for T waves in terms of time occurrence with respect to the current R peak (R i ) and the next R peak (R i+1 ). Where R i T min represents the minimum interval between the T wave and current R peak and R i T max represents the maximum interval between the T wave and the current R peak. (j) premature atrial contraction (PAC) beats from record 209. Here, the empty red circle represents the detected T wave while the a circle with a black asterisk represents detection of merged P and T waves.

Results
The algorithm was evaluated using the MIT-BIH database. The T waves were detected successfully even when the P and T waves are merged in Arrhythmia ECG signals that are affected by: high-frequency noise, baseline wander, normal sinus rhythm (NSR), left bundle branch block (LBBB), right bundle branch block (RBBB), premature ventricular contraction (PVC), and premature atrial contraction (PAC). All of the reasons for detection failure are described below. High-frequency noise results from the instrumentation amplifiers, recording system, and ambient electromagnetic signals received by the cables. The signal shown in Figure 9a has been corrupted by power-line interference at 60 Hz and its harmonics and other high frequencies. It can be seen that the proposed algorithm is robust to noise. Moreover, the proposed algorithm is not sensitive to baseline wander and detected the T waves correctly, as shown in Figure 9b. This is because the moving averages were applied to the bandpass filtered ECG signal-which is discussed in the Bandpass Filter Subsection.
The NSR is a normal ECG cycle; it is initiated by the sinoatrial node and consists of a P wave followed, after a brief pause, by a QRS complex and then a T wave [37]. The proposed algorithm correctly detected T waves in three types of normal beats: (1) NSR without U waves (record 100 of the MIT-BIH database), as shown in Figure 9c; (2) NSR with U waves (record 103), as shown Figure 9d; and (3) NSR with negative polarization (record 108), as shown Figure 9e. The LBBB results from conduction delays or blocks at any site in the intraventricular conduction system, including the main LBBB and the bundle of His. The result of an LBBB is an extensive reorganization of the activation pattern of the left ventricles [37]. The proposed algorithms successfully detected normal and merged P and T waves in two types of LBBBs: (1) LBBB beats with merged P and T waves (record 109), as shown in Figure 9f and (2) LBBB beats with normal T waves (record 111), as shown in Figure 9g. However, RBBB is a result of a conduction delay in a portion of the right-sided intra-ventricular conduction system. The delay can occur in the main RBBB itself, in the bundle of His, or in the distal right ventricular conduction system. The RBBBs may be caused by a minor trauma, such as right ventricular catheterization [37]. As shown in Figure 9h, the proposed algorithms succeeded in detecting the T waves in ECG signals of RBBB (record 118).
The PVCs are characterized by the premature occurrence of a QRS complex that is abnormal in shape, and that has a longer duration than normal QRS complexes, generally exceeding 120 ms [37]. The T wave is commonly large and opposite in direction to the major deflection of the QRS. In general, the QRS complex is not preceded by a P wave, but it can be preceded by a non-conducted sinus P wave occurring at the expected time [37]. In Figure 9i, a special case of PVC is shown, called bigeminy, where the premature ventricular beats occur after every normal beat in an alternating pattern.
The proposed algorithm succeeded in detecting the T waves in the normal beats and the T waves in the premature ventricular beats (record 200). Note that PACs are among the most common causes of irregular pulses and can originate from any area in the heart [37]. The impulse is discharged prematurely by an irritable focus in the atria giving rise to a distorted P wave, usually superimposed on the preceding T wave. As shown in Figure 9j, the proposed algorithms detected the merged T waves in PACs (record 209). As illustrated in Figure 9, the proposed method successfully detected T waves in ECG signals with a low SNR, baseline wander, and various arrhythmias. The performance of the T wave detection algorithms is evaluated using two statistical measures: SE = TP/(TP + FN) and +P = TP/(TP + FP), where TP is the number of true positives (T wave peak detected within the range of 30 ms of the annotated T wave peak), FN is the number of false negatives (annotated T wave peak has not been detected), and FP is the number of false positives (T wave peak detected outside the range of 30 ms of the annotated T wave peak). The sensitivity SE reports the percentage of true beats that were correctly detected by the algorithm. The positive predictivity +P reports the percentage of beat detections that were true beats.
The abnormal heart rhythms caused a large number of FNs compared to the FPs. Table 1 shows the result of T wave detection over 48 records of the MIT-BIH database. FNs are mainly caused by noise and PVC, as in record 219, and atrial fibrillation, as in record 202. The algorithm achieved a sensitivity of 99.86% and a positive predictivity of 99.65%, which are promising results for handling the non-stationary effects, low SNR, PACs, PVCs, LBBBs, and RBBBs in ECG signals.

Comparison of Performance on the QT Database
The detection performance on the QT database obtained by the proposed T wave detector record by record performance is shown in Tables 2 and 3. The overall comparison of our results with the existing T wave detection algorithms on the QT database is demonstrated in Table 4. It summarizes the performances in terms of number of beats, methodology, SE, and +P. Note that the proposed algorithm scored slightly higher overall performances (average of SE and +P) than Martínez et al. [38] and Laguna et al. [39] over the manually annotated T waves. It is clear that the proposed algorithm succeeds in handling long ECG recordings with high performance over the 111,201 automatically annotated heart beats. Moreover, the proposed T wave detector has not been re-tuned over any databases, thus the results are promising, and the algorithm can detect T peaks over different databases, sampling frequencies, types of arrhythmias, and noise.

Processing Time
Less computational time is achieved when the simplest method is used (i.e., the algorithm presented in this paper requires less computational time). This is advantageous in terms of future development of wearable and portable diagnostic devices, and in terms of helping online and real-time diagnoses.
It is misleading to suggest that mentioning the average speed of the proposed detector, over a certain time length of ECG signal, would provide a comparative result. This is because the processing time depends on the number of beats within each ECG recording, not on the record length. In this study, the T wave detector was implemented in MATLAB 2010b (The MathWorks, Inc., Natick, MA, USA) on Intel TM i5 CPU 2.27 GHz.
It is worth noting that the number of beats of the 15-min recordings category in QT database was relatively consistent-with a mean ± SD, number of beats 1059 ± 275-over all records of this category. On the contrary, the 30-min beat average, in MIT-BIH database, was 2291 with an SD of 448 beats. As the processing time depends on the number of beats rather than the recording length [25], we found, for example, the proposed detector took 2.58 s to process record 215-MITDB contains 3362 beats, while it took 0.28 s to process 527 beats in record 33-QTDB. In general, without taking the number of beats into consideration, the speed of the proposed detector is fast. The suggested detector handles 15-min recordings in 0.52 ± 0.14 s, while it takes 1.98 ± 0.32 s to handle 30-min ECG recordings. Table 1. T wave peak detection performance over the annotated MIT-BIH Arrhythmia database [20,21]. To evaluate the performance of the T wave detection algorithm, two statistical measures are used: SE = TP/(TP + FN) and +P = TP/(TP + FP), where TP is the number of true positives (T wave peak detected within the range of 30 ms of the annotated T wave peak), FN is the number of false negatives (annotated T wave peak has not been detected), and FP is the number of false positives (T wave peak detected outside the range of 30 ms of the annotated T wave peak).

Record
No of beats  Table 2. T wave peak detection performance over the manually annotated 11 recordings of the QT database [24]. To evaluate the performance of the T wave detection algorithm, two statistical measures are used: SE = TP/(TP + FN) and +P = TP/(TP + FP), where TP is the number of true positives (T wave peak detected within the range of 30 ms of the annotated T wave peak), FN is the number of false negatives (annotated T wave peak has not been detected), and FP is the number of false positives (T wave peak detected outside the range of 30 ms of the annotated T wave peak).  Table 3. T wave peak detection performance over the automatically annotated QT database [24]. To evaluate the performance of the T wave detection algorithm, two statistical measures are used: SE = TP/(TP + FN) and +P = TP/(TP + FP), where TP is the number of true positives (T wave peak detected within the range of 30 ms of the annotated T wave peak), FN is the number of false negatives (annotated T wave peak has not been detected), and FP is the number of false positives (T wave peak detected outside the range of 30 ms of the annotated T wave peak).  Table 4. T waves detection performance comparison on the QT database [24]. (N/R: not reported).

Limitations of Study and Future Work
The preliminary results are promising, especially after testing the algorithm on the QT database; however, testing the algorithm on a larger sample size is necessary to generalize the findings. In addition, a more focused study is needed to investigate atrial fibrillation, atrial flutter, paroxysmal supraventricular tachycardia, junctional tachycardia, and multifocal atrial tachycardia as the morphology of T waves may differ.
The presented method assumes that the R peaks are correctly detected. There is a linear correlation between the detection of T waves and the detected R peaks. If the R peaks are misclassified, this method will fail as it depends on the position of R peaks. However, this study provides a positive proof of concept for detecting T waves in arrhythmic ECG beats.
We created a rule-based system based on the three RR interval window proposed in [32]. Perhaps, it is important to investigate the development of a rule-based system based on more than three RR intervals to examine if it will improve the overall T wave detection accuracy. Moreover, the proposed method is based on several thresholds, which are calibrated using clinical knowledge. Clinical knowledge is considered the gold standard in our work; thus, the obtained thresholds were considered optimal.
However, an open question for future work is to optimize all thresholds to improve accuracy.
One of the next steps regarding the results of this study is to detect arrhythmic ECG beats using the RT or ST interval as a main feature. In addition, the detection of P waves based on the accurate detection of T wave peaks needs to be examined. Moreover, perhaps, an optimization over the clinical parameters after splitting the databases into a training set and test set may improve the detection rate of the T waves. There is also a need to investigate the T-waves with different morphology, e.g., biphasic T-waves.
Technically, exploring the event-related moving average methodology for detecting events in ECG signals is promising in terms of computational complexity and efficiency. This can be further improved by investigating other bandpass filters with different orders and also by developing fast-moving average (or median) techniques for real-time analysis and mobile phone applications.

Conclusions
There is a limitation when evaluating T wave detection algorithms as datasets with annotated T waves are lacking. Consequently, comparing existing algorithms becomes even more difficult. Therefore, annotation of T waves is discussed and provided. Using these approaches, it is possible to support diagnostic analysis, delivering important information for (differential) diagnosis to medical experts. Using mobile technologies with automatic analysis software driven by medical expert knowledge, it will be furthermore possible to provide screening and monitoring solutions in places where medical expertise is scarce, such as remote rural areas and developing countries.
The use of two moving averages is simple and computationally efficient for mobile electronic health tools, such as cell phones and telemedicine technologies. The assessment of the T detector has been reliably carried out over the existing standard databases (QT and MIT-BIH), which contain different beat types and morphologies found in ECG signals. The developed algorithm was evaluated on all ECG recordings in the MIT-BIH database, 48 self-annotated records containing a total of 109,985 heart beats. It achieved a sensitivity of 95% and a positive predictivity of 98.59% over the MIT-BIH ECG signals, which contain low SNR, baseline wander, paced beats, and various arrhythmias. Interestingly, the proposed algorithm succeeded in scoring the highest overall accuracy of 98.84% over the manually annotated QT database (3542 heart beats) when compared to the other three algorithms (cf. Table 4). Moreover, the algorithm scored a high overall accuracy of 96.7% over the automaticlly annotated QT database (111,201 heart beats). Overall, simplicity and efficiency are required in developing T wave detection algorithms for processing long-term recordings and large databases as well as for expanding our telemedicine capabilities in the near future.