I. Clinical Overview & Neurophysics of Sleep Architecture
The systemic study of human sleep dynamics relies upon high-resolution polysomnographic (PSG) protocols, combining multi-channel electroencephalography (EEG), electrooculography (EOG), and electromyography (EMG). The foundational metrics driving computerized sleep scoring models are electrical field potentials generated by the synchronized postsynaptic potentials of thousands of pyramidal neurons situated inside the cerebral cortex. The spatial orientation of these neurons, running perpendicular to the cortical surface, allows their synchronized dipoles to be registered as oscillatory voltages.
Sleep is structured into discrete, highly regulated cycles of Non-Rapid Eye Movement (NREM) and Rapid Eye Movement (REM) stages. As a healthy human subject moves from active wakefulness into NREM, the underlying neural circuits undergo a marked reduction in frequency accompanied by a steady increase in amplitude, representing the spatial synchronization of vast cortical pools under thalamocortical pacemakers. Wakefulness manifests as desynchronized high-frequency beta oscillations ($12-30 \text{ Hz}$) when engaged in sensory-cognitive processing, transitioning to a highly structured alpha rhythm ($8-12 \text{ Hz}$) during relaxed, eyes-closed baseline periods.
Upon transition to Stage N1 (the light, somnolent threshold of NREM sleep), alpha power drops and is replaced by low-amplitude, mixed-frequency theta activity ($4-8 \text{ Hz}$). Stage N2 (established light sleep) introduces highly specialized thalamocortical signatures: sleep spindles and K-complexes. Sleep spindles represent brief, high-frequency bursts ($11-16 \text{ Hz}$, typically centered at $14 \text{ Hz}$) initiated by recurrent inhibitory bursts of the thalamic reticular nucleus. K-complexes represent a single, high-voltage biphasic wave lasting over $0.5 \text{ seconds}$, representing a transient down-state of cortical silence. Stage N3 (Deep or Slow-Wave Sleep) shows deep synchronization dominated by slow delta oscillations ($0.5-4 \text{ Hz}$), with raw microvolt levels often exceeding $75 \ \mu\text{V}$, reflecting profound sensory disconnection and global synchronization. REM sleep reverses this trend, showing a highly active, desynchronized, low-amplitude profile matching the frequencies of wakefulness, and is often accompanied by distinct eye sweeps and muscle paralysis.
II. Operational Guide & Interactive Workspace Parameters
This browser-based interactive workspace enables granular control over modeled mathematical EEG signal behaviors, spectral energy densities, and real-time auditory synthesis. Users can manipulate parameters to examine how changes alter the visual and acoustic output of the workspace:
- Start Demo Mode: Initiates an automated cycle displaying typical stage progressions. In this mode, parameters like amplitude, noise, and stages are modulated automatically to showcase the transition behaviors. Any manual interaction immediately halts the demo and restores user presets.
- Reset Baseline: Instantly clears any running modifications, restoring default frequencies, amplitudes, noise floors, and speed settings back to default clinical baselines.
- Interactive Control Strip (Start / Pause / Skip): Allows running, freezing, or instantly skipping to the next chronological stage of the sleep cycle timeline.
- Sound ON/OFF: Activates synthesized binaural acoustic tones. The generator maps carrier frequencies to the left and right ears, phase-shifted by the exact dominant frequency of the currently active sleep stage. This translates brainwave frequencies into an audible spatial beat.
- Manual Stage Override: Forces the model to instantly transition directly to the selected target stage (Awake, Light, Deep, or REM), bypassing the automatic sequential progression.
- Transition Velocity: Accelerates the virtual sleep clock up to $5.0\text{x}$ or slows it down to $0.1\text{x}$ to allow detailed inspection of fast-changing transitions.
- Waveform Amplitude & Electrode Noise Floor: Controls the scaling of the raw voltage signals and injects white noise artifacts to simulate muscles, electrode movement, or RF ambient interference.
III. Technical Architecture & Mathematical Modeling
To run efficiently at native frame rates without lagging user inputs, the simulation engine renders procedural waveforms directly via optimized 2D canvas loops. The underlying mathematical model synthesizes multiple sub-frequency parameters based on clinical EEG bands. The raw voltage $V(t)$ generated in real-time is the sum of five individual sinusoids representing dominant spectral bands, supplemented by a pseudo-random Gaussian noise distribution $\epsilon(t)$:
$$V(t) = \sum_{i=1}^{5} A_i \sin(2 \pi f_i t + \phi_i) + \epsilon(t)$$
where $A_i$ represents the amplitude vector corresponding to delta, theta, alpha, sigma, and beta bands, and $f_i$ represents the baseline frequency components mapped for each sleep stage. Additionally, for the Light Sleep stage, sleep spindle bursts are procedurally triggered using an envelope function $E(t)$ governed by a localized Gaussian wave packet:
$$E(t) = A_{spindle} \cdot e^{-\beta (t - t_{spindle})^2} \sin(2 \pi f_{spindle} t)$$
Spectral energy density displays rely on columns populated with phase-locked energy vectors. The canvas dynamically shifts pixel columns horizontally using buffer copies, and appends the latest calculated data columns to minimize memory allocations.
The acoustic synthesizer utilizes the Web Audio API to create a binaural beat experience. Two oscillator channels generate distinct carrier waves directed through separate panning matrices to the left and right stereo channels. The left oscillator produces a static baseline tone:
$$F_{left} = 100 \text{ Hz}$$
while the right oscillator tracks the dominant frequency of the current sleep stage:
$$F_{right} = 100 \text{ Hz} + f_{dominant}$$
Listening with stereo headphones allows the auditory cortex to reconcile this phase discrepancy, producing a subjective acoustic beat matching the modeled brainwave frequency.
IV. Future Directions & Digital Neurotechnology Integration
This educational simulator serves as a foundation for broader integrations. Future updates aim to support raw physiological data ingestion in the standard European Data Format (.EDF), allowing direct comparison of simulated models against real human sleep data.
We also plan to implement real-time fast Fourier transform (FFT) modules to show live power spectral density (PSD) calculations, alongside 3D spatial mapping of scalp electrodes using standard 10-20 system configurations. These additions will help users visualize how delta and spindle waves propagate across frontal, central, and occipital regions, enhancing the workspace's utility for clinical neurophysiology and sleep science education.