# Sleep Apnea Classification Using the Mean Euler-Poincare Characteristic and AI Techniques
## 4 Methodology
### Pre-Processing  
The pre-processing stage is referred to as 'polynomial', and was dealt with as follows. First, the ECG cycle was divided into two interval parts, as our focus was on the part containing the P-waves and R-peaks, only one interval was selected. The function for this process is presented in (1).  
We identified the R-peaks using the Pan-Tompkins algorithm [45] to achieve this objective. Afterward, we split the ECG cycle from R-peak to R-peak using the middle points, \(m_{k}\), and a fixed threshold. Each ECG cycle preserved the main waves, i.e., P, Q, R, S, and T. Every cycle \(N_{j}\) was segmented two sections, \(P_{1}(C)=[m_{k}\quad R_{k+1}]\), and \(P_{2}(C)=[R_{k+1}\quad m_{k+1}]\). Upon completion of the process, the result was in the form of polynomials that correspond to the appropriate order \(D\). In these cases, we only used the \(P_{1}(C)\) polynomials.  
We assessed the normalized root mean square error (NRMSE) using Equation (10) to identify the optimal order of the random polynomials that best approximated the data.  
\[FIT=100\left(1-\frac{\left\|P_{1}(C)-\sum_{k=0}^{D_{1}(C)}\hat{a}_{k}(k,\tilde {\omega})X_{j}(k)^{k}\right\|}{\left\|P_{1}(C)-\bar{P_{1}}(C)\right\|}\right) \tag{10}\]  
where \(P_{1}(C)\) represents the section of the ECG containing the P-waves and R-peaks, \(\bar{P_{1}}(C)\) is the mean of each interval \(P_{1}\), and the inverse of the Vandermonde matrix is used to calculate the vector of estimated coefficients \(\hat{a}_{k}\)[46]. Our goal is to find the maximum FIT based on NRMSE. To do this, we evaluate a group of samples (100,000) from each of the 35 patients for the training set using polynomial orders ranging from 15 to 25 (\(D_{1}(c)=15,\ldots,25\)). To calculate the order of polynomials for the sections of the ECG from the R-peaks to the T-waves, we used Equation (10), and instead of \(P_{1}(C)\), we used \(P_{2}(C)\). Also, we used the integral absolute error (IAE) criterion. The results for \(P_{1}(C)\) and \(P_{2}(C)\) are shown in Figure 1(a),b with both criteria.  
The optimal polynomial order is 15 according to the FIT criterion and 21 based on the IAE criterion. It is later shown that the order of the polynomials significantly influences the final results, particularly during the classification process. Algorithm 1 shows the entire procedure used for selecting the orders and obtaining the polynomials \(P_{1}(C)\) and \(P_{2}(C)\). However, we only use the P-wave polynomials \(P_{1}(C)\) in the following steps.  
Figure 2: Polynomial order results for the sections of the ECG; sections \(P_{1}(C)\) and \(P_{2}(C)\). **(a)** Histogram of the polynomials of P-waves (blue) and T-waves (red) using FIT (Equation (10)). **(b)** Histogram of the polynomials of P-waves (blue) and T-waves (red) using the IAE criterion.  
```
0: ECG
0: Polynomials \(P_{1}(C)\) and/or \(P_{2}(C)\) Initialization;
1: Search the locations \(M_{j}\), \(j=1,\ldots,N_{c}\);
2: Locate the middle points \(m_{j}\);
3: Find the best order for \(P_{1}(C)\) and \(P_{2}(C)\) using Equation (10);
4: Estimate the polynomials \(I_{1}(c)\), \(c\in[m_{j},M_{j+1}]\), and \(I_{2}(c)\), \(c\in[M_{j+1},m_{j+1}]\);
5: Assess the results;
```  
**Algorithm 1** Algorithm for pre-processing the ECG signals