Closed-loop EEG sleep-stage classification and haptic stimulation pipeline for the LUCID: Reality? research project.
PROJECT-07 is the first major technical component of LUCID: Reality?, a long-term neurotechnology research project exploring whether EEG-based sleep-stage detection can be combined with controlled haptic stimulation to support lucid-dream induction.
The repository currently contains the EEG preprocessing, sleep-stage classification, REM detection, decision/safety logic, haptic triggering pipeline, research documentation, experimental results, figures, and the roadmap toward EEG-to-dream-imagery reconstruction.
- Project Overview
- What Has Been Built
- System Architecture
- Current Results
- Dataset
- Repository Structure
- Installation
- Usage
- Core Components
- Research & Documentation
- Project Roadmap
- Current Status
- Limitations & Research Notes
- Future Work
- Citation
- License
- Author
The long-term goal of LUCID: Reality? is to investigate a closed-loop brain-computer interface in which neural activity during sleep can be detected, interpreted, and eventually used to interact with or reconstruct dream content.
PROJECT-07 focuses on the first part of that vision:
EEG Signal
│
▼
Sleep-EDF Preprocessing
│
▼
Sleep-Stage Classification
│
▼
REM Detection
│
▼
Safety / Decision Logic
│
▼
Haptic Trigger
│
▼
Android Device
The current implementation establishes the foundation for moving from offline EEG classification toward a future real-time closed-loop system.
The pipeline processes Sleep-EDF EEG recordings using MNE-Python.
Current preprocessing includes:
- EDF signal loading
- EEG channel selection
- Band-pass filtering
- 30-second sleep epochs
- Sleep-stage label alignment
- Dataset preparation for deep-learning models
- Dataset/class-distribution analysis
The current documented pipeline uses the Fpz-Cz EEG channel and a 0.3–35 Hz band-pass filter.
A deep-learning sleep classifier has been implemented using a CNN-LSTM / stacked LSTM approach.
The classifier is designed to distinguish:
- Wake
- N1
- N2
- N3
- REM
The repository contains the model, training pipeline, evaluation artifacts, logs, plots, and research results.
REM detection is treated as the critical event for the closed-loop stimulation stage.
The pipeline includes:
- REM probability estimation
- Configurable probability thresholding
- Temporal smoothing
- Multi-window confirmation
- Logic designed to reduce false triggers
The current classifier documentation reports 84% REM recall, meaning the evaluated system identified 84 out of 100 REM epochs in that reported evaluation.
The stimulation stage is not intended to trigger simply because one epoch is classified as REM.
The decision layer incorporates conditions such as:
- sustained sleep-state evidence
- recent Wake-state checks
- anti-retrigger logic
- REM probability thresholding
- temporal smoothing
This creates an additional control layer between the neural classifier and physical stimulation.
A working haptic-trigger component connects the classification pipeline to an Android phone.
The current implementation uses the JOIN API to send a vibration/haptic command to the connected device.
This establishes the basic closed-loop concept:
EEG → Classifier → Decision Logic → Trigger → Phone Vibration
The repository also contains research-oriented material including:
- experimental results
- training/evaluation logs
- generated plots
- paper figures
- research report
- research roadmap
- Phase 3 planning document
- licensing and reproducibility documentation
┌──────────────────────┐
│ Sleep-EDF / EEG │
│ .edf data │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ EEG Preprocessing │
│ • MNE-Python │
│ • 0.3–35 Hz filter │
│ • 30 s epochs │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Sleep Classifier │
│ CNN / Stacked LSTM │
│ • Wake │
│ • N1 │
│ • N2 │
│ • N3 │
│ • REM │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ REM Detection │
│ • Probability │
│ • Thresholding │
│ • Temporal smoothing │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Decision / Safety │
│ • State confirmation│
│ • Wake check │
│ • Anti-retrigger │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Haptic Trigger │
│ JOIN → Android │
└──────────────────────┘
The repository currently documents the following evaluation for the Sleep-EDF Cassette pipeline:
| Sleep Stage | Precision | Recall | F1-Score |
|---|---|---|---|
| Wake | 0.69 | 0.54 | 0.61 |
| N1 | 0.32 | 0.38 | 0.35 |
| N2 | 0.89 | 0.78 | 0.83 |
| N3 | 0.84 | 0.88 | 0.86 |
| REM | 0.67 | 0.84 | 0.74 |
Reported overall accuracy: 76.85%
Reported REM recall: 84%
The broader project roadmap also records a separate LOSO evaluation of the CNN-LSTM work with 80.14% accuracy and κ = 0.67. These figures should be interpreted as results from different evaluation configurations rather than as one combined metric.
The primary dataset used by the classifier pipeline is Sleep-EDF.
| Property | Value |
|---|---|
| Dataset | Sleep-EDF Cassette |
| Source | PhysioNet |
| Subjects | 20 in the documented classifier run |
| Epoch length | 30 seconds |
| Sampling rate | 100 Hz |
| EEG channel | Fpz-Cz |
| Filter | 0.3–35 Hz |
| Classes | Wake, N1, N2, N3, REM |
| Documented epochs | 18,226 |
The broader roadmap also describes a larger 153-subject Sleep-EDF preprocessing pipeline used in the research program. The exact subject count therefore depends on the experiment/configuration being referenced.
Important: Dataset files are not expected to be committed to this repository. Obtain the appropriate Sleep-EDF data from its official source and follow its licensing/data-use requirements.
PROJECT-07/
│
├── classifier_main_pipeline/
│ ├── logs/
│ ├── models/
│ ├── paper_figures/
│ ├── plots/
│ │
│ ├── src/
│ │ ├── preprocess_sleep_edf.py
│ │ ├── lstm_model.py
│ │ ├── train_rem_model.py
│ │ ├── detect_rem.py
│ │ ├── decision_logic.py
│ │ └── main.py
│ │
│ ├── class_distribution.py
│ ├── trigger_vibrations.py
│ ├── RESULTS.md
│ ├── RESEARCH_REPORT.txt
│ ├── requirements.txt
│ └── README.md
│
├── Research Papers/
│ └── Phase 2/
│
├── PHASE3_PLAN.md
├── ROADMAP.md
├── LICENSE
└── README.md
The structure above reflects the repository's current organization and the main documented pipeline components. Individual experimental files and generated artifacts may evolve as development continues.
git clone https://github.com/anu-rag-007/PROJECT-07.git
cd PROJECT-07/classifier_main_pipelineWindows:
python -m venv .venv
.venv\Scripts\activateLinux/macOS:
python3 -m venv .venv
source .venv/bin/activatepip install -r requirements.txtpython src/preprocess_sleep_edf.pyThis prepares the raw Sleep-EDF recordings for model training/evaluation.
python src/train_rem_model.pyThe training pipeline uses the configured deep-learning architecture and training parameters.
python src/main.pyThe full pipeline connects preprocessing/model inference with REM detection, decision logic, and the haptic trigger stage.
Hardware note: The haptic component requires a configured Android device and the corresponding JOIN integration. Offline model inference can be developed/tested independently.
Responsible for:
- loading EDF recordings
- filtering EEG
- segmenting signals into epochs
- preparing sleep-stage labels
- generating model-ready data
Contains the neural-network architecture used for sleep-stage classification.
The documented architecture includes a 2-layer stacked LSTM, with:
- hidden dimension: 128
- dropout: 0.3
Handles model training, including:
- weighted loss
- Adam optimization
- learning-rate scheduling
- model checkpointing/evaluation
Converts model output into REM detection decisions using:
- REM probability
- configurable thresholding
- temporal smoothing
The documented implementation uses a probability threshold above 0.6 and five-window smoothing.
Provides the safety/decision layer between classification and stimulation.
Its documented logic includes:
- sustained-state checks
- recent Wake-state protection
- anti-retrigger behavior
Connects the software pipeline to an Android device through JOIN and issues the haptic/vibration command.
The repository is structured not only as a software prototype but also as a research workspace.
RESULTS.md— experimental/evaluation resultsRESEARCH_REPORT.txt— research notes/reportpaper_figures/— figures prepared for research communicationResearch Papers/Phase 2/— Phase 2 research materialROADMAP.md— long-term project roadmapPHASE3_PLAN.md— Phase 3 development plan
The project roadmap currently describes a progression from EEG sleep staging toward EEG-to-dream-imagery reconstruction using THINGS-EEG, EEG embeddings, image-generation systems, and eventually 3D dream-environment reconstruction.
Status: Substantially implemented
- Sleep-EDF preprocessing
- Sleep-stage classification
- REM detection
- Evaluation and metrics
- Haptic trigger
- Closed-loop prototype foundation
- Research publication/archival work
Planned/ongoing work includes:
- THINGS-EEG integration
- Short-window EEG representation learning
- EEG alignment/embedding model
- EEG-conditioned image generation
- ComfyUI integration
- IP-Adapter-based conditioning
- Dream-imagery reconstruction experiments
Planned research direction:
EEG
│
▼
Dream Image
│
▼
Multi-view Reconstruction
│
▼
3D Scene / NeRF
│
▼
Navigable Dream Environment
The current Phase 3 plan explores technologies such as Zero-1-to-3 and Instant-NGP as potential components of this future stage.
Phase 1
EEG Sleep Staging
↓
Phase 2
EEG → Dream Imagery
↓
Phase 3
Dream Imagery → 3D Environment
↓
Phase 4
Multi-user Shared Dream World
↓
Phase 5
Artificial Reality
Repository: PROJECT-07
Project: LUCID: Reality?
Development stage: Research prototype / v0.1 foundation
- EEG preprocessing pipeline
- Sleep-stage classification
- REM detection
- Decision/safety logic
- Haptic trigger integration
- Offline end-to-end pipeline
- Evaluation and research artifacts
- Research roadmap
Real-time EEG stream integration.
The long-term implementation is intended to replace offline EEG recordings with a live EEG source and evaluate the classifier under real-time sleep conditions.
This repository represents an experimental research prototype, not a clinically validated medical or sleep-monitoring device.
Important limitations include:
-
Offline-first evaluation
Much of the current pipeline is evaluated using recorded EEG rather than continuous live EEG. -
Generalization
Sleep-stage performance can vary substantially across subjects and recording environments. -
N1 classification difficulty
The reported evaluation shows considerably lower N1 performance than N2/N3. -
REM detection is not equivalent to lucid dreaming
Detecting REM does not guarantee that a subject is lucid. -
Haptic stimulation requires further validation
The presence of a working phone trigger demonstrates the technical pathway, not the efficacy or safety of lucid-dream induction. -
Human experimentation requires appropriate oversight
Future studies involving human subjects, sleep manipulation, or neurotechnology should follow applicable ethical, institutional, and regulatory requirements. -
Dream reconstruction remains an open research problem
The Phase 2/3 roadmap describes research goals rather than capabilities already achieved by the current repository.
The immediate technical priorities are:
- Real-time EEG streaming
- Robust subject-independent evaluation
- Improved N1/REM classification
- Calibration and threshold optimization
- Real-time inference latency measurement
- More robust safety interlocks
- Wearable EEG integration
- Controlled experimental validation
- THINGS-EEG representation learning
- EEG-to-image reconstruction
- 3D dream-environment reconstruction
The ultimate research question is whether measurable neural activity during sleep can provide enough information to reconstruct meaningful aspects of dream imagery.
If you use this work, please cite:
@misc{sharma2026lucid,
author = {Sharma, Anurag},
title = {Automated Sleep Stage Classification
for Closed-Loop Lucid Dream Induction
via CNN-LSTM on Single-Channel EEG},
year = {2026},
publisher = {Zenodo},
doi = {10.5281/zenodo.21885881},
url = {https://doi.org/10.5281/zenodo.21885881}
}This project is released under the MIT License.
See LICENSE for the complete license text.
Anurag Sharma
B.Tech CSE (AI & ML)
PROJECT-07 is part of the broader LUCID: Reality? research initiative.
The project was started as an early-stage exploration of EEG, artificial intelligence, brain-computer interfaces, sleep-stage classification, and the possibility of interacting with dream experiences through closed-loop systems.
This project builds upon publicly available EEG/sleep research resources and open-source scientific software, including:
- Sleep-EDF / PhysioNet
- MNE-Python
- PyTorch
- NumPy
- scikit-learn
- Android/automation tooling used for haptic experimentation
LUCID: Reality? and PROJECT-07 are research and educational projects.
Nothing in this repository should be interpreted as medical advice, a clinically validated diagnostic system, or a proven method for inducing lucid dreams.
The current implementation demonstrates a technical research pipeline and should not be used for unsupervised human experimentation.