Spatial Filters and Common Spatial Patterns

Every scalp electrode hears the whole brain at once. A spatial filter is a weighted sum of electrodes that listens to one part. Common spatial patterns find the best one by solving a single eigenproblem.

Learn how an imagined hand movement shows up in 8 or 22 blurred channels, how CSP unmixes them, and how a one-line classifier and a 796-parameter network finish the job.

Press C3 alone, then drag the skull blur up. The two hands stop separating. Press CSP filter and they come back. Then we build, piece by piece, the covariance, the whitening, the eigenproblem, the log-variance feature, LDA and EEGNet.

You need a rough idea of variance (how spread out a signal is) and a little linear algebra (a matrix turns one list of numbers into another). We build the rest from zero.

Built from primary sources, not course slides: Ramoser, Muller-Gerking and Pfurtscheller 2000, Blankertz et al. 2008, Lawhern et al. 2018 (EEGNet) and the BCI Competition IV dataset 2a description. Every idea is re-taught here in our own words and devices. Lesson 14 of the Brain-Computer Interfaces series; it follows Lesson 13, ERPs and the P300.

Separate the two hands

0.90
40

Toy scalp, not recorded data: 8 electrodes, two motor sources that lose 65% of their power on the opposite side during imagery, one steady occipital rhythm and sensor noise, all seeded so every visit is the same. The filter and threshold are the real algorithms from this lesson (CSP, then a one-dimensional LDA), trained on the trials you set and scored on 100 fresh ones. Real accuracies are in Chapter 10.

Chapter 0

Why One Electrode Is Not Enough

See how the skull blurs every brain signal across the whole scalp, and why that forces us to combine electrodes

Picture a volunteer in a comfortable chair. A cap with 22 small metal discs sits on her head. A screen shows an arrow pointing left. She does not move. She only imagines squeezing her left hand, for four seconds.

Somewhere under the cap, a patch of her brain that controls the left hand has just changed its behaviour. We want a computer to notice. That computer is a brain-computer interface (BCI): a system that reads brain signals and turns them into a command, such as moving a cursor.

The cap records EEG, electroencephalography: the tiny voltages that brain activity leaves on the scalp, measured by electrodes (the metal discs). One number per electrode, 250 times a second in the dataset we use in this lesson. The task she is doing is called motor imagery: thinking about a movement without making it.

The skull is frosted glass

Here is the trouble. A brain signal does not travel in a straight line to the nearest electrode. It spreads through brain tissue, fluid, bone and skin. Every electrode receives a smeared mix of many sources. This smearing is called volume conduction.

Think of a room with eight microphones and three people talking. No microphone hears one person alone. Each hears all three, some louder, some softer. Reading one microphone tells you little about one speaker.

In EEG the “speakers” are patches of cortex, which we call sources. The “microphones” are electrodes. Drag the blur in the device below. At low blur, the electrode over a source hears it clearly and its neighbours hear little. At high blur, even the electrode on the other side of the head hears it.

One source, eight listeners

Choose a source, then drag the blur. Each dot is an electrode. Its size and number show how much of the source it hears, as a share of the loudest electrode.

0.90

Illustrative model, not a head simulation: an electrode hears a source with strength exp(−d² / 2s²), where d is the distance on the drawn head and s is the blur. Real volume conduction depends on the skull and tissue of each person. The same toy scalp drives every device in this lesson.

The left and right motor areas sit about as far apart as the electrodes C3 and C4. At the default blur of 0.90, C4 still hears 54% of what C3 hears from the left source. The two hands are not on separate channels. They are mixed on every channel.

The trade this lesson is about

If every electrode is a mix, one electrode cannot isolate a source. But several electrodes together can. Each electrode holds a different blend of the same sources. A weighted sum of electrodes, with the right weights, can cancel the sources we do not want and keep the one we do.

That weighted sum is a spatial filter. It filters across space (which electrode) instead of across time (which frequency). The rest of the lesson answers three questions. What should a filter look for? How do we find the weights? And what do we do with its output once we have it?

The answer to the first is variance. When a hand area wakes up for imagery, the rhythm over it gets quieter. Quieter means lower variance. We want the filter whose output has high variance for one hand and low variance for the other. The method is called common spatial patterns (CSP), and Ramoser, Muller-Gerking and Pfurtscheller published it for imagined hand movement in 2000. Their abstract reports best single-trial results of 90.8%, 92.7% and 99.7% for three subjects.

The road

  1. What changes in the signal: mu and beta rhythms, and the timing of one trial in the real dataset.
  2. A spatial filter: a weighted sum of electrodes, and its variance ratio.
  3. Covariance and whitening: the shape of the data, and how to make it round.
  4. CSP as an eigenproblem: the equation, and a 2 by 2 solved by hand.
  5. Filters and patterns: why a filter map and a pattern map look different.
  6. Log-variance features: turning each filtered trial into a few numbers.
  7. LDA: the classifier that draws the line.
  8. When CSP breaks: artifacts, few trials, a new day.
  9. EEGNet: a 796-parameter network that learns the same recipe.
  10. Field guide: numbers, code, and where to go next.
Why does a BCI combine many electrodes with a spatial filter instead of reading the single electrode nearest the brain area it cares about?

Chapter 1

Imagery Makes a Rhythm Quieter

Find the one thing that changes in the EEG when a hand area wakes up, and see how one real trial is timed

Sit still and do nothing. Your motor cortex, the strip of brain that plans movement, is idling. Millions of its neurons switch on and off together, like a stadium crowd clapping in time. On the scalp this shows up as a steady wave about 10 times a second.

Now imagine squeezing your left hand. The neurons that control that hand stop clapping together. Each one works on its own job. The synchronised wave over that area gets quieter. The brain did not get less active. It got less in step.

Mu, beta, and a name for the drop

A rhythm is a wave that repeats at a steady rate. Rate is counted in hertz (Hz): waves per second. Over the motor cortex two rhythms matter. The mu rhythm sits at 8 to 12 Hz. The beta rhythm sits higher, with the EEGNet paper giving 18 to 26 Hz. Together they are the sensorimotor rhythm.

The drop in rhythm strength is called event-related desynchronization, or ERD. “Event” is the imagined movement. “Desynchronization” means the neurons fell out of step. Lawhern and colleagues put the key fact in one line: mu and beta desynchronize over the sensorimotor cortex on the side opposite to an actual or imagined movement.

So left-hand imagery quiets the right motor area, near electrode C4. Right-hand imagery quiets the left area, near C3. That crossing over is the whole code. If we can measure rhythm strength on each side, we can read which hand was imagined.

Rhythm strength is variance

How do we measure the strength of a wave? Take its samples, subtract the average, square each one, and average the squares. That number is the variance: how far the signal swings around its middle, in squared units.

P
The power of the signal over this stretch of time. For a band-passed signal, variance and band power are the same number (Blankertz et al. 2008).
1/T
Divide by the number of samples, so a longer trial does not look louder.
T
The number of samples in the trial window. Two seconds at 128 samples per second gives T = 256.
∑
Add up the terms for every sample from the first to the last.
xt
The voltage at sample number t, after we keep only the band we care about.
x̄
The average voltage. For a band-passed signal it is close to zero.

A band-pass filter keeps one range of frequencies and removes the rest. Pass 8 to 12 Hz and the variance left is the mu power. (Our lessons on the DFT and the STFT show how frequency content is measured, and Lesson 12 shows these rhythms in detail.)

x = [2, −1, 3, −2, 1] → mean 0.6 → squares 1.96 + 2.56 + 5.76 + 6.76 + 0.16 = 17.2 → P = 17.2 / 5 = 3.44

Halve every voltage and each square shrinks to a quarter, so P becomes 0.86. A rhythm whose amplitude falls to 71% of its resting size has lost half its power. This is why BCI papers talk about power, and why we will work with variance from here on.

One real trial, second by second

The data in this lesson comes from the Graz group, released for BCI Competition IV as dataset 2a. Nine people, two sessions each on different days. Each session holds 6 runs of 48 trials, 288 trials in all. Four tasks: imagine the left hand, the right hand, both feet, or the tongue. This lesson uses the left versus right pair.

A trial lasts about 6 seconds. At t = 0 a cross appears and a short tone plays. At t = 2 s an arrow shows which hand to imagine, and stays for 1.25 s. The person keeps imagining until the cross disappears at t = 6 s. No feedback is given. Twenty-two electrodes record at 250 Hz, band-passed 0.5 to 100 Hz with a 50 Hz notch against mains hum.

EEGNet’s authors cut each trial to the window 0.5 to 2.5 s after the cue, and resampled to 128 Hz. That gives 2 seconds, T = 256 samples, per channel. The device below draws a toy of this timeline. Drag the playhead through it.

Watch the rhythm drop

Choose a hand, then drag the playhead. The curves show mu power at C3 (teal) and C4 (pink), as a share of resting power. The shaded block is the window EEGNet used.

1.0 s

Timing is the dataset’s (cross and tone at 0 s, cue at 2 s for 1.25 s, imagery until 6 s, then a break; the 0.5 to 2.5 s window is Lawhern et al. 2018). The curves are illustrative: a 55% drop on the opposite side and 4% on the same side, with small ripples. Real depth varies by person and by trial.

At t = 1.0 s both sides sit near their resting level, 96% and 104% with the small ripples. Move to 3.5 s with the left hand chosen: C4 has fallen to 46% of rest while C3 stays at 96%. That gap, in one trial, is the evidence a decoder gets.

Where it goes wrong

Symptom: a trial where power jumps up at every electrode at once. Measurement: the dataset provides 3 extra EOG channels (voltages from the muscles that move the eyes), and the organisers say they are for artifact removal and must not be used for classification. A blink shows as a huge spike there. Fix: mark and drop such trials; the experts who prepared this dataset flagged artifact trials by eye.

A second trap: the mu peak is not at 10 Hz for everyone. If the band is wrong, the drop is small. A common fix is a bank of narrow bands. The FBCSP method used as EEGNet’s baseline cuts 4 to 40 Hz into nine bands of 4 Hz and runs CSP in each.

A person imagines moving the right hand. Where do you expect the mu rhythm to get quieter?

Chapter 2

A Spatial Filter Is a Weighted Sum

Combine two electrodes with weights, and see how the weights decide which hand the output can tell apart

Two microphones stand in a room. One speaker is near the left microphone, another near the right. Both voices reach both microphones. You want a recording of the left speaker alone. What can you do with just the two tracks?

Add them, subtract them, or mix them at any ratio. Subtract the right track from the left and a sound that both hear equally cancels out. The result favours whatever the two tracks hear differently. Choosing the mix is the art.

The filter, as one line of arithmetic

Our two “microphones” are electrodes C3 and C4. Call their voltages x1(t) and x2(t) at time t. A spatial filter picks two numbers, the weights w1 and w2, and makes a new signal:

y(t)
The filtered signal: one number per moment, built from all electrodes. This is what the rest of the pipeline sees.
w1, w2
The weights. Positive adds that electrode, negative subtracts it, zero ignores it.
xi(t)
The voltage on electrode i at time t, after band-pass filtering to mu or beta.
wT x(t)
The same sum written as a dot product of two lists. With 22 electrodes the list has 22 numbers and the formula does not change.

Picking w = (1, 0) reads C3 alone. Picking (1, −1) reads the difference C3 − C4. In the data flow, one trial of C channels and T samples, a matrix of shape (C, T), becomes one row of T samples. A CSP filter bank uses a few weight lists at once and gives a few rows.

The variance of a mixed signal

We care about the power of y, which is its variance (Chapter 1). Variance of a sum is not the sum of variances, because the two electrodes move together. Writing it out for two electrodes:

Var(y) = w1² Var(x1) + w2² Var(x2) + 2 w1 w2 Cov(x1, x2)

Here Cov, the covariance, measures how much two signals rise and fall together. If both go up at the same moments, it is positive. Collect the three numbers Var(x1), Var(x2), Cov(x1, x2) into a 2 by 2 table Σ (sigma). Then the line above is wT Σ w. The next chapter looks at Σ closely.

Now we need one Σ per hand. From the toy scalp at blur 0.90, with C3 and C4 only, ΣL (left-hand trials) has Var(C3) = 1.22, Var(C4) = 0.76 and Cov = 0.73. For right-hand trials the two variances swap: ΣR has Var(C3) = 0.76, Var(C4) = 1.22 and the same Cov. C4 is quieter during left imagery, as Chapter 1 said.

Try w = (1, 0), C3 alone: the output variance is 1.22 for left trials and 0.76 for right trials. The ratio is 1.22 / 0.76 = 1.60. Try w = (1, 1): 1.22 + 0.76 + 2 × 0.73 = 3.44 for the left, and the same 3.44 for the right. Ratio 1.00. The sum is loud but blind: both hands look the same. (The device below uses weights of length 1, so at 45 degrees it reads half of 3.44, which is 1.72 for both classes. The ratio does not change.)

So loudness is not the goal. The goal is a difference between classes. We measure that with the ratio of the two variances.

r(w)
The variance ratio of filter w. A value of 1 means the filter cannot tell the hands apart. Far from 1, in either direction, means it can.
wT ΣL w
The power of the filtered signal during left-hand trials.
wT ΣR w
The power of the filtered signal during right-hand trials.

Turn the filter

Each dot is one moment of EEG: its C3 voltage across, its C4 voltage up. The teal line is your filter direction. Turn it and read the two variances. Press a preset to jump.

0°

Toy data built from the Chapter 0 model at blur 0.90: two motor sources, one losing 65% of its power per class, plus sensor noise 0.35. Dots are seeded random draws from the two covariance tables; the curves are exact wT Σ w for each angle. Angles are measured from the C3 axis.

Start at 0 degrees: C3 alone, ratio 1.60. Press C3 + C4: ratio 1.00, the curves cross. Press Best ratio. The filter lands near 156 degrees, weights about (−0.92, +0.40), and the ratio reaches 2.03. The best filter subtracts part of C4 from C3. It cancels the share of C3 that comes from the other hand’s source.

Where it goes wrong

Symptom: a filter that is very loud but cannot separate the hands. Measurement: its ratio r(w) sits at 1.00, as the sum above showed. This is what happens if you choose w to maximise variance alone: you rediscover the strongest rhythm in the head, usually the one at the back that never changes with the hand. That is the idea behind PCA (see our PCA lesson). Fix: maximise the ratio of the two class variances, not the variance. Chapters 3 and 4 do exactly that.

A filter gives output variance 3.44 on left-hand trials and 3.44 on right-hand trials. What does that tell you?

Chapter 3

Covariance Is the Shape of the Data

Describe each hand by an ellipse, then stretch space until the two ellipses are easy to compare

Plot every moment of a two-electrode recording as a dot: C3 voltage across, C4 voltage up. When both electrodes rise and fall together, the dots lie along a diagonal. When they move on their own, the dots form a round cloud. The cloud has a shape, and the shape is what carries the power.

The covariance matrix

A covariance matrix writes that shape as a table. For C channels it has C rows and C columns. The diagonal cell (i, i) is the variance of channel i. The off-diagonal cell (i, j) is the covariance of channels i and j, how much they move together. The table is symmetric: cell (i, j) equals cell (j, i).

For one trial, stored as a matrix X with C rows and T columns, with the average of each row already removed, the formula is short.

Σ
The covariance matrix of the trial. Shape (C, C). With 22 channels that is 22 × 22, which holds 253 different numbers because it is symmetric.
1/T
Average over the T samples of the trial window.
X
One band-passed trial. Shape (C, T). Row i is channel i over time.
XT
The same table turned on its side, shape (T, C). Multiplying X by XT adds up, over all samples, the product of every pair of channels.

To describe a hand, not one trial, average the trial matrices over all training trials of that hand. Blankertz and colleagues write it as a pooled estimate. We get ΣL for left-hand imagery and ΣR for right. Chapter 2 already used them, as 2 by 2 tables.

Picture each table as an ellipse. Its long axis points where the data swings most. Its length shows how much. A tilted ellipse means correlated channels. The long and short axes are the eigenvectors of the table (directions that the table only stretches, never turns), and the stretch factors are the eigenvalues. If this sounds like PCA, it is: our PCA lesson builds the idea.

Whitening: make the pooled ellipse round

Compare two tilted ellipses by eye and it is hard. Compare them after both have been squeezed so that their sum becomes a perfect circle, and the comparison gets very simple. That squeeze is whitening.

Add the two class tables: Σc = ΣL + ΣR. This is the pooled shape of all the data. Find its eigenvectors (columns of a matrix U) and eigenvalues (a diagonal matrix D). Then build the whitening matrix:

P = D−1/2 UT  →  P Σc PT = I

Read it right to left. UT turns the axes of the pooled ellipse to line up with the grid. D−1/2 divides each axis by the square root of its eigenvalue, so every axis ends with variance 1. The result is the identity matrix I: a unit circle. Apply P to every sample, x becomes Px, and the two class ellipses change shape with it.

Here is the key fact. After whitening, the two class tables are PΣLPT and PΣRPT. They still add up to I. So one is I minus the other. Two tables that sum to I share the same eigenvectors, and their eigenvalues sum to 1 axis by axis: if the left class has variance 0.67 along one axis, the right class has 0.33.

Whiten, then rotate

Blue is left-hand imagery, warm is right-hand. The rings are two standard deviations. Step through the three stages and watch the shapes change. The grey circle is the pooled shape.

Same toy covariance tables as Chapter 2 (C3 and C4 at blur 0.90). The stages are exact: stage 2 applies P = D−1/2UT from the pooled table, stage 3 also applies the eigenvectors VT of the whitened left-hand table. The motion is a straight blend between the matrices.

In stage 1 both ellipses are tilted, and the grey pooled shape is a stretched diagonal. In stage 2 the grey shape becomes a unit circle, but the class ellipses are still tilted inside it. In stage 3 we rotate so both ellipses line up with the axes. Now each axis is a candidate filter. On the horizontal axis the left class has variance 0.67 and the right class 0.33. On the vertical axis they swap.

Where it goes wrong

Symptom: whitening with 22 electrodes and only a few trials gives wild filters. Measurement: look at the eigenvalues of the pooled table. If the smallest is tiny next to the largest, then D−1/2 multiplies that axis by a huge number and amplifies noise. Samples in a trial are strongly related, so 256 samples are not 256 independent facts. Fix: use more trials, fewer channels, or shrink the table toward a simple round shape. Chapter 8 does this with one slider.

After whitening with the pooled table ΣL + ΣR, the left-hand variance along some axis is 0.8. What is the right-hand variance along the same axis?

Chapter 4

CSP Is One Generalized Eigenproblem

Turn “find the filter with the biggest variance ratio” into an equation a computer solves in one step

Chapter 2 asked for the weights w that make the variance ratio r(w) as large as possible. We tried angles by hand. With 22 electrodes there are 22 weights, and no slider can search that space. We need a method that goes straight to the answer.

From a ratio to an equation

Scaling w by 2 multiplies both variances by 4 and leaves the ratio alone. So we may fix the scale. Choose it so that the pooled power of the output is 1: wT(ΣL + ΣR)w = 1. Under that rule the left-hand power wTΣLw is a number between 0 and 1. It is the share of the output’s power that comes from left-hand trials. Call it λ. The right-hand share is 1 − λ.

Maximising λ under that rule is a standard problem. The answer w satisfies this equation (Blankertz et al. 2008 write it as their Eq. 5):

ΣL
The covariance table of left-hand trials. Shape (C, C).
ΣR
The covariance table of right-hand trials. Shape (C, C).
w
A spatial filter: C weights. The equation has up to C answers, one filter per eigenvalue.
λ
The left-hand share of that filter’s output power. Near 1 means the filter listens to the left hand. Near 0 means it listens to the right. Near 0.5 means it is useless.

This is a generalized eigenproblem. An ordinary eigenproblem asks for w with A w = λ w. The generalized one puts a second table on the right: A w = λ B w. Here A is ΣL and B is the pooled table. Blankertz writes the same thing with ΣR alone on the right, so that the “eigenvalue” is the ratio λ / (1 − λ).

Libraries solve it in one call: scipy.linalg.eigh(Sigma_L, Sigma_L + Sigma_R) or MATLAB’s eig(S1, S1+S2). The result is C filters, sorted by λ. Inside, it does what Chapter 3 showed: whiten with the pooled table, then rotate. Here is the whole recipe with its data shapes.

  1. Pool. Average the training trials of each hand: ΣL and ΣR, each (C, C).
  2. Whiten. Eigen-decompose ΣL + ΣR = U D UT. Set P = D−1/2 UT, shape (C, C).
  3. Rotate. Eigen-decompose the whitened left table, S = P ΣL PT = V Λ VT. The diagonal of Λ holds the λ values.
  4. Assemble. W = PT V. Column j of W is filter j, with left share λj.

A 2 by 2 solved by hand

Use the tables from Chapter 2: ΣL = [[1.22, 0.73], [0.73, 0.76]] and ΣR = [[0.76, 0.73], [0.73, 1.22]].

  1. Pool. Σc = [[1.99, 1.46], [1.46, 1.99]].
  2. Eigen of the pool. Along (1, 1)/√2 the stretch is 1.99 + 1.46 = 3.45. Along (1, −1)/√2 it is 1.99 − 1.46 = 0.53.
  3. Whitening rows. Divide each axis by the square root of its stretch (√3.45 = 1.86 and √0.53 = 0.73). P = [[0.381, 0.381], [0.971, −0.971]].
  4. Whiten the left table. S = P ΣL PT = [[0.50, 0.17], [0.17, 0.50]]. Its eigenvalues are 0.50 + 0.17 = 0.67 and 0.50 − 0.17 = 0.33, along (1, 1)/√2 and (1, −1)/√2.
  5. Assemble. w1 = PT(1, 1)/√2 = (0.956, −0.417), λ = 0.67. w2 = PT(1, −1)/√2 = (−0.417, 0.956), λ = 0.33.
  6. Check. w1TΣLw1 = 1.119 + 0.133 − 0.581 = 0.67. The ratio 0.67 / 0.33 = 2.03, the same maximum the slider in Chapter 2 found.

The sums are small, but every step is the real algorithm. A 22 channel version has the same four steps and a 22 by 22 table.

Solve it live

The two bars are the two CSP filters. Blue is the share of power from left-hand trials, warm the share from right-hand trials. A bar split 50/50 carries no information. Change the scalp and watch the split.

0.90
35%

Two-electrode toy (C3 and C4) from the Chapter 0 model, with sensor noise 0.35. Filters are computed by the four steps above, live in your browser, with weights scaled so the pooled output power is 1. At blur 0.90 and 35% the result matches the hand calculation: λ = 0.67 and 0.33.

Drag the blur to its maximum. The two sources overlap more and the shares move toward 0.5: the problem gets harder, as it should. Now raise the power left after ERD to 95%, meaning almost no drop. Both bars sit near 50/50 and the filters are junk. No algorithm can find a difference that is not in the data.

Where it goes wrong

Symptom: cross-validated accuracy near 100%, then a bad result on a new session. Measurement: CSP uses the class labels to compute ΣL and ΣR. If you computed the filters on all trials before splitting, the test trials helped to build the filters. Fix: compute filters only on training trials, inside every fold of cross-validation. Blankertz and colleagues state this rule directly.

A CSP filter has λ = 0.05. What does it tell you?

Chapter 5

Filters Unmix, Patterns Show Where

Tell the recipe that removes the other sources apart from the map of the source itself, and learn which one to plot

A sound engineer isolates a singer from a noisy room. To do it, the engineer subtracts the drums from one microphone, the bass from another, and adds a third. The final recipe has strange numbers: plus 0.9 here, minus 0.4 there. Now ask a different question: where does the singer stand? The recipe does not say. The answer is a different list of numbers: how loudly the singer reaches each microphone.

EEG has the same two lists. The first is the filter: how to combine electrodes to isolate a source. The second is the pattern: how loudly that source reaches each electrode, a map of the head.

Two models, one inverse

Assume the scalp signal is a mix of sources: x(t) = A s(t). Here s(t) lists the source signals and A is the mixing matrix. Column j of A is the pattern aj: source j’s fingerprint on the electrodes. Unmixing runs the other way, s(t) = WT x(t), and column j of W is the filter wj. The two matrices are inverses of each other, and Blankertz and colleagues define the patterns that way.

A
The patterns, one per column. Shape (C, C). Plot a column on the head and you see where that component sits.
W
The filters from Chapter 4, one per column. Shape (C, C).
Σc
The pooled covariance table. CSP scales W so that WTΣcW = I, and that is why the inverse collapses into this simple product.

Worked on the 2 by 2 case: Σc = [[1.99, 1.46], [1.46, 1.99]] and the left-hand filter w1 = (0.956, −0.417). Multiply: a1 = Σc w1 = (1.99 × 0.956 − 1.46 × 0.417, 1.46 × 0.956 − 1.99 × 0.417) = (1.29, 0.56).

Read the two lists. The filter has a negative weight on C4. The pattern has positive weights on both, strongest on C3. Brain activity over the left motor area is stronger at C3 and weaker, but present, at C4: that is the pattern, and it is plausible. The negative filter weight is not a feature of the brain at all. It is a correction: it subtracts the leak of the other source from C4. Check the pairing: w1 · a1 = 0.956 × 1.29 − 0.417 × 0.56 = 1.00, and w1 with the second pattern gives 0.00. Each filter listens to exactly one pattern.

Filter map, pattern map

Pick a component. The left head is its filter, the right head is its pattern. Teal dots are positive, pink dots negative, and a bigger dot is a larger weight. Then raise the sensor noise.

0.35

Toy scalp at blur 0.90 with 8 electrodes, 40 seeded training trials, CSP computed live. “Match” is the cosine between a map and the true map of the motor source that the toy model used (1.00 is a perfect copy). The middle component has no single true source, so it shows no match.

With component 1 and noise at 0.35, the pattern peaks at C3, a smooth blob over the left motor area. Its match with the true source map is 1.00 (to two places). The filter looks messier, with mixed signs and a match of only 0.41. Raise the noise to 1.0. The pattern still matches at 0.98 and the filter stays far off, at 0.50. Noise changes the recipe, but no amount of it turns the recipe into a map.

Which one do you plot?

To use the component on new data, use the filter. To look at it, or to say anything about the brain, use the pattern. Blankertz et al. show both for each component, and Haufe and colleagues, in a 2014 paper on interpreting weight vectors, argue the same point for linear models in general: weights of an unmixing recipe are not a map of the source.

Where it goes wrong

Symptom: a “brain map” of a CSP filter that shows a strong weight over the forehead or far from any motor area. Measurement: plot the pattern for the same component. If the pattern peaks over the motor strip while the filter shows spots elsewhere, you plotted the recipe. Fix: plot A = Σc W. A pattern that still peaks away from the motor strip, or on one noisy electrode, signals an artifact component (Chapter 8).

You want a topographic map showing where a CSP component sits on the scalp. What do you plot?

Chapter 6

One Number per Filter: Log Variance

Squeeze a filtered trial down to a few numbers, and see why the logarithm makes them easy to classify

After CSP you hold a trial that is a few rows of 256 numbers each. A classifier cannot do much with 256 raw voltages that shift in time. It needs a short list of facts about the trial. Chapter 1 told us which fact matters: how strong the rhythm was. So each row becomes one number, its power.

From a trial to a feature vector

A feature is one number that summarises a trial. A feature vector is the list of them. The recipe has three steps, and the shapes show what each one does.

  1. Filter. Keep the J chosen CSP filters, stacked as Wsel, shape (C, J). Apply them: WselT X turns a trial of shape (C, T) into shape (J, T). For 22 channels, 256 samples and J = 6 that is (22, 256) → (6, 256).
  2. Power. For each of the J rows, compute the variance over time. Shape (6, 256) → (6,).
  3. Log. Take the natural logarithm of each. The feature vector f has J numbers.

Blankertz and colleagues write the whole thing as one formula, and add the classifier weights that Chapter 7 will learn.

f(X)
The classifier’s score for trial X. A positive score means one hand, a negative score the other.
wjT X XT wj
The variance of trial X after filter j. Same as wjTΣwj with the trial’s own covariance table.
log
The natural logarithm. It turns a ratio of two powers into a difference of two numbers.
βj
A weight for feature j, learned by the classifier. Large β means the feature matters.
β0
An offset that sets where the score changes sign.

One toy trial, left hand, from the lesson’s 8 electrode scalp: filter 1 output has variance 1.72 and filter 8 has 0.28. The features are ln 1.72 = 0.54 and ln 0.28 = −1.27. A right-hand trial would flip the pattern: filter 1 quiet, filter 8 loud.

Why the logarithm

Variance cannot go below zero, and it varies by multiples: one trial is twice as loud as another, not 0.3 units louder. A plot of raw variances has a long tail to the right. A few loud trials sit far from the rest and pull any straight-line classifier toward them.

A logarithm answers “how many times bigger?” with a plain difference. ln 2 is the same step whether you go from 1 to 2 or from 10 to 20. So log-variances of one class bunch into a symmetric lump, close to the bell shape that the classifier in Chapter 7 assumes. Try it below. Switch between the two views and read the skew, a number that is 0 for a symmetric lump and large when there is a long tail.

Raw or log?

Each bar counts held-out trials whose filtered power falls in that range. Blue is left-hand imagery, warm is right-hand. Switch the view, then shorten the trial.

100 samples

Toy scalp at blur 0.90, 8 electrodes. CSP filter 1 is trained on 60 seeded trials of 100 samples; the 200 test trials are new and have the length you set. Skew is the average third standardised moment of the two classes. Separation d′ is the gap between class means divided by their typical spread: a bigger number means easier to tell apart.

At 100 samples the raw view has a skew of 1.27, a long right tail. Switch to log and the skew falls to 0.21. The gap between the classes, measured in spreads, grows from 2.03 to 2.56, so the log view is also the easier one to separate.

How many filters?

A filter bank of all C filters would overfit: the classifier would have 22 weights to learn from maybe 100 trials. The useful filters sit at the two ends of the λ list (Chapter 4). Blankertz and colleagues find J = 6, three from each end, often satisfactory. The FBCSP baseline in the EEGNet paper uses two pairs, 4 filters, in each of 9 frequency bands from 4 to 40 Hz, which gives 36 features per one-versus-rest question.

Where it goes wrong

Symptom: accuracy collapses when you shorten the decision window to make the interface faster. Measurement: drag the trial length to 20 samples. Each variance is now an average over only 20 numbers, so it jitters, and the class lumps spread and overlap. d′ drops from 2.56 to 2.02. Fix: trade speed for stability on purpose. Use a longer window, or average the features over successive windows, and measure the loss.

Why does the pipeline take the logarithm of each filter’s output variance before classification?

Chapter 7

LDA Draws the Line Between the Hands

Turn two log-variance features into a left-or-right call with one straight line, and see how few trials it needs

You have a table. Each row is a calibration trial: two log-variance numbers and a label, left or right. Plot them as dots on a sheet of paper, blue for left and warm for right. The two groups form two clouds. A decision is now a geometry question: where does a new dot fall, relative to a line between the clouds?

The simplest line is a perpendicular bisector: halfway between the cloud centres. It works when both clouds are round. They are rarely round. One feature may spread more than the other, and the two may move together. Linear discriminant analysis (LDA) is the classic fix.

The idea: walk between the means, in whitened space

LDA assumes each class is a bell-shaped cloud, and that both clouds have the same shape. Call the class centres (means) μL and μR, and the shared spread table Σw, the average within-class covariance of the features. Whiten the features with Σw, exactly as in Chapter 3. Both clouds become round. In that space the best line is the bisector. Translated back to the original features it is a straight line with these weights:

w
The classifier weights, one per feature (J numbers). They are the βj of Chapter 6.
Σw−1
The inverse of the shared spread table. It undoes the stretch and tilt of the clouds, the same job as whitening.
μR − μL
The arrow from the left-hand cloud centre to the right-hand cloud centre.
b
The offset, chosen so that the score is exactly zero at the midpoint between the two centres.
f
The feature vector of one trial, from Chapter 6.
z
The score. Positive: call it right hand. Negative: call it left hand. Zero: exactly on the line.

A worked example, every step

Take two features and these illustrative numbers. Left-hand mean μL = (0.6, −1.0). Right-hand mean μR = (−0.4, 0.2). Shared spread Σw = [[0.20, 0.05], [0.05, 0.30]].

  1. Invert the spread. The determinant is 0.20 × 0.30 − 0.05² = 0.0575. Σw−1 = [[5.22, −0.87], [−0.87, 3.48]].
  2. Arrow between means. μR − μL = (−1.0, 1.2).
  3. Weights. w = (5.22 × −1.0 + −0.87 × 1.2, −0.87 × −1.0 + 3.48 × 1.2) = (−6.26, 5.04).
  4. Midpoint. (0.1, −0.4). Offset b = −(−6.26 × 0.1 + 5.04 × −0.4) = +2.64.
  5. Score a new trial f = (0.5, −0.9): z = −3.13 − 4.54 + 2.64 = −5.03. Negative, so left hand.
  6. Score another f = (−0.5, 0.3): z = 3.13 + 1.51 + 2.64 = +7.29. Positive, so right hand.

Notice that the weights are not just the arrow between the means, (−1.0, 1.2). The inverse spread rescaled and rotated it. That is why LDA beats a plain bisector when the clouds are not round.

The model has J weights and one offset, three numbers for two features. It trains by arithmetic, not by search, so a calibration session of a few dozen trials is enough. Calibration, the short recording session in which the person follows cues so the system can learn, is how Blankertz and colleagues describe the first phase of their BCI. In scikit-learn the classifier is LinearDiscriminantAnalysis().fit(F, y) with F of shape (n_trials, J).

Draw the line

Bold dots are the trials you train on. Faint dots are 200 fresh trials the line never saw. The line is the LDA boundary. Drag the number of training trials and compare the two scores.

16
1.00

A harder toy setting than the hero (imagery leaves 50% of the power, sensor noise 0.60, trial-to-trial power jitter 0.45), so the mistakes show. Each time, CSP (one filter from each end) and LDA are both trained from scratch on the trials you choose. Test trials are 200 new seeded ones. Results come from one random draw of trials, so they wobble by several percentage points from seed to seed.

At the default of 16 training trials, the line gets 100% of the training trials right and 85% of the fresh ones. Drag down to 6 trials: still 100% on training, only 70.5% on fresh trials. Drag up to 60: the training score falls to 86.7% and the fresh score settles at 83.5%. The two scores meet. That meeting is the sign that the line has learned the pattern, not the particular dots.

Where it goes wrong

Symptom: a perfect score during calibration and a poor score in use. Measurement: the gap between training and test accuracy, 100% against 70.5% at 6 trials above. With few trials the shared spread table Σw is estimated badly, and its inverse amplifies the error. Fix: collect more calibration trials, use fewer features, or add shrinkage, which blends Σw toward a round shape before inverting (scikit-learn: solver="lsqr", shrinkage="auto"). Always judge on trials the model did not see.

A decoder scores 100% on its 6 calibration trials and 70% on new trials. What is the most likely cause?

Chapter 8

Three Ways CSP Fails, and the Fix for Each

Break the pipeline with a blink, a short calibration and a new day, then repair it

You trained a decoder on Monday. It scored 84% on held-out trials. On Tuesday the person sits down again, and the score drops to 74%. Nothing in the code changed. This chapter explains why, and shows the two other ways the same pipeline breaks.

The dataset in this lesson was built to expose the third problem. Each person in BCI Competition IV dataset 2a recorded two sessions on different days. One session carries the labels for training. The other is held back to score the classifiers. The organisers rank entries by the kappa coefficient, a score that corrects for guessing. Your decoder meets tomorrow’s head, not today’s.

Failure 1: one blink

Recall that CSP squares and sums, so it rewards loud signals. An eye blink or a jaw clench puts a huge voltage on a few electrodes. If one training trial has such an artifact, and the artifact is not spread evenly across the two classes, one CSP filter will lock onto it. Blankertz and colleagues give a real case: the best-eigenvalue filter in one recording was caused mainly by a single trial.

Symptom: the best filter has a very high λ (say 0.92) but its pattern peaks on one forehead-side electrode, not over the motor strip. Measurement: plot the pattern (Chapter 5), and compute the total power of each trial. A trial with several times the median power is suspect. Fix: drop such trials before computing ΣL and ΣR. This lesson’s device uses a simple rule: drop any training trial with more than 3 times the median total power. More robust options exist, such as scoring filters by the ratio of per-trial medians instead of pooled eigenvalues, a remedy that Blankertz et al. propose.

Failure 2: too few trials

Eight trials give a table Σ that is mostly noise. You met the result in Chapter 7: perfect training score, weak test score. Shrinkage helps. It blends each estimated table with a plain round one:

Σ′ = (1 − γ) Σ + γ · (trΣ / C) · I

Here γ is between 0 and 1, trΣ is the sum of the diagonal (total power), C is the number of channels, and I is the identity. The round part has the same total power as Σ but no tilt, so it cannot overfit a tilt that is not there. Shrinkage is applied to ΣL and ΣR before they go into the eigenproblem.

Failure 3: a new day

Electrodes sit a few millimetres off. Impedance changes. The person is tired. The patterns move a little, and the filters you computed yesterday are tuned to yesterday’s patterns. This is called nonstationarity: the statistics of the signal change over time. In the device, “next day” means a skull blur of 1.25 instead of 1.00 and sensor noise 0.75 instead of 0.60. Those two numbers are illustrative, not measured.

Symptom: accuracy decays between sessions. Measurement: always evaluate on a later session, as the competition did, not only by cross-validation inside one session. Fix: re-calibrate with a few new trials (Lesson 21 on drift covers this), regularise, or adapt the classifier as new labelled trials arrive.

Break it, then repair it

The head is the pattern of CSP filter 1. The bars score the decoder on its own training trials, on 200 fresh trials from the same day, and on 200 from the next day. Switch things on and off.

20
0.0

Same harder toy as Chapter 7 (imagery leaves 50% of power, noise 0.60, jitter 0.45). The blink multiplies electrode FC3 by 6 in one training trial. Results come from one seeded set of trials, so exact figures change with the seed; the direction of each effect is what the device shows. The next-day set uses blur 1.25 and noise 0.75 (illustrative).

Start at the defaults, 20 trials, nothing switched on: 100% on training, 84% same day, 74% next day. Press Blink in a training trial. Training falls to 80%, same day to 72%, next day to 65%, and the head lights up at FC3 with λ = 0.92. Press Drop extreme-power trials. One trial is removed, and the scores climb to 95%, 85% and 78%, a little above the clean figures.

Now clear both switches and drag the trials down to 8. At zero shrinkage the next-day score is 65%. At shrinkage 0.5 it is 69%, while the same-day score stays at 85%. The gain depends on the draw of trials: on two other random draws it was larger (from 52% to 73%) and smaller (69% to 73%). At 20 trials, shrinkage 0.6 barely matters (next day 74% against 75%).

The best CSP filter has λ = 0.93, but its pattern peaks on a single frontal electrode, far from the motor strip. What is the most likely explanation, and the first thing to try?

Chapter 9

EEGNet Learns the Same Recipe

Rebuild band-pass, spatial filter and pooling as four small layers, and count every weight

The CSP pipeline has stages a person designed: pick a frequency band, find spatial filters, take log variance, fit a line. Each stage is sensible, and each is fixed in advance. Suppose you hand all four jobs to a neural network and let it learn them together from the labels. That is the idea behind EEGNet, by Lawhern and colleagues at the US Army Research Laboratory (Journal of Neural Engineering, 2018).

The two deep networks that the EEGNet paper compares against have 40,644 to 175,727 weights, depending on the dataset (its Table 3). EEG data sets are small, a few hundred trials per person. EEGNet asks a different question: how small can a network be and still work across several kinds of brain signal? Its authors tested four: the P300 and error-related negativity (both event-related potentials), movement-related cortical potentials, and the sensorimotor rhythm of this lesson.

Four layers, with their shapes

A convolution slides a small window of weights along the data, and each position gives one output number. A feature map is the list of outputs from one set of weights. EEGNet uses convolutions in three unusual shapes. Follow one trial: C = 22 electrodes, T = 256 samples (2 seconds at 128 Hz), as in the paper’s sensorimotor setting.

  1. Temporal convolution. F1 filters, each (1, K) in size, slide along time on every electrode separately. They act as band-pass filters. Input (1, 22, 256) becomes (F1, 22, 256). The paper sets K to half the sampling rate, 64 samples at 128 Hz, so the filters see frequencies from 2 Hz up. It used 32 for this dataset, because the data were high-passed at 4 Hz. Then batch normalisation.
  2. Depthwise spatial convolution. A window of size (C, 1) covers all electrodes at one instant, and each temporal filter gets its own D of them. This is a spatial filter, learned, per frequency band. (F1, 22, 256) becomes (D·F1, 1, 256). Then batch normalisation, an ELU nonlinearity, average pooling by 4 in time, (D·F1, 1, 64), and dropout.
  3. Separable convolution. First a (1, 16) window along time on each map alone, which is half a second at the 32 Hz rate after pooling. Then a (1, 1) mix across maps. Result (F2, 1, 64) with F2 = D·F1. Then batch normalisation, ELU, average pooling by 8, (F2, 1, 8), dropout.
  4. Classifier. Flatten to F2 × 8 numbers and apply one dense layer to N classes with softmax.

Compare with the CSP pipeline. Layer 1 plays the part of a band-pass filter bank. Layer 2 plays the part of CSP, a spatial filter in each band (the paper says this pair of steps is inspired in part by FBCSP). The pooling and layer 3 summarise the filtered signals over time into a few numbers, the job that log variance did. The dense layer plays the part of LDA. Where CSP computes its filters from covariance tables by one solve, EEGNet finds them by gradient descent on the classification loss.

The authors used Adam with default settings and categorical cross-entropy loss, trained for 500 iterations with validation stopping, and set dropout to 0.5 for within-subject tests and 0.25 for cross-subject tests.

Counting parameters

Each layer's weight count follows from its shape. Use the sensorimotor setting: C = 22, T = 256, F1 = 4, D = 2, F2 = 8, K = 32, N = 4.

  1. Temporal convolution: F1 × K = 4 × 32 = 128. Batch norm: 2 × F1 = 8.
  2. Depthwise: C × D × F1 = 22 × 2 × 4 = 176. Batch norm: 2 × 8 = 16.
  3. Separable: 16 × D × F1 + F2 × D × F1 = 128 + 64 = 192. Batch norm: 2 × 8 = 16.
  4. Dense: 8 × (256 / 32) = 64 inputs, times 4 classes, plus 4 offsets = 260.
  5. Total: 128 + 8 + 176 + 16 + 192 + 16 + 260 = 796.

The paper’s Table 3 lists 796 trainable parameters for EEGNet-4,2 on this dataset, 1,716 for EEGNet-8,2, 40,644 for ShallowConvNet and 152,219 for DeepConvNet. The two EEGNets are 51 and 24 times smaller than the shallow net. Try the numbers yourself.

Trace the signal, count the weights

Each row is a layer: its output shape and its trainable weights. The bar is the weight count. Press the button to follow one trial down the stack, or change the sizes.

4
2

Counts follow the paper’s Table 2 with K = 32, T = 256 (2 s at 128 Hz), N = 4 classes, F2 = D × F1, and count batch-norm scale and shift and the dense offsets; at 22 electrodes, F1 = 4, D = 2 the total is the paper’s 796 (Table 3), and at F1 = 8 it is 1,716. Other settings are our arithmetic, not paper results. Both totals were also checked against a PyTorch build of the same layers.

At the defaults the total reads 796. Slide F1 to 8 and it reads 1,716. Change to 64 electrodes and the total rises to 1,132: only the depthwise layer depends on the number of electrodes. Even at 16 temporal filters and 4 spatial filters per band, 9,380 weights at 22 electrodes, the network is smaller than ShallowConvNet.

python (checked: 796 and 1,716 weights)
class EEGNet(nn.Module):
    def __init__(self, C=22, T=256, N=4, F1=4, D=2, K=32, p=0.5):
        super().__init__(); F2 = F1 * D
        self.block1 = nn.Sequential(
            nn.Conv2d(1, F1, (1, K), padding="same", bias=False),   # (1,C,T) -> (F1,C,T) band-pass filters
            nn.BatchNorm2d(F1),
            nn.Conv2d(F1, F1*D, (C, 1), groups=F1, bias=False),      # -> (F1*D,1,T) spatial filters per band
            nn.BatchNorm2d(F1*D), nn.ELU(), nn.AvgPool2d((1, 4)), nn.Dropout(p))
        self.block2 = nn.Sequential(
            nn.Conv2d(F1*D, F1*D, (1, 16), padding="same", groups=F1*D, bias=False),
            nn.Conv2d(F1*D, F2, (1, 1), bias=False),            # -> (F2,1,T/4)
            nn.BatchNorm2d(F2), nn.ELU(), nn.AvgPool2d((1, 8)), nn.Dropout(p))
        self.fc = nn.Linear(F2 * (T // 32), N)                 # logits; softmax lives in the loss
    def forward(self, x):                                      # x: (batch, 1, C, T)
        return self.fc(self.block2(self.block1(x)).flatten(1))

The paper also constrains the depthwise weights to a maximum norm of 1 and the dense weights to 0.25; the sketch leaves that out.

What the paper found

On this sensorimotor dataset, in within-subject four-fold tests, the paper reports that ShallowConvNet and the FBCSP baseline performed very similarly, DeepConvNet performed worse than all other models, and EEGNet-8,2 performed similarly to FBCSP. In the cross-subject test, which never touched the test person’s training data, the paper found no significant difference among the CNN models, with all of them slightly ahead of FBCSP. Across the four paradigms, the abstract summarises: EEGNet generalises better than the reference algorithms when only limited training data is available. These are reported as stated in the text; the accuracy numbers are in the paper’s figures.

Where it goes wrong

Symptom: a network that scores well on the training trials and poorly on the next session. Measurement: weights per training trial. The competition gives 288 trials per session. DeepConvNet has 152,219 weights for 288 trials, about 530 per trial. EEGNet-4,2 has 796, under 3 per trial. Fix: keep the network small, use dropout (0.5 for within-subject work), and test on a later session. A second trap: the temporal window is tied to the sampling rate. Copy K = 64 to data sampled at 250 Hz and the filter covers 0.26 s, not 0.5 s. Set K to half the sampling rate.

In EEGNet, which layer does the job that CSP does, learning a spatial filter for each frequency band?

Chapter 10

The Field Guide

Keep the numbers, the checklist and the working code, and find where to go next

Imagined hand movement quiets the mu and beta rhythms over the opposite side of the motor cortex. The skull blurs that change across every electrode. CSP unmixes it: it solves one generalized eigenproblem on the two class covariance tables and returns filters whose output power is most different between the hands. The log of each filtered trial’s variance gives a few features, and LDA draws a line between the classes. EEGNet learns the same chain, band-pass, spatial filter, summary, classifier, in four small layers.

The numbers and terms

ItemValueSource
BCI Competition IV 2a9 subjects, 4 classes (left hand, right hand, both feet, tongue), 22 EEG and 3 EOG channels, 250 Hz, 0.5 to 100 Hz band-pass with a 50 Hz notchGraz data set A description
Sessions and trials2 sessions on different days; 6 runs of 48 trials; 288 trials per sessionGraz data set A description
One trialcross and tone at 0 s; cue at 2 s for 1.25 s; imagery until 6 sGraz data set A description
Scoringkappa coefficient; the largest kappa winsGraz data set A description
EEGNet window0.5 to 2.5 s after the cue, resampled to 128 Hz: T = 256Lawhern et al. 2018
Rhythmsmu 8 to 12 Hz, beta 18 to 26 Hz, drop on the side opposite the handLawhern et al. 2018
CSP filters keptJ = 6: three from each end of the λ listBlankertz et al. 2008
FBCSP baseline9 bands of 4 Hz from 4 to 40 Hz; 2 CSP pairs per band; 36 features per one-versus-rest questionLawhern et al. 2018
EEGNet-4,2 / 8,2796 / 1,716 weights on this datasetLawhern et al. 2018, Table 3
ShallowConvNet / DeepConvNet40,644 / 152,219 weights on this datasetLawhern et al. 2018, Table 3
Ramoser et al. 2000best results for three subjects: 90.8%, 92.7%, 99.7% (from the abstract)Abstract only; full text not opened
Toy results in this lessonillustrative, from a seeded 8 electrode simulationThis page
Spatial filter
A weight for each electrode. The weighted sum is a new signal. Columns of W.
Spatial pattern
The scalp map of a component. Columns of A = Σc W. Plot this one.
λ
A filter’s share of power from one class. Near 0 or 1 is useful. Near 0.5 is not.
Whitening
Transforming the data so the pooled covariance table is the identity.
ERD
Event-related desynchronization: a rhythm that gets quieter around an event.

Checklist before you trust a CSP decoder

The core as code

python (checked on synthetic data)
import numpy as np
from scipy.linalg import eigh
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

def trial_cov(X):                                  # X: (C, T), already band-passed
    X = X - X.mean(axis=1, keepdims=True)
    return X @ X.T / X.shape[1]                       # (C, C)

def fit_csp(trials, y, n_pairs=1, shrink=0.0):         # trials: (n, C, T), y in {0, 1}
    def mean_cov(c):
        S = np.mean([trial_cov(x) for x in trials[y == c]], axis=0)
        return (1 - shrink) * S + shrink * np.trace(S) / len(S) * np.eye(len(S))
    S0, S1 = mean_cov(0), mean_cov(1)
    lam, W = eigh(S1, S0 + S1)                         # generalized eigenproblem; lam = class-1 share, ascending
    keep = np.r_[0:n_pairs, len(lam) - n_pairs:len(lam)]
    return W[:, keep], lam                              # filters (C, 2*n_pairs)

def features(trials, W):
    Z = np.einsum("cj,nct->njt", W, trials)              # (n, 2m, T)
    return np.log(Z.var(axis=2))                     # (n, 2m) log-variance features

W, lam = fit_csp(Xtr, ytr)                             # training trials only
clf = LinearDiscriminantAnalysis().fit(features(Xtr, W), ytr)
print(clf.score(features(Xte, W), yte))                # held-out trials

Where it sits, and where to go next

CSP is a linear, two-class method. Extensions handle more classes with one-versus-rest (as the FBCSP baseline does), many bands (filter banks), and artifacts (robust variants in Blankertz et al.). EEGNet moves the whole chain into a small network. The newest line, EEG foundation models, pretrains a large network on many recordings. The reading list names CBraMod (ICLR 2025, arXiv 2412.07236), which uses separate spatial and temporal attention in parallel. Our lesson on EEG foundation models covers that step.

Keep going: Lesson 13 (the other classic EEG signal, event-related potentials), Lesson 15 (linear decoders for continuous control), Lesson 21 (drift and recalibration, the cure for Chapter 8’s third failure), Lesson 11 (cleaning EEG before any of this), and the PCA and ICA lessons for the other two ways to unmix channels.

Sources

Every claim above traces to one of these. Opened and read: Blankertz, Tomioka, Lemm, Kawanabe, Muller, “Optimizing spatial filters for robust EEG single-trial analysis,” IEEE Signal Processing Magazine 25(1), 2008 (authors’ draft); Lawhern et al., “EEGNet,” J. Neural Engineering 2018, arXiv 1611.08024 (published version DOI); Brunner, Leeb, Muller-Putz, Schlogl, Pfurtscheller, “BCI Competition 2008, Graz data set A”. Abstract only: Ramoser, Muller-Gerking, Pfurtscheller, “Optimal spatial filtering of single trial EEG during imagined hand movement,” IEEE Trans. Rehab. Eng. 8(4), 2000. Cited by title and checked in Crossref, not read: Tangermann et al., “Review of the BCI Competition IV,” Frontiers in Neuroscience, 2012; Haufe et al., “On the interpretation of weight vectors of linear models in multivariate neuroimaging,” NeuroImage, 2014. No course slides were used for this lesson.

The takeaway. CSP finds the weighted sum of electrodes whose power differs most between two mental states. Log variance and LDA turn that into a decision. EEGNet learns the same chain from data with 796 weights.
You report 96% accuracy for a CSP and LDA decoder, with filters computed on all trials before cross-validation. What should you do before trusting the number?

Open Teach and explain the CSP pipeline on a whiteboard without looking back. Or go to the hero, set the skull blur to 1.4 and see for yourself how far one filter can carry you.

BCI 14: Spatial Filters and Common Spatial Patterns
Ramoser et al. 2000 · EEGNet · Back to Gleams