# Integrating Mobile Devices and Wearable Technology for Optimal Sleep Conditions
## 2 Materials and Methods
### Sleep State Analysis Algorithm  
Sleep can be categorized into two main phases: rapid eye movement (REM) and non-rapid eye movement (NREM) [43]. Typically, a nocturnal sleep cycle encompasses 4 to 5 distinct stages, beginning with the wakeful state and progressing through the rapid eye movement phase, followed by the non-rapid eye movement phase. Subsequently, the cycle reenters the rapid eye movement stage, transitioning into the unconscious phase before once again entering the non-rapid eye movement phase. This cyclical pattern eventually culminates in returningto the wakeful state. The non-rapid eye movement period can be further partitioned into four stages: N1, characterized by shallow sleep; N2, representing deep sleep; and N3 and N4, signifying extended periods of deep slumber [44; 45]. An 8 h nightly sleep usually includes 4-5 sleep cycles, a sleep cycle generally lasts 90 to 120 min [45], and a good sleep cycle usually occupies 15-20% of the total sleep during deep sleep. During deep sleep, the body is completely relaxed and rested and the brain begins to reorganize its memory, incorporating pre-sleep information into deep memory and other important tasks. Figure 1 shows a typical diagram of the nocturnal sleep cycle [46].  
Distinguishing various sleep states relies on EMG analysis during REM and non-REM (NREM) sleep, as well as the collection of other physiological signals such as brainwaves, heart rate fluctuations, and the frequency and intensity of body movements. Physiological data are collected through brainwave analysis to differentiate between sleep states and assess sleep quality. Conductive adhesive electrodes are placed externally on the cerebral cortex, allowing for the recording and analysis of the user's brainwave energy distribution before, during, and after sleep using a physiological signal device. This analysis facilitates the evaluation of sleep state and quality. Electrocardiogram (ECG) measurements capture heart potential signals, enabling the recording and quantitative analysis of physiological parameters such as heartbeat intensity and frequency. This data acquisition provides insight into the subject's relaxed and excited sleep states.  
The categorization of sleep stages is rooted in brainwave analysis, which involves examining the transformation of potential brain energy from time to frequency through Fourier transform. When transitioning from wakefulness to sleep, changes in brainwave frequencies occur. During the awake state, alpha (8-13 Hz) and beta (13-30 Hz) brainwaves are more active. The rapid eye movement (REM) phase exhibits significant low-frequency brainwave signals (2-7 Hz) and heightened EMG activity. In non-REM (NREM) sleep, deep sleep phases (N3 and N4) are characterized by the presence of delta (0.5-4 Hz) and theta (4-7 Hz) waves, which constitute a substantial portion of the overall energy distribution. Notably, deep sleep stage N4 is dominated by delta waves [47].  
Analyzing heartbeats during sleep involves the recording of each heartbeat cycle using physiological signal devices. These cycles begin with the electrical impulse of the sinoatrial node, leading to atrial and ventricular contractions. Electrocardiogram (ECG) results are subjected to three methods of analysis: time, frequency, and nonlinearity. Time domain analysis includes metrics such as the mean, standard deviation, and percentiles of the R-wave interval (refer to Figure 2). Frequency domain analysis involves Fourier or wavelet transform to distinguish energy distribution between sympathetic and parasympathetic nervous activities at both low (0.04-0.15 Hz) and high (0.15-0.4 Hz) frequencies. The  
Figure 1: Normal night sleep cycle [46].  
nonlinear analysis encompasses graphical and parameter-based methods, with graphical analysis presenting ECG scatter plots that reveal trends in heart rate variability (HRV) [48].