MRM MRM MRM MRM
SYSTEM BOOTING
Back to projects
AI / Machine Learning

EEG Sleep Stage Classifier (SVM)

An end-to-end MATLAB pipeline classifying real clinical EEG epochs into five sleep stages at 92.6% accuracy.

Difficulty — expert Complexity — 9/10 Category — AI / Machine Learning

Overview

A complete biomedical machine-learning pipeline that takes raw overnight EEG recordings from PhysioNet's Sleep-EDF database and automatically scores each 30-second epoch into Wake, N1, N2, N3 or REM sleep, following AASM scoring conventions. The project's hardest problem wasn't the classifier — it was getting clean, correctly-labeled training data out of a clinical file format MATLAB doesn't natively support.

Architecture

A staged pipeline: custom EDF+ annotation decoding → Butterworth bandpass/notch filtering → Z-score normalization → 30-second epoch segmentation → 17-dimensional time/frequency feature extraction → One-vs-All ECOC multi-class SVM → real-time inference and hypnogram generation.

Key Features

  • A custom binary parser decoding EDF+ Time-Stamped Annotation Lists, since MATLAB's native edfread() can't handle annotation-only hypnogram files.
  • 17-dimensional feature vectors combining 7 time-domain statistics with Welch PSD band power across delta through gamma.
  • A One-vs-All ECOC SVM trained on 6,662 epochs and tested on 1,665, preserving class distribution via stratified split.
  • A saved, deployable model enabling real-time diagnosis of new recordings without retraining.

Development Process

  • Reverse-engineered the EDF+ annotation format to build a working TAL parser, since off-the-shelf tools failed on this dataset.
  • Applied identical preprocessing at training and inference time to prevent train/test skew.
  • Extracted and validated the 17-feature representation before committing to classifier training.
  • Trained the ECOC SVM and evaluated per-class accuracy to identify systematically harder stages.

Challenges & Solutions

Challenge

MATLAB's built-in EDF reader cannot decode annotation-only EDF+ files, which blocked label extraction entirely.

Solution

Wrote a custom binary parser for the EDF+ TAL format from the specification, synchronizing decoded labels back to the raw EEG signal.

Challenge

REM sleep shares spectral characteristics with neighboring stages, making it the hardest class to separate.

Solution

Accepted the accuracy trade-off transparently — 72.8% on REM vs. 98.9% on Wake — and reported per-class results rather than a single blended number.

Results & Impact

The trained SVM reached 92.61% overall accuracy on held-out test epochs, with per-class performance ranging from 98.9% (Wake) to 72.8% (REM), reflecting genuine physiological overlap rather than a modeling flaw.

Future Improvements

  • Explore deep learning (CNN/LSTM) architectures for the harder REM/N1 boundary.
  • Expand training data beyond three overnight recordings.
  • Package as a lightweight desktop diagnostic tool.