# On-the-Fly Sleep Scoring Algorithm with Heart Rate, RR Intervals and Accelerometer as Input
## 2 Materials and Methods
### Motion-Aware On-the-Fly Sleep Scoring Algorithm  
Data acquired from the wearable device were passed to the Matlab algorithm capable of on-the-fly scoring (every second, the algorithm evaluates the sleep stage the subject is in based on the data acquired so far).  
The algorithm takes HR, RR intervals, and a 3-axis accelerometer as input. Assuming that at the beginning of the recording the subject is awake, it starts looking for the physiological HR fall to find the sleep onset [9]. Next, it examines the various stages by following the known sleep stage behavior.  
* LIGHT Sleep: NREM1 is a transitional phase between sleep and wakefulness, lasting a few minutes, in which the body begins to relax and from which it is easy to be awakened. Later, it transitions to NREM2, a phase more stable than NREM1 and with fewer movements that covers about 50% of total sleep. In the algorithm under consideration, NREM1 and NREM2 are combined to constitute the light sleep stage, a phase characterized by lower HR and Respiratory Rate than wakefulness.
* DEEP Sleep: NREM3 sleep, also known as deep sleep, is characterized by further lowering and regularization of HR and respiratory rate, movements are minimal. In adult individuals, it covers about 15-20% of total sleep and is mostly in the early part of sleep.
* REM Sleep: The last one is the Rapid Eye Movements (REM) phase, which owes its name to the irregular movements that the eyes make during this stage. It is characterized by brain activity similar to that during wakefulness, and consequently by higher cardiac activity than the other sleep phases. Total muscle atonia is present. It covers about 20-25% of total sleep and it is mostly in the final part of sleep [10; 25].  
The algorithm begins with the assumption that the user who starts it is awake. Even if the smartwatch requires more than 1 s to evaluate the different metrics, it starts evaluating them when worn, and their value is available when the application starts. Then, after each sample acquisition, if the subject is awake, the algorithm looks for an HR decrease and for the following HR stabilization inside the evaluation window. If it happens, the subject has fallen asleep. When the subject is sleepy, the algorithm behaves as follows:  
* IF a large enough portion of HR is greater than the threshold AND a large enough portion of RR intervals is smaller than the threshold AND there are enough movements, THEN the subject is AWAKE;
* ELSE IF the standard deviation of the smoothed HR inside the window is smaller than the threshold, THEN the subject is in DEEP sleep;
* ELSE IF a large enough portion of the SDNN is larger than the threshold AND the movements are minimized, THEN the subject is in REM sleep;
* ELSE the subject is in LIGHT sleep.  
All the recordings were processed by this algorithm and the scoring obtained is compared with the PSG scoring written by the medical doctor. The flowchart in Figure 1 shows how the algorithm works according to the rules seen above. The 10 parameters (winSize, hrTh, rrTh, accTh, stdTh, \(\times\)1, \(\times\)2, \(\times\)3, \(\times\)4, and \(\times\)5) were defined based on a set of polysomnography tests other than those presented in this paper. In particular, HR, RR intervals, and accelerations behavior were observed in the different sleep stages. hrTh, rrTh, and accTh, are based on those metrics behavior during wakefulness, stdTh is based on how the standard deviation of the HR processed through a moving average decreased during deep sleep. The parameters \(\times\)3 and \(\times\)5 reflect a minimum time interval necessary for deep and rem stage transition. The tests presented in this paper are used to validate the performance of the algorithm by running a double-blind experiment: the PSG scoring is performed by the medical doctor independently from the algorithm results, and the algorithm results are collected without knowing the outcome of PSG scoring.  
+++ ==ERROR: No output for this page==