# Fog-Computing-Based Heartbeat Detection and Arrhythmia Classification Using Machine Learning
## 4 Implementation Details
### Heartbeat-Detection Module  
The KNN classifier used by the _heartbeat-detection module_ is based on a \([m\times n]\) feature matrix, comprised of \(m\) segments of ECG signals retrieved from \(n\) sensor leads (i.e., channels). Each segment \(s\) used to train the classifier is labeled based on the annotations provided by the physicians:  
\[\textit{label}(s)= \begin{cases}1&\text{if $s$ contains a $QRS$ annotation}\\ -1&\text{otherwise.}\end{cases} \]  
Because of to the unique characteristics of each individual person, the ECG signals produced by each heart are unique. For this reason, the training step is performed with an _intra-patient_ perspective: a separated classification instance is used for each patient. Each KNN classifier is trained using an ECG segment of \(t\) seconds and a five-fold cross-validation is employed to avoid overfitting on the training set. In the five-fold cross-validation, the original training set is randomly partitioned into five equally sized subsamples. Of the five subsamples, a single subsample is retained as the validation data for testing the model, and the remaining four subsamples are used as training data. The cross-validation process is then repeated five times, with each of the five subsamples being used exactly once as the validation data. The five results are then averaged to produce a single estimation. To fine-tune the performance of the module, we evaluated the performance using standard statistical indices to determine the precision and recall derived from three parameters: correctly detected peaks (_correctly detected_), total detected peaks (_detected_), and actual number of peaks (_real_peaks_). \[\textit{Precision}=\frac{|\textit{correctly detected}|}{|\textit{detected}|}\]  
\[\textit{Recall}=\frac{|\textit{correctly detected}|}{|\textit{real\_peaks}|}\]  
\[\textit{F1 Score}=\frac{2\cdot\textit{Precision}\cdot\textit{Recall}}{ \textit{Precision}+\textit{Recall}}\]  
Note that the annotations of the \(R\) peak locations in the database were not placed precisely on the maximum of the \(QRS\) complex by the annotators. More precisely, in some cases, the \(R\) peak is not properly annotated, although it is guaranteed to be inside the \(QRS\) complex. For this reason, we consider a peak detection correct if it resides inside a range of 0.1 s around the actual peak annotation. We chose such an amount of sample such as to be comparable to the average \(QRS\) width (for details, refer to the book of [PERSON] [53], Chapter 2, Section 41). Clearly, the duration \(t\) of the training set is an important parameter for the overall performance of the module. Keeping \(t\) at minimum levels is essential, as a small training set is preferable for faster computation. For this reason, we conducted a series of experiments to identify the minimum amount of signal necessary for the training step without compromising the accuracy of the results. Figure 4a depicts the precision, the recall, and the F1 score achieved for different training-set lengths. Given the outcome of the experiment, a training set of \(t=50\) s (corresponding to 3% of the ECG signal) suffices to produce very high accuracy--higher than 90%. On average, this corresponds to about 56 beats. Given that the ECG signal is split into contiguous and fixed size regions, another important parameter, whose value we need to choose appropriately, is the window size. Thus, we conducted a second set of experiments to identify the precision and recall obtained for different window sizes. Figure 4b depicts the results of this experiment. Given the outcome of the fine-tuning, a window size of 50 samples maximizes the F1 Score. To better understand the cases where the heartbeat-detection module fails to properly detect an \(R\) peak, we identified a set of examples from the dataset for which the module fails.