# On-the-Fly Sleep Scoring Algorithm with Heart Rate, RR Intervals and Accelerometer as Input
###### Abstract
The ANS is divided into two main components: the sympathetic nervous system (which stimulates \"fight or flight\" responses) and the parasympathetic nervous system (which promotes rest and recovery). During sleep, the ANS plays a key role in regulating the different stages of sleep, especially in the transition between REM and non-REM sleep. HRV is a measure of the variation in time intervals between successive heartbeats (RR intervals), and it is closely related to the activity of the ANS [15]. Because of this relationship, wearable devices are able to perform sleep scoring quite accurately, although less accurate than a PSG, but cheaper and less invasive. In addition to RR intervals, there are several HRV metrics, such as SDNN (Standard Deviation of NN intervals) and RMSSD (Root Mean Square of Successive Differences), which are used in conjunction with HR and accelerometer to achieve a higher accuracy. An important topic in the study of sleep is on-the-fly sleep scoring. An on-line (or on-the-fly) algorithm generates output in response to each input portion without knowing the future input [16]. An off-Line algorithm, instead, provides the output after having received the whole input sequence. In addition to being able to estimate how the previous night went, in some cases, it would be useful to be able to know at which stage the subject is at a specific moment. For example, sometimes it's useful to avoid a subject falling into REM or deep sleep and to act instantaneously if it happens. Different studies have been conducted where the algorithms are able to identify the sleep stage during the night. [PERSON] et al. [17] developed an automated threshold calculation algorithm capable of identifying NREM, REM, and wake in mice. They used EEG and EMG and achieved an overall accuracy of 90%. [PERSON] et al. [18] developed a time-distributed Convolutional Neural Network able to perform real-time sleep scoring. Their algorithm is able to identify five stages: N1, N2, N3, REM, and wake. They used a single-channel EEG obtained by a modified version of the Muse headband [19] and achieved an overall accuracy of 83.5%. [PERSON] et al. [20] developed another real-time algorithm able to detect sleep-wake patterns with a single-channel EEG with a resolution of 30s. They applied it to critically ill patients achieving a media sensitivity of 97.9% and a median specificity of 49.7% on the sleep-wake pattern identification. [PERSON] et al. [21] implemented a real-time Automated Sleep Scoring device with one probe that senses blood oxygen, PPG, and actigraphy. It was able to perform four stages of sleep scoring with an accuracy of 75.1%. Most of them require EEG, which is an invasive technology, or require ad-hoc devices. The idea behind this work is to implement an on-the-fly scoring algorithm that is able to work with signals available from a consumer smartwatch, in particular, HR, RR intervals, and 3-axis acceleration at 1 Hz. The research significance of our concept is not just a chance to perform sleep scoring with HR, RR intervals, and accelerations, but the possibility of using these parameters in an on-the-fly sleep scoring algorithm. The obtainment of these data from cheap, low-invasive, and widespread wearable devices, can lead to interesting applications. A useful case for the on-the-fly sleep scoring algorithm could be while driving. Despite the spread of autonomous driving, available systems are not completely autonomous. Cars with partial and conditional automation (L2-3) require human attention. The driver must be able to take back control of the vehicle when needed through the handover procedure [22]. In an automated context, drivers tend to be distracted and could become drowsy more easily, and drowsy driving is a public health problem that causes 17.6% of road accidents, according to an AAA Foundation study of the National Highway Traffic Safety Administration [23]. In particular, a deeper sleep state leads to a higher sleep inertia, preventing a timely control regain [24].