EEG / BCIPythonCSP + LDASignal Processing

BCI Decoder

Offline CSP + LDA decoder for a motor-imagery cursor BCI

An offline EEG decoding pipeline — Common Spatial Patterns + LDA — that learns user-specific spatial filters to move a cursor by imagining left- vs right-hand movement. It matched a hand-tuned live decoder at ~91%, and showed that fewer electrodes beat more.

~91% accuracy

10-electrode montage

matched the live decoder

Stack

Python · scikit-learn · SciPy · NumPy · Matplotlib

BCI Decoder

the problem

A live BCI decoder is calibrated by hand during the session — fast, but blind to each person's brain. Could an automated, data-driven pipeline match a decoder a human tuned in real time?

how it works

A person imagines moving their left or right hand to steer a cursor; the two produce mirror-image drops in EEG power over opposite hemispheres. The pipeline applies a common-average reference and an 8–15 Hz bandpass, then Common Spatial Patterns learns spatial filters tuned to *that* person's brain. The log-variance of the filtered signal feeds an LDA classifier, validated with 5-fold cross-validation.

The offline decoding pipeline — from imagined movement to cursor control.
The offline decoding pipeline — from imagined movement to cursor control.

fewer electrodes, better decoder

The hypothesis: strict feature selection beats a broad montage. Trimming from 17 electrodes down to 10 motor-cortex channels consistently *improved* accuracy — the extra channels were feeding eye-blink and muscle artifacts into CSP, which maximizes variance blindly. More data isn't always better.

Left-Right decoding accuracy: the 10-electrode motor montage edged out the broader 17-electrode set on both sessions.
Left-Right decoding accuracy: the 10-electrode motor montage edged out the broader 17-electrode set on both sessions.

what CSP could and couldn't do

On the lateralized left-right task, the automated decoder matched the hand-tuned live baseline (within cross-validation error) — parity from a fully data-driven pipeline. On the up-down task (both hands vs. rest) it dropped: both classes light up *both* hemispheres, so there's no spatial contrast for CSP to exploit. The failure mapped cleanly onto the math.

Offline CSP + LDA vs. the live, hand-tuned baseline — parity on lateralized tasks, a drop on bilateral ones.
Offline CSP + LDA vs. the live, hand-tuned baseline — parity on lateralized tasks, a drop on bilateral ones.

key decisions

CSP + LDA over deep learning

With only ~135 valid trials, a CNN would overfit. CSP learns robust spatial filters from a tiny dataset, and LDA's assumptions fit the log-variance features — data-efficient and interpretable, not a black box.

Cut electrodes, don't add them

Peripheral channels added high-dimensional noise to CSP's covariance estimates. Restricting to the motor cortex improved generalization — a feature-selection win, not a model win.

outcomes

  • Matched a hand-tuned live decoder on the left-right task (~91%, within CV error)
  • Feature selection (17 → 10 electrodes) improved accuracy on both sessions
  • Diagnosed why CSP fails on bilateral tasks — spatially symmetric classes
  • Validated with 5-fold cross-validation (±5% std)
  • Open-sourced on GitHub

up next

GreenLlama

Compressed, FPGA-accelerated LLM with a fairness gate