**5.1. ECG signals for multi-classification**

**Algorithm 1.** ECG R wave peak location algorithm.

3: read ECG data into ECG data buffer *ecg*\_*v* from ECG data file *ecg*\_*data*\_*file*;

8: Look for R wave peak in small area of [*rp lp*] in range of [*tp*, *bp*] using template;

4: calculate segment number *N =* ⌈*L*/*hstep*⌉ where *L* =length(*ecg*\_*v*);

6: let search range in vertical direction equal start position;

9: **if** *isfindpeak*() // to decide whether find the target.

13: Update range of [*tp*, *bp*] for next iteration;

18: **return** ECG data array v, R wave peak array *ecg*\_*pos*;

sampling unit lies on the assumption of Z ∽ Ν(μ, σ<sup>2</sup>

7: **while not** *bfind* **and** *tp* > 0 **and** *bp* > 0 **do**

10: Save the result to *ecg*\_*pos*;

16: update *rp* and *lp* respectively;

2: **initial**: set segment length *hstep* and searching step *vstep*, empty ECG data buffer *ecg*\_*v*[*M*] and R wave peaks array

**Figure 2.** Neural network structure of VAE. It consists of three parts: The encoder, the decoder, and the sampling unit. The encoder (indicating by number 2) and the decoder (indicating by number 6) are all fully connected multilayers neural networks. The sampling unit consists of the mean generator (indicating by number 3), the standard deviation generator (indicating by number 4), and the latent vector generator (indicating by number 5). The structure of the

).

1: **input:** ECG data file name *pa*

80 Machine Learning and Biometrics

5: **for** each segment *s* in *N*

11: **break**; 12: **else**

14: **end if** 15: **end while**

17: **end for**

*ecg*\_*pos*[*m*];

To demonstrate the performance of our models on dealing with ECG signals, it is necessary to abstract an intact ECG signal in a cardiac period, which consists of features such as P-wave, QRS complex, and T-wave as described in Section 4. Then detection and location of P-wave becomes more critical step as every cardiac period of ECG signal starts at P-wave. However, as the amplitude of P-wave is smaller than that of QRS complex, and there are many kinds of noise on ECG singles. These factors enlarge the difficulties of abstraction of ECG signals in a cardiac period.

Our solution to alleviate this problem is offered by the fact that it is more feasible to locate R-peaks than to locate the start position of a P-wave. Instead of focusing on the cardiac period, we separate one cardiac period into two semi-cardiac periods at R-peak and then take two parts of the adjacent ECG signals together to form a new period ECG signal, which consists of the second part of the previous cardiac period and the first part of the next one. **Figure 4(a)** shows an example of an ECG signal that is composed of two parts of the adjacent semi-period. Additionally, in the view of information, there is no any feature lost in this separation.

The original ECG recording from ECG database contains several hours of ECG data, and it is unfeasible to train our models using these original ECG data directly. To train our models well, 30,000 ECG signals are abstracted completely from three different ECG databases. The AHA ECG database, the APNEA ECG database [24], and CHFDB ECG database [24]. Additionally, for ECG data augmentation [32], these ECG data are divided into three different groups according to their source databases and each group has 10,000 ECG signals. On this basis, we augment the ECG data by zeroing a small segment on ECG signals and different positions we selected to zero correspond to different class labels. **Figure 4(b)**–**(d)** are three examples of our augmentation. Concretely, the labels of **Figure 4(b)**–**(d)** are 3, 4, and 5, respectively. (We use numbers 1–8 as eight labels for different class of ECG signals in all of our experiments. We add labels for the different classes of ECG signals, not for training our models but for simplifying evaluating the accuracy of our models in testing process.)

To evaluate the properties of our models on denoising for ECG signals, different type noise on different level are added into the original ECG records. These noise include Gaussian noise, salt and pepper noise, and Poisson noise. Moreover, to imitate baseline wandering noise, different amplitude sinusoidal signals are superimposed on the original ECG signals. The coefficients of the sinusoidal signal are 0.01, 0.05, and 0.1, respectively in all of our experiments. **Figure 5** shows the ECG signals polluted by different noises. **Figure 5(a)** and **(c)** show the augmented ECG signals without adding noise except for some one polluted during sampling. **Figure 5(b)** shows ECG signal polluted by the sinusoidal noise and the Gaussian noise. The coefficients for the sinusoidal and for the Gaussian are all 0.01. Nevertheless, the coefficients for the sinusoidal and for the Gaussian are 0.05 and 1 as shown in **Figure 5(d)**. The mean and variance of the Gaussian noise are 0 and 0.01, respectively.

data as described in Section 5.1 (We call this group as MI dataset). In order to verify the performance of the VAE model on ECG signals, the parameters of the model are shown in the **Table 1**. **Table 2** shows the performance of the VAE model on recognizing these ECG signals from both BI and MI datasets. The results clearly show that the accuracies of recognition are higher than 95% for MI recorders and even more than 97% for BI recorders. In the view of the data complexity, the result is reasonable because the complexity of MI is much

**Figure 5.** Single periodic ECG signal polluted by different noises. (a) Original ECG signal without adding noise. (b) ECG signal of (a) with Gaussian noise. (c) Original ECG signal with a segment- flatness. (d) ECG signal of (c) contaminated by

Electrocardiogram Recognization Based on Variational AutoEncoder

http://dx.doi.org/10.5772/intechopen.76434

83

Advantages of VAE model on recognization ECG signals can be further shown by comparasion with other autoecoders such as CAE,DAE, and SAE mentioned in Section 2. In order to make the comparison be fair and reasonable, all of the parameters of the model are the same exept for that of the sampler in VAE model (the values of the parameters can be seen in **Table 1**). Moreover, the ECG records of BI and MI from ahadb database are used to train and test all the models. **Figure 6** shows the accuracy of the models on recognizing the ECG records. Both **(a)** and **(b)** in **Figure 6** take the rate of the representation to the input on size as variable. **Figure 6(a)** takes the BI ECG records from the ahadb as the datasource for the models. Conversely, the MI

higher than that of BI.

Learning rate 0.01

Optimizer AdamOptimizer

**Table 1.** Parameters of VAE model.

Gaussian and sine wave noise imitating basing line wander.

**Parameter name Value Comment**

Input size 400 Equal the length of signal h1 100 First layer of the encoder h2 10 Second layer of the encoder z-mean 2 Mean of the sampler z-variance 2 Variance of sampler

Function Log-sigma Logarithmic sigma

Batch size 100 Randomly select samples from the dataset
