# Fog-Computing-Based Heartbeat Detection and Arrhythmia Classification Using Machine Learning
## 3 System Design
After differentiation, the signal is squared point by point, based on the formula: \(y(nT)=[x(nT)]^{2}\). This transformation makes all data points non-negative and performs a nonlinear amplification of the output of the derivative, emphasizing  
Figure 2: A Fog-computing based organization of the system components and their interaction utilizing different networking technologies. the higher frequencies, in our case the ECG frequencies and the QRS complex in particular. The result of the preprocessing stage for each step are depicted in Figure 3.
2. Heartbeat-detection module--The second stage of the arrhythmia-detection system consists in extracting the heartbeats from the ECG samples produced by the previous module. The ECG samples retrieved are segmented in contiguous regions of fixed duration (see Section 4 for more details on the length of the regions). Given this transformation, each segment is examined to identify if it contains a \(QRS\) complex. Given a \(QRS\) complex, the \(R\) peak location is then detected from such regions using the formula: \(R\_peak=argmax(abs(region))\). In words, the location of the \(R\) peak is the one corresponding to the maximum absolute value in the region detected as a \(QRS\) complex. This is because, given that a time segment has been detected as a \(QRS\) wave, it is very likely that, in such a small portion of the signal, the \(R\) peak is located at the point of maximum amplitude. The output of the module is a sequence of locations of \(R\) peaks. The identification of the \(QRS\) complexes is implemented based on the KNN algorithm, as it can be executed at hardware level in one of the recently introduced asymmetric multicore embedded processors that provide a dedicated core for hardware assisted pattern matching (e.g., the NXP MPC8572E module [24] and the Intel(r) Curie(tm) module [23]). The KNN implementation supported by the hardware is based on a simple majority voting to identify the label that will be associated to a given value. Note that the use of the KNN algorithm in this module is different from the one used by [PERSON] et al. [33], as the latter does not use a window but examines each ECG sample individually. It is also different from the one used by [PERSON] et al. [49], in which the KNN algorithm is used to characterize whether the \(QRS\) complex is normal or abnormal. The design aspects and the technical details of the heartbeat detection module are presented in more details in Section 4. 3. Arrhythmia classification module--The third module conducts the final stage of arrhythmia classification. The module examines the \(R\) peaks detected by the previous module, identifies the signal around an \(R\) peak location, and extracts the corresponding region from the raw signal. In this way, the sequence of \(R\) peaks is transformed into a tridimensional stream of the form: \(sequence=(b,\ t,\ s)\) where \(b\) is the total number of detected beats, \(d\) is the number of previous beats to consider so as to make a prediction, and \(s\) is the raw signal that makes up the specific beat. Essentially, we generate a _sliding window_ over the sequence of beats, of length equal to \(t\) beats. Given that the ECG data are sequential and that determining whether a heartbeat is arrhythmic or not depends not only on its morphology but also on those of the previous beats, we designed a particular kind of recurrent neural network (RNN) for the arrhythmia classification purpose, based on long-short term memory (LSTM) units [50]. LSTM-based architectures are very powerful for applications on time-series data, we therefore use them for our problem. The transformed sequence is passed to the neural-network classifier, which has been designed and trained in order to identify normal and arrhythmic beats. The detected arrhythmic beats are then classified in more fine-grained classes, defined by a well-known standard for cardiac algorithm evaluation [25]. The methodology for training and validating the RNN is presented in more details in Section 4.