# On-the-Fly Sleep Scoring Algorithm with Heart Rate, RR Intervals and Accelerometer as Input
## 6 Discussion  
PSG is the gold standard for the study of sleep structure and pathology. It consists of monitoring a patient for one night by collecting data from different kinds of sensors, such as EEG and EOG. The next day, the collected data are read by a physician. This test allows the highest reliability in sleep detection, but it is invasive and expensive [26]. To fix these problems, there has been a growing interest in wearable devices such as smartwatches and smart rings, whose sensors have improved greatly in recent years and which enable remarkable results in sleep estimation. The problem addressed in this work is to create an algorithm for on-the-fly scoring, but one that can be used with wearable devices. Then, data were collected from wearable devices, and a Matlab algorithm was written to take input from the collected data and calculate the scoring. PSG was used as ground truth, as it is the most accurate technology for sleep scoring. A comparison of the results was made, and the following was determined. The most difficult phase to identify was light sleep, which is a transitional phase, and has characteristics in common with wakefulness and REM stage, and lends itself to being confused with them. The easiest to identify were wakefulness and deep sleep. Wakefulness has HR and acceleration larger than every sleep stage, so it is usually more recognizable than the other stages, except for motionless wakefulness. Having both the HR and accelerometer can help in distinguishing between motionless wake and sleepiness. Deep sleep is also recognizable thanks to the smaller HR variation that characterizes it. The greater reliability in wakefulness recognition allowed the sleep-wake pattern values, namely TST, WASO, Sleep Onset time, and awakening time, to be more accurate. In particular, the Sleep Onset was shown to be the best identified parameter by looking for the physiological fall of HR. In the following works, the authors compared the sleep scoring performed by different wearables with the one obtained by PSG. [PERSON] et al. [27] compared five commercial sleep-tracking devices with actigraphy and PSG. [PERSON] et al. [28] made a comparison between 11 commercial sleep devices, including wearable, wearable, and airable devices. Across all the devices, they found a bias toward the light sleep stage. In particular, speaking about wearables, they found that wakefulness is usually misclassified as light. [PERSON] et al. [29] compared the performances of seven consumer sleep-tracking devices with the PSG, obtaining an overestimation of light sleep with respect to the medical device. [PERSON] et al. [30] evaluated the sleep tracking ability of a specific smartwatch, the Samsung Galaxy Watch 3. They compared it to the PSG, obtaining weaker results on REM sleep. All of them agree on the overestimation on the Total Sleep Time (TST) at the expense of the Wakefulness After Sleep Onset (WASO). This is due to the difficulties in finding motionless wake, which sometimes causes our algorithm to misclassify it as light sleep. The main limitations on the results, with respect to the PSG, are the absence of EEG, which is critical for sure sleep stage identification, and the fact that the algorithm is on-the-fly, so \begin{table}
\begin{tabular}{c c c c} \hline
**Sleep Onset Advance** & **Wake Up Advance** & **TST Difference** & **WASO Difference** \\ \hline \(-11\) m 39 s & \(-14\) m 03 s & 07 m 00 s & 04 m 15 s \\ \hline \end{tabular}
\end{table} 
Table 10: How sleep–wake parameters identified by the algorithm are different from the ones found by the medical doctor on the online scoring. evaluations cannot be conducted on the whole signals, but are performed while signals are collected. An interesting application for the on-the-fly sleep scoring algorithm could be the identification of a deep sleep state while driving to allow the driver to recover. The problem with the proposed algorithm is the dependency on the 3-axis accelerometer, which could be strongly affected at the wheel, especially on rough terrains.