CSP and FBCSP for Motor-Imagery EEG

8 minute read

Published:

Motor-imagery EEG is not read channel by channel.

A left-hand trial and a right-hand trial may differ in a pattern spread across several electrodes. One channel may be weak. Another may be noisy. The useful signal is often a contrast across channels.

CSP starts there.

It asks a simple question:

Can I build a new channel that has high variance for one class and low variance for the other?

The Trial

Start with one filtered EEG trial:

\[X \in \mathbb{R}^{C \times T}.\]

\(C\) is channels. \(T\) is time.

For motor imagery, the classes may be left hand and right hand. CSP does not look for a waveform. It looks for variance differences.

That is reasonable for motor imagery because event-related desynchronization and synchronization change rhythm power.

The Spatial Filter

CSP learns a matrix:

\[W \in \mathbb{R}^{C \times K}.\]

Each column of \(W\) is a spatial filter. Applying the filter gives new signals:

\[Z = W^\top X.\]

The rows of \(Z\) are not original electrodes. They are weighted mixtures of electrodes.

One filter may emphasize activity over motor cortex. Another may subtract activity from nearby channels. The point is not to make a pretty channel. The point is to make a discriminative channel.

What CSP Optimizes

CSP uses class covariance matrices.

Call them \(\Sigma_1\) and \(\Sigma_2\).

For a filter \(w\), the projected variance in class 1 is:

\[w^\top \Sigma_1 w.\]

The projected variance in class 2 is:

\[w^\top \Sigma_2 w.\]

CSP looks for filters with a large ratio:

\[\frac{w^\top \Sigma_1 w}{w^\top \Sigma_2 w}.\]

Filters from one end of the solution favor class 1. Filters from the other end favor class 2.

That is why CSP features usually come in pairs. Keep filters from both ends, instead of keeping only the largest ones.

The two ends are easier to see if CSP is solved in two familiar steps. First, whiten the composite covariance \(\Sigma_1 + \Sigma_2\). After that, the two class covariances add up to \(I\), so along any unit direction the class variances are \(d\) and \(1 - d\). Then rotate to the eigenvectors \(V\) of the whitened class-1 covariance \((\Sigma_1 + \Sigma_2)^{-1/2}\Sigma_1(\Sigma_1 + \Sigma_2)^{-1/2}\). The filters are

\[W^\top = V^\top (\Sigma_1 + \Sigma_2)^{-1/2},\]

and the variance ratio along each new axis is \(d / (1 - d)\): large at one end, small at the other. In eegwhiten this is whiten_relative(Sigma1, reference = Sigma1 + Sigma2).

Cov of class 1
1.000.000.001.00
Cov of class 2
1.000.000.001.00

Dots are band-passed time samples from two classes (teal and amber). Both have zero mean, so only their covariances differ. Whitening Σ₁ + Σ₂ turns the composite 2σ ellipse (dashed) into the 2σ circle of I, after which the two class covariances add up to I. The rotation Vᵀ then diagonalises both at once: wherever one class has variance d, the other has 1 − d, so the axis that is best for class 1 is worst for class 2. The arrows are the CSP patterns, the columns of (Wᵀ)⁻¹, which end as unit vectors along the axes.

The ratio depends only on the direction of \(w\). Doubling \(w\) multiplies both variances by four and leaves the ratio unchanged. With two channels, a direction is one angle \(\theta\), so the whole objective fits in one curve. The figure below turns \(w\) over the same samples.

Histograms of \(w^\top x\) per class
θ0° wTΣ1w (class 1)– wTΣ2w (class 2)– log ratio–
Session-1 filters, log ratio on the data shown
max end– min end–
\(\log\big(w^\top\Sigma_1 w \,/\, w^\top\Sigma_2 w\big)\) for every direction, \(\theta \in [0^\circ, 180^\circ)\)
δ = 0°
Toy data: the same simulated two-channel samples as the figure above (band-passed time samples, not trials; both classes have zero mean). Drag the ring at the tip of the arrow, click or tap the plot to point the arrow there, or use the slider. The two projected variances change with the direction of \(w\) and only with its direction, since scaling \(w\) scales both. The curve has two ends, and they are the two CSP filters: the max end is the filter behind the \(z_1\) axis at the end of the figure above, the min end the one behind \(z_2\). The ends are not mirror images (their \(\lvert\log\lambda\rvert\) differ) and the two filters are not orthogonal. They are filters, not the patterns drawn as arrows above. The max-end filter does not follow the long axis of \(\Sigma_1\): it sits between that axis and the short axis of \(\Sigma_2\), because a low class-2 variance raises the ratio as much as a high class-1 variance. The session shift rotates every sample, \(x \to R(\delta)\,x\), a minimal model of a shifted cap. The extreme values do not change, but the best directions turn by \(\delta\), so the session-1 filters (grey) lose contrast; far enough, one of them crosses zero.

The Feature

After filtering, CSP usually keeps log-variance features:

\[f_j = \log \left(\frac{\operatorname{var}(Z_j)} {\sum_{\ell=1}^{K}\operatorname{var}(Z_\ell)}\right).\]

The classifier sees \(f\), not the whole time series.

This turns a trial into a short vector. A linear classifier such as LDA often works well after this step because CSP already did much of the class separation.

Why Filtering Comes First

Motor-imagery information sits in frequency bands. The usual targets are mu and beta rhythms.

But the best band can move by subject, session, and task.

If CSP is fitted on a poor band, it learns spatial filters for weak signal. The classifier then receives clean-looking but useless features.

This is the reason for FBCSP.

Filter-Bank CSP

FBCSP repeats CSP across several frequency bands.

The pipeline is:

raw EEG trial
-> bandpass filter 1
-> CSP features for band 1
-> bandpass filter 2
-> CSP features for band 2
-> ...
-> concatenate features
-> select useful features
-> train classifier

The filter bank spreads the bet.

One subject may carry signal around 8-12 Hz. Another may show stronger separation around 18-26 Hz. FBCSP lets the data choose useful bands through the training procedure.

Where Leakage Appears

CSP is supervised. It uses labels.

FBCSP often adds feature selection. That also uses labels.

So the safe order is strict:

  1. split train and test,
  2. fit band filters if they are learned,
  3. fit CSP on training trials only,
  4. select features using training folds only,
  5. apply the fitted pipeline to test trials.

If CSP sees the test labels, the result is not a test result. The simulator in Train/Test-Safe EEG Feature Extraction runs both orders on pure noise and shows how far the wrong one can inflate accuracy.

What CSP Does Not Solve

CSP does not remove all session shift.

It can be sensitive to covariance changes. A filter fitted on one session may not remain optimal in the next session. Electrode impedance, fatigue, attention, and small cap shifts can move the covariance structure.

The session slider in the filter figure above shows the simplest case. Rotating the channels keeps the best achievable ratios, but it moves the directions that reach them, so the old filters lose contrast. Recentring each session with its own whitening, as covariance alignment does, does not undo a pure rotation. The aligned sessions still differ by the same rotation.

That does not make CSP weak. It means CSP is a feature extractor, not a deployment policy.

For cross-session work, CSP often needs help from whitening, covariance alignment, domain adaptation, or source-session selection.

The Useful Mental Model

CSP builds discriminative sensors.

FBCSP builds discriminative sensors in several frequency bands.

The classifier is the last step. The main work happens before it.

For motor-imagery EEG, this is the clean pipeline:

frequency structure -> spatial contrast -> log-variance feature -> classifier

Once that is clear, the later questions become easier.

Which bands are stable?

Which spatial filters transfer across sessions?

Which source sessions should be trusted?

Those questions are the bridge from feature extraction to domain adaptation.