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.
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
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.
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.
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.
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.
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.
Chapter 1
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.
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.
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.
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.
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.
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.
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.
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.
Chapter 2
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.
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:
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.
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.
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.
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.
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.
Chapter 3
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.
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.
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.
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.
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.
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.
Chapter 4
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.
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):
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.
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]].
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.
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.
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.
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.
Chapter 5
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.
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.
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.
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.
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.
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.
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).
Chapter 6
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.
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.
Blankertz and colleagues write the whole thing as one formula, and add the classifier weights that Chapter 7 will learn.
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.
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.
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.
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.
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.
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.
Chapter 7
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.
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:
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]].
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).
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.
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.
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.
Chapter 8
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.
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.
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.
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.
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.
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%).
Chapter 9
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.
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.
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.
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.
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.
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.
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.
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.
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.
Chapter 10
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.
| Item | Value | Source |
|---|---|---|
| BCI Competition IV 2a | 9 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 notch | Graz data set A description |
| Sessions and trials | 2 sessions on different days; 6 runs of 48 trials; 288 trials per session | Graz data set A description |
| One trial | cross and tone at 0 s; cue at 2 s for 1.25 s; imagery until 6 s | Graz data set A description |
| Scoring | kappa coefficient; the largest kappa wins | Graz data set A description |
| EEGNet window | 0.5 to 2.5 s after the cue, resampled to 128 Hz: T = 256 | Lawhern et al. 2018 |
| Rhythms | mu 8 to 12 Hz, beta 18 to 26 Hz, drop on the side opposite the hand | Lawhern et al. 2018 |
| CSP filters kept | J = 6: three from each end of the λ list | Blankertz et al. 2008 |
| FBCSP baseline | 9 bands of 4 Hz from 4 to 40 Hz; 2 CSP pairs per band; 36 features per one-versus-rest question | Lawhern et al. 2018 |
| EEGNet-4,2 / 8,2 | 796 / 1,716 weights on this dataset | Lawhern et al. 2018, Table 3 |
| ShallowConvNet / DeepConvNet | 40,644 / 152,219 weights on this dataset | Lawhern et al. 2018, Table 3 |
| Ramoser et al. 2000 | best results for three subjects: 90.8%, 92.7%, 99.7% (from the abstract) | Abstract only; full text not opened |
| Toy results in this lesson | illustrative, from a seeded 8 electrode simulation | This page |
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
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.
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.
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.