## 4 Two-Stage Method for Determining the Type of Human Breathing
### Algorithm
\[y=Asin(\omega t+\varphi)+c, \tag{2}\]  
```
1:functionTypeOfBreath(\(MarkersFile\))
2:for all\(i,j\in\{1,2,3\},i<j\)do:
3:for all\(t\) in \(MarkersFile\)do
4:\(d^{t}_{ij}=\sqrt{(X^{t}_{j}-X^{t}_{i})^{2}+(Y^{t}_{j}-Y^{t}_{i})^{2}+(Z^{t}_ {j}-Z^{t}_{i})^{2}}\)
5:\(d_{ij}\gets d^{t}_{ij}\)
6:\(d^{\prime}_{ij}=savgolFilter(d_{ij},15,5)\)
7:\(A_{ij}=\sqrt{2}std(d^{\prime}_{ij})\)
8:\(c_{ij}=\overline{d^{\prime}_{ij}}\)
9:\(f_{ij}=ffftreq(d^{\prime}_{ij})[1]\)
10:\(\omega_{ij}=2\pi f\)
11:\(\varphi_{ij}=0\)
12:\(sintw_{ij}(t)=A_{ij}sin(\omega_{ij}t+\varphi_{ij})+c_{ij}\)
13:\(<A^{\prime}_{ij},c^{\prime}_{ij},f^{\prime}_{ij},\omega^{\prime}_{ij},\varphi ^{\prime}_{ij}>=curveFit(sinw_{ij}(t),d^{\prime}_{ij})\)
14:for all\(i,j\in\{1,2,3\},i<j\)do:
15:\(params\leftarrow<A^{\prime}_{ij},c^{\prime}_{ij},f^{\prime}_{ij},c^{\prime}_{ ij},\varphi^{\prime}_{ij}>\)
16:\(params\leftarrow\frac{A^{\prime}_{ij}}{A^{\prime}_{23}},\frac{A^{\prime}_{12}}{ A^{\prime}_{13}},\frac{A^{\prime}_{23}}{A^{\prime}_{13}},\frac{c^{\prime}_{23}}{ c^{\prime}_{23}},\frac{c^{\prime}_{23}}{c^{\prime}_{13}},\frac{c^{\prime}_{12}}{ f^{\prime}_{23}},\frac{f_{12}}{f^{\prime}_{13}},\frac{f_{23}}{f^{\prime}_{13}}\)
17:\(params^{\prime}=stdScale(params)\)
18:\(predictedType=model_{1}.predict(params^{\prime})\)
19:if predictedType == mixed_typethen
20: return\(model_{2}.predict(params^{\prime})\)
21:else
22: return\(predictedType\)
```  
**Algorithm 1** Algorithm of determining the type of human breath. One of the important hyperparameters for training a machine learning algorithm for determining the type of human breathing is the time window (the amount of data supplied to the algorithm for one run). We analyzed the overlay of the sine wave \(sinw_{ij}\) on the graph of the distance function between the two sensors \(d^{\prime}_{ij}\) (i.e., the breathing type graph). The graphs shown in Figure 18 present the results of the transformation of the function \(d^{\prime}_{ij}\) using the least squares method [45] for time windows of 5, 10, 30, and 50 s. You can see that the smaller the window, the better the \(sinw_{ij}\) function describes the dataset. Based on the graphs, the most appropriate window sizes are 5 and 10 s (when the \(sinw_{ij}\) graph almost entirely coincides with the \(d^{\prime}_{ij}\) graph). To find the optimal time window size, we conducted an experiment to determine how the time window affects the accuracy of the trained model. We fixed the other hyperparameters and varied the time window from 5 to 60 s: for the values in the interval 5-15 s, the measures were taken each second; for the interval 16-60 s, the measures were taken each 5 s. As the accuracy of the trained model is a random value, we performed 100 measurements for each time window size and calculated the mean value. Figures 19 and 20 present the dependencies of the mean accuracy and standard deviation on the window size. The graph in Figure 19 shows the highest accuracy achieved in 5-12 s. We hypothesize that the small time window sizes produced better accuracy because they contained fewer cycles of breathing and so lower the chance of a person performing different types of breathing in one data sample. Figure 20 shows the standard deviation of the model accuracy. While it grows from 5 to 12 s, that increase is far lower than the increase of the mean accuracy, so we decided that 10 s was the best time window to train machine learning models for determining the type of human breathing. Figure 18: The function of the distance between two sensors \(d^{\prime}_{ij}\) and the corresponding sine wave \(sinw_{ij}\) obtained by the nonlinear least squares method with time windows of 5 (a), 10 (b), 30 (c), and 50 (d) seconds. We trained the selected classifiers using a two-step algorithm. We used the following metrics to assess the quality of the trained models: accuracy, precision, recall, and F1-measure [46; 47; 48], training and running time, and the value of log_loss loss function.