One electrode hears 30,000 numbers a second. A threshold set from the median of the noise, not its spread, turns them into the spike events a brain-computer interface decodes.
Learn how a chip or a program finds the few moments when a neuron fired inside a trace that is mostly noise, how to score the result, and why the crude answer is enough to move a cursor.
Slide the threshold, then flip the noise estimate between Spread and Median. Watch which spikes get caught and which get missed. Then we build, piece by piece, the estimate, the crossing rule, the energy operator, the ROC curve and the chip arithmetic.
You need a feel for averages (add up and divide) and what a voltage is (a push on charge, measured in volts). We build the rest from zero.
Built from primary sources, not slides: Quiroga, Nadasdy and Ben-Shaul (2004), Shaikh et al. (2018), Even-Chen et al. (2020) and the Neuralink 2019 platform paper. The sources are listed in the Field Guide. Part of the series on brain-computer interfaces.
Illustrative: a made-up trace, 90 ms at 30,000 samples per second, white-ish noise of 10 µV and a busy neuron (about 130 spikes per second, peaks of 45 to 95 µV). The noise level and the spike times are known because we made them, so every hit and miss is exact. Real recordings give no such answer key.
Chapter 0
See why a brain-computer interface must turn a flood of voltage readings into a short list of event times
Picture a metal wire thinner than a hair. A surgeon has placed its tip a fraction of a millimetre from a brain cell. A small chip reads the wire’s voltage 30,000 times every second and writes each reading down as a number.
Most of those numbers say nothing. They are hiss: the electronics, the salty tissue and the faint chatter of hundreds of far-away cells. Now and then, for about one thousandth of a second, the voltage dips sharply and comes back. That dip is the cell talking.
The dip is called a spike, or an action potential. It is the all-or-nothing electrical pulse a neuron sends to its neighbours. A neuron that is busy sends tens of spikes every second. A neuron that is quiet sends almost none. The rate of spikes carries the message.
A brain-computer interface (a BCI, a system that reads brain signals and turns them into commands) wants that message. Say a person imagines moving a hand to the right. The cells near the electrode speed up or slow down. A program reads how fast each electrode’s cells fire and guesses the intended move. So the first job is simple to say: find the spikes.
Two words first. A sample is one voltage reading. A trace is the stream of samples from one electrode, in order. Voltage here is measured in microvolts (µV): one millionth of a volt. A spike seen from outside the cell is small, typically tens to a few hundred µV. A 1.5 V battery is about 15,000 times larger than a spike of 100 µV.
The data has a fixed shape. For one electrode, one second of recording is a list of 30,000 numbers, one for every sample. If the wire carries a spike at 12.4 ms and another at 31.0 ms, the answer we want is just two numbers: 12.4 and 31.0. That is [30000] → [events], and it is called spike detection.
Two things make this hard. The spikes are short: about 1 ms is 30 samples. And their size is not far above the noise. If you set your eye on a trace you can often see the spikes, but a computer needs a rule. The whole lesson is about that rule, how to set it, how to score it, and how little of the trace it needs to keep.
Drag the window below from one full second down to a few thousandths of a second. At the widest setting the spikes look like thin hairs on a thick fur of noise. Zoom in, and each hair turns into a clear, shaped pulse.
Warm ticks under the trace mark where made-up spikes really are. Drag the window from 1,000 ms down to 3 ms and watch what the spikes look like at each scale.
Illustrative: one made-up second at 30,000 samples per second, noise of 10 µV, about 33 spikes of 60 to 100 µV. Each spike lasts 30 samples (1 ms) in our template. The window shrinks on a logarithmic scale and stays centred on one spike.
Count what the device says. In one second, about 33 spikes cover about 990 of the 30,000 samples. That is 3%. The other 97% is background. A detector keeps the 3% and forgets the rest. That choice is also why later chapters talk about data rate and power: forgetting 97% of the data is cheap, and it is what lets a chip inside the skull talk over a radio.
We follow the usual order of a spike detector. First we learn what the trace is made of. Then we estimate the noise level, and we use a trick that does not break when the neuron is busy. Next we set a threshold from that estimate and turn crossings into events. We add one cleverer signal, the nonlinear energy operator. We score all of it with an ROC curve. Then we ask how the events become BCI features, what they cost on a chip, and how they fail.
Every number printed here is either from a source we opened (they are listed in the last chapter), or it is marked as made up. Our traces are made up on purpose. In a made-up trace we know where every spike is, so we can score the detector exactly.
Chapter 1
Split a raw trace into spike, noise and slow waves, and see why a filter must come before any threshold
Open a recording file and look at the first few numbers from one electrode: 212, 207, 215, 150, 218, 224. The numbers drift up and down by hundreds of microvolts. Somewhere in there a neuron fired. Can you tell where?
Not by looking at the raw values. The trace is a sum of three different things, and only one of them is the spike. Pull them apart and the problem gets easy.
Think of a loud party. Someone beside you speaks: that is the spike, sharp, short and close. Behind them is the murmur of the whole crowd: that is the noise, a steady hiss with no pattern. Under both, the bass of the music comes up through the floor: that is the local field potential (the LFP), a slow swell made by thousands of cells working together.
The spike is a fast, sharp dip lasting about one millisecond. The cell is close to the electrode tip, so its pulse is large enough to stand out.
The noise has two sources. Part is electrical: the metal tip, the tissue and the amplifier all add a small random hiss. Part is biological: hundreds of cells farther away each fire, and their pulses are too faint to pick out one by one. Together they form a fuzz sometimes called hash. We model it as random wiggles around zero.
The LFP is slow. It rises and falls tens of times per second, and its swings can be larger than the spikes. A frequency is how many times a wave repeats each second, measured in hertz (Hz). The LFP lives below a few hundred Hz. A spike is so short that its energy sits mostly between about 300 and 3,000 Hz. (The lesson on the discrete Fourier transform shows how to see this split; the short-time Fourier transform shows how it moves over time.)
That gap in frequency is the opening. A filter is a rule that keeps some frequencies and removes others. A bandpass filter keeps a band and drops what is above and below. In the papers this lesson uses, the band is 300 to 3,000 Hz: Shaikh and colleagues filtered between 300 and 3,000 Hz before detecting, and the minimal recorder in Even-Chen and colleagues keeps roughly 250 Hz to 3 kHz. The LFP falls outside it, and so the bass vanishes.
A real filter needs some maths. We can get the idea with a tiny one. Take each sample and subtract the average of the samples around it. A slow wave barely changes across a few samples, so it equals its own local average and cancels. A spike changes fast, so it does not.
Work it with seven samples. The raw values are 200, 204, 208, 150, 216, 220, 224. The slow wave climbs by 4 each step, and the middle sample holds a spike.
average = (200 + 204 + 208 + 150 + 216 + 220 + 224) / 7 = 1422 / 7 = 203.1
middle sample after the filter = 150 − 203.1 = −53.1 µV
its neighbour after the filter = 208 − 203.1 = +4.9 µV
Before the filter the spike value, 150, sat well inside the range of the slow wave (200 to 224) and no threshold on raw values could single it out cleanly. After the filter the spike is −53.1 and its neighbour is +4.9. The spike now stands apart. The step that gave the extra push was subtracting the slow part.
Try it on a longer trace. Choose Raw, raise the slow wave, and see the spikes sink into the swells. Then choose Filtered.
The trace is spikes plus noise plus a slow wave. Flip between raw and filtered. Then grow the slow wave and watch how much of the picture each one fills.
Illustrative: 100 ms at 30,000 samples per second; noise 10 µV; spikes of 60 to 100 µV; the slow wave is three sines at 8, 20 and 45 Hz. The “filter” subtracts the average of the nearest 45 samples (1.5 ms), which removes roughly everything below 300 Hz. It is a teaching filter. A real chip uses a properly designed one.
Read the device’s sentence. With a slow wave of 200 µV, the raw trace swings over about ten times the size of a spike, so the spike is a small part of the range. After filtering, the swing is a few spike heights and the spikes dominate. One step has changed the problem from impossible to routine.
The filter does not remove the hash. The noise sits in the same band as the spikes, so it stays. What is left is the real problem of this lesson: spikes of a certain height sitting on noise of a certain height. How high is the noise? Chapter 2 answers that, and it is the key step.
Chapter 2
Estimate the noise level with the median of the absolute values, and see why the usual spread gets it wrong on a busy channel
You want a threshold that sits clear of the noise but not far above the spikes. A good rule says: four times the noise level. The rule needs one number first. How big is the noise?
The obvious answer is the standard deviation (SD): the typical distance of a sample from the average. A calm trace gives a small SD. A rough trace gives a large one. Every spreadsheet has the button. But watch what happens on a real channel.
The SD looks at every sample. Samples inside a spike are far from zero, so they pull the SD upward. One spike in a stretch barely matters. A busy neuron, firing a hundred times a second, matters a great deal.
Work a small case. Take nine filtered samples in µV: 3, −8, 5, 2, −60, 6, −4, 9, 1. The −60 is a spike. The other eight are noise. Their SD is 5.1 µV. Add the spike back and the SD of all nine becomes 20.0 µV. One sample out of nine made the “noise” four times larger.
Then the damage compounds. A threshold of four times 20.0 is 80 µV. Most real spikes are smaller than that. The detector misses them. The more the neuron fires, the more spikes it misses. The measurement punishes you for the very thing you want to see. You saw this in the hero at the top of the page: the Spread setting missed spikes that the Median setting caught.
The median is the middle value when you sort the numbers. Sort the nine absolute values: 1, 2, 3, 4, 5, 6, 8, 9, 60. The middle one, the fifth, is 5. The 60 sits at the end of the list. It could be 60 or 6,000 and the middle would not change.
That is the whole idea. A spike is rare in time, so most samples are noise. The middle sample is a noise sample, and a few huge ones cannot drag it. Statisticians call this robust: it keeps working when a few values are wild.
We take the absolute value first, meaning we drop the minus sign, so a dip of 8 and a bump of 8 count the same. The median of the absolute values is a number about as big as the typical noise sample. It is not yet the noise SD. We must scale it.
Noise is usually close to a bell curve, the Gaussian. For a bell curve with spread σ, exactly half of all samples fall within a distance of 0.6745 σ from zero. (Look up the 75th percentile of a standard bell curve and you find 0.6745.) So half the absolute values are below 0.6745 σ, which makes 0.6745 σ the median. Turn the sentence around and you get the estimate.
nine-sample case: median(|x|) = 5 → σ̂ = 5 / 0.6745 = 7.4 µV (SD with the spike: 20.0)
This is the estimate Shaikh and colleagues write down for the “absolute threshold” detector, equations (1) and (2) in their paper: σ equals the median of |x| over 0.6745, and the threshold is k times σ, with k an integer typically between 2 and 5. Quiroga, Nadasdy and Ben-Shaul (2004) is the usual reference for this median-based threshold in spike sorting, but we could not open the paper, so we did not check that claim. We opened the formula in Shaikh et al.; for Quiroga’s own multiplier, secondary sources disagree between 4 and 5, so we do not print one for that paper. In Even-Chen and colleagues, a commercial recorder uses −4.5 times the RMS voltage.
The name median absolute deviation (MAD) belongs to a close cousin: the median of the distances from the median. On a filtered trace the median sits near zero, so the two are nearly the same. Many people say MAD for both.
Now slide the firing rate and compare the two estimates against the truth.
Three bars: the true noise (we made it, so we know it), the spread estimate and the median estimate. Raise the firing rate and watch which bar climbs. The dashed lines are the thresholds at 4 times each estimate.
Illustrative: half a second of made-up filtered trace at each rate, noise 10 µV, spikes 45 to 95 µV, so a typical spike peak is about 70 µV. The estimates are computed live from the samples. Real neurons do not fire at a steady rate, and real noise is not exactly a bell curve.
At 0 spikes per second both estimates agree with the truth, as they should on pure noise. At 200 per second (several cells on one electrode, as in a threshold-crossing channel) the spread reads 13.0 µV and the median reads 10.5 µV. The four-times threshold moves from −42 to −52 µV, which lifts it above the weakest fifth of our spikes. The gap looks modest here because our made-up spikes are short and few. The nine-sample case above shows the extreme.
The symptom: a channel that used to show many events goes quiet when the animal starts a task, although you know the cells are active. The measurement: compute both estimates on the same stretch. If the SD is far above the median-based number, spikes are inflating it. A ratio near 1 means clean noise. A ratio that climbs well above 1 (here 1.15 or more) means contamination. The fix: threshold from the median-based estimate.
The price is memory. To find a median you must sort, or at least hold, many samples. On a desktop that costs nothing. On a chip with a power budget it is a real cost, and Chapter 8 shows a small trick that gets around it.
Chapter 3
Turn a noise estimate into a rule, count each spike once, and pick the multiplier that fits your tolerance for false alarms
You have a clean, filtered trace and a noise estimate of 10 µV. You hold a ruler against the screen. Anything that dips below the ruler line counts as a spike. That is the entire detector. Everything else in this chapter is about drawing the line well.
The line is the threshold. We put it at a multiple of the noise: threshold = −k × σ̂, where k is a number you choose and σ̂ is the median-based estimate from Chapter 2. The minus sign matters. A spike recorded just outside the cell body usually points downward, and the papers this lesson uses count negative crossings: Shaikh and colleagues state that negative threshold crossings of the filtered signal are considered spikes.
A threshold crossing is the moment the trace goes from above the line to below it. In code it is one comparison on neighbouring samples: sample i is below the line, and sample i−1 was not.
python# x: filtered trace, shape [30000], one second, microvolts sigma = np.median(np.abs(x)) / 0.6745 # robust noise estimate, Chapter 2 thr = -k * sigma # for example k = 4, sigma = 10 -> -40 uV below = x < thr # bool [30000] cross = np.flatnonzero(below[1:] & ~below[:-1]) + 1 # sample numbers, shape [n_events] times = cross / 30000.0 # seconds
Follow the data. The input is a list of 30,000 numbers. The comparison makes a list of 30,000 true-or-false values. The last line keeps only the places where false turned to true. The output is a short list of sample numbers. A crossing at sample 4,120 means a spike at 4,120 divided by 30,000, or 137.3 ms.
There is a catch. Noise rides on top of the spike, so the trace can wobble around the line. Take these samples in µV with the line at −40: −8, −22, −41, −38, −44, −30, −12. The trace dips under at −41, pops back above at −38, then dips again at −44. The rule fires twice. There was one spike.
The fix is a dead time: after a crossing, ignore the next stretch of samples. A common stretch is 1 ms, which is 30 samples at 30,000 per second. Even-Chen and colleagues describe checking for crossings in a 1 ms window, and Shaikh and colleagues use the same idea. The choice costs nothing true. After a neuron fires it cannot fire again for a moment, called its refractory period, of about a millisecond or two. So a second spike inside 1 ms from the same cell does not exist.
Before the device, one more design point. The rule looks only at two neighbouring samples, so it needs almost no memory: it keeps the previous sample and the threshold. That is why it suits a chip that must run on every sample, all day, on a tiny power budget. The price is that it trusts the threshold completely, so everything depends on the noise estimate from Chapter 2.
Slide the line in the device below. Switch the dead time off and on. Count the markers.
Dots are single samples of a 13 ms trace with three true spikes (warm ticks). The dashed line is the threshold. Green markers are events the detector reports. Move the line and flip the dead time.
Illustrative: 400 samples of made-up trace, noise 10 µV (the estimate is computed from the trace), three spikes of 55 to 70 µV. Hits and false alarms are counted against the answer key. A crossing within 16 samples of a true peak is a hit.
At 3 times the noise with no dead time you will see extra markers where the trace flutters across the line. The dead time folds each flutter into one event. Raise the line to 7 and the events vanish: the line now sits below every spike.
How far below zero should the line go? Noise on a bell curve falls below −k σ with a probability you can look up. At 30,000 samples per second, that probability turns into a count of false events each second, if the samples were independent. They are not quite, since neighbouring samples are correlated and cross together, so treat the last column as an upper bound.
| Multiplier k | Chance a noise sample is below the line | False crossings per second (upper bound) |
|---|---|---|
| 3 | 0.135% | about 40 |
| 4 | 0.0032% | about 1 |
| 5 | 0.000029% | about 0.009 |
Look at the jump. Going from 3 to 4 cuts false events about 40 times. But a higher line also misses more real spikes: any spike weaker than k times the noise never crosses. The multiplier trades one error for the other. Shaikh and colleagues report that k between 2 and 5 is typical, and in their monkey experiments the best decoding came at k = 3. Chapter 5 gives you the tool to see this trade-off as a curve.
Chapter 4
Multiply each sample by its neighbours so that sharp, tall peaks light up and smooth swells go dark
Look at a spike on a screen and ask what makes your eye catch it. It is tall, and it is quick. A slow swell can be just as tall and you ignore it. A fast flutter can be quick and you ignore it too. A spike is both at once.
The plain threshold of Chapter 3 only checks height. If a slow wave leaks through the filter, the height test sees it as a spike or hides real spikes under a raised noise estimate. We want a signal that scores “tall and quick” together.
The nonlinear energy operator (NEO) is that signal. Kaiser proposed it in 1990 as a simple way to measure the “energy” of a wave. Mukhopadhyay and Ray showed in 1998 that it works for spikes. For each sample x[n], with the sample before it and the sample after it, compute:
Read the formula in words. Square the middle. Subtract the product of its two neighbours. If the neighbours look like the middle, the two terms nearly cancel and the output is near zero. If the middle stands out from its neighbours, the first term wins and the output is large.
Work three small cases, in µV.
a spike: x = 2, −1, −8, −3, 1 → ψ at −8 is 64 − (−1)(−3) = 61
its neighbours: ψ at −1 is 1 − (2)(−8) = 17; ψ at −3 is 9 − (−8)(1) = 17
a slow ramp: x = 10, 11, 12 → ψ at 11 is 121 − (10)(12) = 1
small noise: x = 1, −2, 2 → ψ at −2 is 4 − (1)(2) = 2
The ramp and the noise score 1 and 2. The spike centre scores 61, and its neighbours 17. For a clean wave A sin(ωn) the output is about A² sin²ω, so the score grows with the square of both the height and the speed. A slow swell has a tiny ω and scores near zero, even when it is tall. That is the trick.
The output is positive at a peak whether the spike points up or down, so one threshold above zero is enough. Shaikh and colleagues set it as a multiple l of the average of ψ over a stretch of the trace. We call the multiple l, to keep it apart from k in the plain detector. The crossing and the dead time work as in Chapter 3.
Two practical points. First, NEO needs only two multiplies, one subtract and a running average per sample. It needs no sorting. That is why it suits a chip: Shaikh and colleagues point to NEO detectors in the literature that use 40 and 50 nW per channel, and they note that NEO can even run on the analog signal before conversion to numbers. Second, many published detectors average ψ over a few samples before the threshold. Shaikh’s equations do not. The device below makes that smoothing a switch.
The device uses a made-up trace with one added problem: a leftover 110 to 260 Hz hum of 20 µV that the filter did not remove. It raises the noise estimate, which hurts the plain detector. Set both multipliers, then compare.
Top: the filtered trace and its plain threshold. Bottom: the NEO output and its threshold. Green rings are caught spikes, red rings missed ones, red crosses false alarms. Move each multiplier.
Illustrative: 100 ms of made-up trace, noise 10 µV, 9 spikes of 45 to 95 µV, plus a 20 µV leftover hum at 110, 180 and 260 Hz (the hum is our addition, to show the effect). The plain noise estimate is the median one from Chapter 2 and reads about 23 µV with the hum. Dead time is 30 samples for both detectors.
With the starting settings the plain detector finds none of the nine spikes: its line sits at about −93 µV, below every one of them. The NEO detector with smoothing finds all nine. Slide k down to 3 and the plain detector wakes up, finding three. That is the pattern the literature reports: NEO is less sensitive to where you put the line.
Shaikh and colleagues compared both detectors on recordings from two monkeys with arrays in motor cortex, using a simple decoder to read out four tasks. With the best multiplier for each, NEO decoded about 5% better in monkey A and about 2% better in monkey B than the plain threshold, and NEO’s accuracy varied less from session to session. It is a modest gain, from one research group, with a small decoder. We treat it as a hint, not a rule. On a clean trace with white noise and no leftover hum, our own toy tests show the plain threshold at least as good as NEO. NEO earns its place when the trace is messy and the chip is small.
Chapter 5
Score a detector by how many spikes it finds for each false alarm it allows, and compare two detectors by a whole curve
A smoke alarm in your kitchen faces a choice. Make it jumpy and it screams at toast. Make it calm and it sleeps through a real fire. No setting is right in every house. A spike detector faces the same choice, and so we need a clear way to speak about it.
Compare the detector’s events with an answer key that lists the true spikes. Every true spike is either a hit (the detector reported it near the right time) or a miss (it did not). Every reported event that matches no true spike is a false alarm. In our devices, an event within 16 samples (about half a millisecond) of a true peak counts as a hit.
Two rates summarise the outcome. The hit rate is hits divided by true spikes. The false alarm rate is false alarms per second. The usual text-book version divides false alarms by the number of non-events. A trace has no natural count of non-events, since any instant could hold one, so a rate per second is the honest unit.
example: 43 true spikes, 36 found, 2 false alarms in 1 s → hit rate 36/43 = 84%, false alarm rate 2 per second
Now sweep the threshold. A strict line gives few hits and few false alarms. A loose line gives many of both. Plot one point for each setting, with false alarms on the horizontal axis and hits on the vertical, and join them. The result is the receiver operating characteristic, the ROC curve. (The name comes from radar, where operators had to decide when a blip was a plane.)
Reading it takes one rule. The best corner is top-left: all the hits, none of the false alarms. A curve that hugs that corner belongs to a better detector. A curve under another is worse at every setting. A single number such as 90% hits means nothing until you know how many false alarms bought it.
Why a curve and not a single score? Because the two errors cost different amounts in different systems. A cursor can tolerate a few extra events, since the decoder averages over many channels. A system that counts every event as a spike for later sorting may tolerate almost none. The curve shows all the trade-offs at once, and each user picks the point that suits the job.
The curve also turns a vague worry into a decision. Suppose your decoder can live with two false alarms per second per channel. Draw a vertical line at 2 and read the curve where it crosses. That is the hit rate you can have.
Each curve sweeps the detector’s multiplier from strict to loose on one made-up second. The dashed red line is your budget of false alarms. The dots mark the best each detector can do within it. Move the budget, then add leftover hum.
Illustrative: one made-up second at 30,000 samples per second with 43 true spikes of 45 to 95 µV and noise of 10 µV. The hum (110, 180 and 260 Hz) is our addition. The plain detector uses the median noise estimate with k from 1.5 to 8; NEO uses l from 2 to about 60 times the mean. Both use a 30-sample dead time. With no hum, expect the plain detector to win.
Start with the hum at 0 and the budget at 2. The plain detector finds almost every spike: its curve jumps to the top within a fraction of a false alarm. NEO trails it. Now raise the hum to 20 µV. The plain curve collapses, because the hum inflates its noise estimate. The smoothed NEO curve holds up and takes the lead.
The symptom: a threshold that looked perfect on one clean file floods the decoder with events on another day. The measurement: draw the ROC on data from the day you will use it, not the day you tuned it. The fix: choose the threshold from the false alarm budget, not from the best hit rate alone. Then check the choice against the real goal.
That real goal is the decoder. Shaikh and colleagues did exactly this. They swept the multipliers for both detectors and judged each by the decoding accuracy of the task, not by hits and false alarms. The best plain setting was k = 3 and the best NEO settings were l = 13 and 15. A spike detector exists to feed a decoder, so the decoder’s accuracy is the final score.
One more honest point. Real recordings rarely come with an answer key, because nobody sees every neuron. Researchers use semi-synthetic data, where real noise is mixed with spike shapes at known times, or they pair an electrode with a second recording inside a cell. Our toy traces are the extreme version of the first option.
Chapter 6
Count events in short bins on every channel, and see why a rough count is enough to read out an intended direction
A decoder is a program that guesses what a person intends, such as the direction of a cursor move. It does not want a list of event times. It wants a fixed table of numbers it can feed to a formula, every 20 milliseconds or so. So the events must become numbers.
The step is simple counting. Cut time into short, equal slices called bins. For each channel, count the events inside each bin. Stack the channels side by side. A feature is any number given to a decoder, and these counts are the features.
The shapes make it concrete. A Utah array has 96 electrodes (Even-Chen and colleagues used 96-electrode arrays). Detection turns each channel’s 30,000 samples per second into a short list of event times. Binning turns that list into one count per bin. For 96 channels the table has the shape [bins × 96], one row per bin and one column per channel. In Even-Chen et al., spike data are binned at 60 Hz, which means 60 bins each second, about 17 ms each.
This is where the earlier chapters pay off. A threshold crossing is not tied to one neuron. Everything near the electrode that fired hard enough lands in the same count. The result is called multiunit activity. Separating the cells is spike sorting, which means clustering spike shapes by neuron. Sorting is slow, needs a person to check, and drifts day to day.
Does the BCI lose much by skipping it? Even-Chen and colleagues state that the benefit of spike sorting is minimal compared with simple threshold crossing, about a 5% difference in performance, and they design around threshold-crossing events. Shaikh and colleagues cite similar earlier comparisons. The count per bin, from many cells together, carries nearly as much as the count from one cell at a time.
Many cells in motor cortex fire more when the intended movement goes in their favourite direction, called their preferred direction. This is a textbook pattern. Here we use it as a toy model to show how counts become a guess.
Take four channels with preferred directions 0° (right), 90° (up), 180° (left) and 270° (down). In one bin they count 6, 3, 1 and 2 events. Their usual count is 3. Subtract that to get the changes: +3, 0, −2, −1.
Now let each channel pull on an arrow. A positive change pulls toward its preferred direction. A negative change pushes away. Channel 1 pulls right by 3. Channel 3 pushes away from left, which is right again, by 2. Channel 4 pushes away from down, which is up, by 1. The sum is 5 to the right and 1 up. The guess is the angle of that sum, atan(1/5), about 11° above straight right.
guess = angle of Σ (count − usual count) × (arrow of preferred direction) = angle of (5, 1) = 11°
Real decoders are better than this. A Kalman filter tracks a smooth path, a generalised linear model maps counts to movement, and PCA finds the few patterns that carry the signal. But all of them eat the same table of counts.
Now run the toy with many channels. Each channel is a made-up multiunit site with 30 events per second at rest and 20 more in its preferred direction. A bin lasts 100 ms. Try fewer channels, then let the detector miss spikes or add false alarms.
Each tick around the ring is one channel at its preferred direction; longer ticks mean more events in the bin. The warm arrow is the true direction, the teal arrow is the decoder’s guess. Press New trial, then change the channels or the detector.
Toy model, not data: counts are drawn from a Poisson distribution (the standard model for random event counts) with a mean of 0.1 s × (30 + 20 cos(angle from the preferred direction)) × hit rate + 0.1 s × false alarms. The decoder is the arrow sum above. The error is the median over 300 seeded trials. Real cells are not this tidy.
With 96 channels and a perfect detector the median error is about 5 degrees. Cut the channels to 8 and it grows to about 18 degrees. Now the lesson from detection: let the detector find only 40% of the spikes and the error rises from 5 to about 8 degrees, a small price for losing most of the data. Add 20 false alarms per channel per second on top and it reaches about 13 degrees. The guess depends on the pattern across channels, not on any single count, so it bends before it breaks.
That is why a rough detector can be good enough. The decoder wants a pattern, many channels agreeing. It does not need every spike. The same fact justifies the next chapter: if the pattern survives a lossy detector, we can afford to throw away most of the data before it leaves the skull.
Chapter 7
Count the bits per second at each design choice, and see why spike detection moves onto the implant
Suppose you want a wireless BCI with no cable through the skin. A small radio inside the head must send every channel’s data out. The radio draws power, and power turns into heat in the brain. So the question is plain arithmetic: how many bits must leave the head each second?
A bit is one yes-or-no digit. Each sample is stored as a number with some count of bits, called its resolution. Even-Chen and colleagues describe a conventional research recorder that samples each electrode with 16 bits at 30,000 samples per second. Multiply: 16 × 30,000 = 480,000 bits per second, or 480 kbit/s, for one channel.
Now scale. At 96 channels, the stream is 96 × 480 kbit/s = 46 Mbit/s. At 1,000 channels it is 480 Mbit/s. For scale, the same paper cites one published wireless link that runs at 67 Mbit/s. A thousand raw channels will not fit.
The paper compares designs in its Table 1. Each step throws away something the BCI does not need.
First, keep only the band that holds spikes, and sample it just fast enough. The paper’s minimal design keeps 250 Hz to 3 kHz and samples at 9,000 per second, three times the top frequency. It also drops to 8 bits per sample. That gives 8 × 9,000 = 72 kbit/s per channel, a cut of more than six times.
Second, detect on the chip and send only the crossings. The paper’s minimal design with on-chip thresholding sends 1 kbit/s per channel. That is consistent with one yes-or-no flag for each 1 ms window, since the paper checks for crossings once per millisecond. A thousand channels then produce about 1 Mbit/s, which the paper states in section 5.1.2. The ratio to the raw stream is 480 to 1.
Third, send compressed features instead. The paper also considers sending the top 20 principal components at 8 bits, about 0.16 Mbit/s. PCA finds those components.
Radios cost energy per bit. Even-Chen and colleagues assume a transmitter that spends 8.5 pJ (picojoules) per bit. Power is energy per bit times bits per second. For 1,000 channels:
raw: 480,000,000 bit/s × 8.5 pJ = 4.1 mW crossings only: 1,000,000 bit/s × 8.5 pJ = 8.5 µW
The crossings-only radio draws about 480 times less. This is only the radio. The amplifiers and converters add their own cost, which is why the paper also relaxes the recording circuit. It reports an overall saving of about one order of magnitude against academic prototypes and about two against commercial systems.
Move the sliders and see where each design hits the link limit.
Three bars on a log scale: total bits per second out of the head for the same channels. The dashed line is one published 67 Mbit/s link. Choose a design for the sentence below, and raise the channel count.
Per-channel rates are from Even-Chen et al. 2020, Table 1: 480 kbit/s (16 bit, 30 kS/s, a conventional recorder), 72 kbit/s (8 bit, 9 kS/s, minimal design) and 1 kbit/s (minimal design with on-chip thresholding). The 8.5 pJ per bit transmitter and the 67 Mbit/s link are the numbers that paper cites. The slider runs from 16 to 4,096 channels on a log scale. Radio power only; amplifiers and converters are extra.
At 96 channels all three fit. At about 140 channels the raw stream passes the 67 Mbit/s mark. At 1,000 channels even the reduced stream sits just above it, while the crossings stay far below. Only the thresholded design keeps scaling.
Notice what the third design gives up. The raw stream lets you change your mind later: re-filter, pick a new threshold, sort the spikes. The crossings stream does not. If the threshold was wrong on the day, the data you needed was never sent. For a research system with a cable that is a bad trade. For a wireless implant with a small power budget it is often the only one that fits, and that is why a careful detector design pays off.
The same logic drives commercial implants. Neuralink’s 2019 platform paper (Musk and Neuralink, Journal of Medical Internet Research 21(10):e16194) describes a system with thousands of channels and spike detection on the chip. Those are the company’s claims, and we did not check its chip numbers here. The arithmetic above shows why any design with many channels will try to detect before it transmits.
The symptom: the decoder’s output stutters, and the logs show gaps in the data. The measurement: multiply bits, samples per second and channels, and compare with the radio’s capacity. The fix: send fewer bits. Reduce the band and the resolution first. Detect on the chip if you must. The cost of the last step is that the waveform is gone. You can no longer re-sort or re-threshold later, so some systems keep a way to send raw snippets on request.
Chapter 8
Replace the sorted median with a one-register tracker, and see how few bits the samples may use before the estimate breaks
On a laptop, the median of a second of samples takes a blink. Sort 30,000 numbers and pick the middle one. Now ask a chip the same. Each of its channels would have to store 30,000 samples of 16 bits, which is 480,000 bits, just to find one number. Multiply by a thousand channels. The memory alone would dwarf the amplifiers.
Shaikh and colleagues make this point about the plain detector: it is hard to put on the implant because a median-based threshold needs large memory and a large area to compute. So chip designers look for an estimate that updates one sample at a time.
Here is the idea. Keep one number, m, our guess for the median of |x|. Look at each new sample. If its absolute value is above m, nudge m up a little. If it is below, nudge m down a little. That is all.
Why does it find the median? The median is the value with half the samples above it and half below. If m is too low, more than half of the samples sit above it, so m is nudged up more often than down, and it climbs. If m is too high, the reverse happens. It stops drifting when the ups and downs balance, which is exactly at the median. This is a standard streaming trick, and it needs no sorting and no stored samples.
For chips we nudge by a fixed fraction, so the update is a shift and an add, which is cheap in hardware. With a step of 1/8:
above: m ← m × (1 + 1/8) below: m ← m / (1 + 1/8)
Follow four samples in µV, starting at m = 10. The sample |x| = 14 is above 10, so m becomes 11.25. Next |x| = 6 is below, so m becomes 11.25 / 1.125 = 10. Next |x| = 9 is below, so m = 8.89. Next |x| = 20 is above, so m = 8.89 × 1.125 = 10 again. The guess wobbles around 10 and follows the middle. Dividing by 0.6745 at the end turns m into the noise estimate σ̂. Even that division folds away: a threshold of k = 4 is 4 / 0.6745 = 5.93 times m, one constant.
The tracker has a step size. A big step reacts fast and wobbles a lot. A small step is calm and slow. With a step of 1/64, climbing from 5 to 10 takes about 45 upward nudges, or 1.5 ms at 30,000 samples per second. With 1/1,024 it takes about 710, or 24 ms. You choose between speed and steadiness.
The second dial is the resolution of the samples. The converter turns a voltage into a whole number of steps. With 8 bits there are 256 levels. If the full range is plus or minus 512 µV, each step is 4 µV. Noise of 10 µV then spans a few steps and the median still means something. With 6 bits the step is 16 µV, bigger than the noise itself. More than half of the noise samples round to exactly zero, the median of |x| is zero, and the tracker slides to zero. A threshold of zero fires on everything. (The lesson on quantization noise shows why the step size sets the error.) Even-Chen and colleagues keep 8 bits in their minimal design, with the amplifier gain set so the noise covers several levels.
Try both dials.
The yellow line is the chip’s running estimate of the noise, which starts at a wrong guess of 3 µV. The dashed lines are the true noise and the exact median estimate on the same quantized samples. Choose a step size, then a bit depth.
Illustrative: 200 ms of made-up trace (noise 10 µV, 40 spikes per second of 45 to 95 µV), rounded to the chosen number of bits over a full range of ±512 µV, which is our choice, not a chip’s. The tracker is the multiplicative one above. Settling means within 10% of the exact estimate and staying within 25% for 10 ms. Wobble is the half-range of the estimate over the last 100 ms.
At 8 bits and 1/64, the tracker settles in about 12 ms, then wobbles by roughly a quarter. Switch to 1/256 and the wobble drops to a few percent, but it takes about 58 ms to settle. At 6 or 4 bits the estimate collapses toward zero no matter what the step is. No clever update rule can recover a signal the converter has already rounded away.
The same one-register trick serves NEO. Its threshold needs the mean of ψ, and a running mean is the same pattern: m ← m + (ψ − m) / 2s, one shift and one add. Shaikh and colleagues cite NEO detectors in the literature at 40 and 50 nW per channel, one with an automatic threshold, and they note that the median-based threshold is the harder one to build.
The symptom: one channel suddenly reports thousands of events per second. The measurement: read its threshold register. If it has fallen close to zero, the estimate has collapsed. The fix: add gain before the converter so the noise spans more levels, use more bits, and keep a floor under the register so the threshold can never reach zero.
Chapter 9
Find the three common failures, a shared artifact, a bad channel and a drifting noise level, and the cheap fix for each
On Monday the cursor obeys. On Tuesday morning it jitters, and then drifts to the corner of the screen. Nothing changed in the code. Somewhere between the electrode and the decoder, the event counts stopped meaning what they meant yesterday. Where do you look?
Three causes explain most such mornings. Each leaves a different fingerprint in the counts.
An artifact is a signal that does not come from the neurons you want. A person clenches their jaw, a cable moves, a nearby device switches on. The disturbance reaches every electrode at once, with nearly the same shape. Because the disturbance adds noise, a threshold set earlier now sits far too low. Every channel floods.
The measurement is simple: look at many channels together. Real spikes are local. A cell fires near one electrode, not near all. An artifact is shared. If all 96 channels jump in the same 20 ms, it is not the brain.
The cure uses the same fact. A common average reference (CAR) subtracts, at each sample, the average over all channels. Whatever is shared cancels. What is local stays. Take four channels at one instant, in µV: 30, 28, 33, 29. The average is 30, so they become 0, −2, 3, −1. Now a shared artifact adds 25 to every channel: 55, 53, 58, 54. The average is 55, so they become 0, −2, 3, −1 again. The artifact is gone and the local differences stayed untouched.
x′i[n] = xi[n] − (1/N) ∑j xj[n] (channel i, sample n, N channels)
Now the opposite. An electrode loses contact, or its wire breaks. Only that channel changes, and its noise grows several times. Fixed thresholds flood it. Thresholds that follow the noise level go the other way: they rise with the noise, and the channel falls silent, because spikes no longer rise above the new line. Neither is a good outcome, and CAR cannot help, since the problem is not shared. In fact a bad channel leaks a small share of its noise into everyone else through the average.
The measurement is the noise estimate from Chapter 2. It is already computed for the threshold, so it costs nothing to log. When one channel’s estimate climbs past about twice its own baseline, flag it. The fix is not to repair the channel in software but to mask it, so the decoder stops reading garbage, and to tell the person running the system.
Noise changes over hours and days as the tissue and the electrode settle. A threshold fixed at the start of the session slowly stops fitting. The fix is to re-estimate. But the speed matters. A tracker that re-estimates every few hundred milliseconds will chase an artifact: when the noise bursts, it raises the line and loses real spikes at the moment you needed them. A slow tracker survives the burst but follows drift late. Chapter 8 showed the same trade as step size.
The device holds eight made-up channels over six windows of 0.4 s each. Window 3 carries a shared artifact. From window 5, channel 6 breaks. Red cells are wrong: a bright cell is flooded with false alarms, an outlined one has gone silent. Switch the method and watch which failures each one fixes.
Each cell is the number of events one channel reported in one window. Green is right. Red is wrong. Try the three setups in order.
Illustrative: every window is 12,000 samples (0.4 s) of made-up trace with noise of 10 µV and about 20 spikes of 45 to 95 µV. Window 3 adds the same 25 µV of random shared noise to all channels. From window 5, channel 6 gets 30 µV of extra private noise. A cell is wrong if it holds more than 8 false alarms or misses more than 40% of its spikes. The lines are 4 times the median estimate.
With fixed lines, window 3 floods all eight channels, and the bad channel floods too. After CAR the shared artifact vanishes, but channel 6 still floods. With a new line each window, channel 6 stops flooding but goes silent. No setting fixes everything. The health flag does the rest: it names channel 6, so a person or a program can mask it.
Put the steps in this order: bandpass filter (Chapter 1), common average reference, noise estimate and threshold (Chapters 2 and 3), dead time, binning (Chapter 6), and a log of each channel’s estimate for the health check. Each step feeds the next.
Chapter 10
Review the whole detector, with the numbers, the checklist, the code and the sources
An electrode gives 30,000 voltage samples per second. A bandpass filter (about 300 to 3,000 Hz) removes the slow waves. The noise level is estimated as the median of |x| divided by 0.6745, which the spikes cannot inflate. The detector reports a threshold crossing when the trace drops below −k times that level, then ignores the next 1 ms. The nonlinear energy operator, x[n]² − x[n−1]x[n+1], is an alternative score for height and speed together. An ROC curve compares detectors by hits against false alarms. Counting crossings in short bins gives the features a decoder reads, and detecting on the implant shrinks the data from 480 kbit/s to about 1 kbit/s per channel.
| Item | Value | Where it comes from |
|---|---|---|
| Recording rate, conventional | 16 bits at 30,000 samples/s = 480 kbit/s per channel | Even-Chen et al. 2020, Table 1 |
| Minimal recorder | 250 Hz to 3 kHz, 9,000 samples/s, 8 bits = 72 kbit/s | Even-Chen et al. 2020, Table 1 |
| With on-chip thresholding | 1 kbit/s per channel; 1 Mbit/s for 1,000 channels | Even-Chen et al. 2020, Table 1 and section 5.1.2 |
| Filter band for detection | 300 to 3,000 Hz | Shaikh et al. 2018, Methods |
| Noise estimate | median|x| / 0.6745 | Shaikh et al. 2018, eq. (1) |
| Threshold multiplier k | an integer, typically 2 to 5; for the plain threshold, best decoding at k = 3 in both monkeys | Shaikh et al. 2018 |
| Commercial built-in detector | −4.5 times the rms voltage, 1 ms window | Even-Chen et al. 2020, Methods 7.5 |
| NEO | ψ[n] = x[n]² − x[n−1]x[n+1]; threshold l times the mean of ψ; best l = 15 and 13 | Shaikh et al. 2018, eq. (3) and (4) |
| NEO against plain, decoding | about +5.3% (monkey A) and +2.2% (monkey B) | Shaikh et al. 2018, Results |
| Sorting against crossings | about 5% performance difference | Even-Chen et al. 2020, section 2.1 |
| Bins for decoding | 60 Hz, non-overlapping | Even-Chen et al. 2020, Methods 7.5 |
| Transmitter energy | 8.5 pJ per bit (the paper’s assumption) | Even-Chen et al. 2020, section 4.3 |
| NEO detectors in hardware | 40 nW and 50 nW per channel (reported by others) | cited in Shaikh et al. 2018 |
| Chance a noise sample is below −kσ | k = 3: 0.135%; k = 4: 0.0032%; k = 5: 0.000029% | bell-curve tail, computed |
Terms. Sample, trace, microvolt, spike, action potential, local field potential, bandpass filter, standard deviation, median, robust, MAD, threshold, threshold crossing, dead time, refractory period, NEO, hit, miss, false alarm, ROC curve, bin, feature, multiunit activity, spike sorting, bit, resolution, step size, artifact, common average reference. Each is defined where it first appears above.
pythonimport numpy as np def detect(x, fs=30000, k=4.0, dead_ms=1.0, neo=False, l=8.0, smooth=5): """x: filtered trace, float array [n_samples] in microvolts. Returns event sample numbers [n_events].""" dead = int(fs * dead_ms / 1000) # 30 samples = 1 ms if not neo: sigma = np.median(np.abs(x)) / 0.6745 # Chapter 2, robust to spikes below = x < -k * sigma # Chapter 3, bool [n_samples] cross = np.flatnonzero(below[1:] & ~below[:-1]) + 1 else: psi = np.zeros_like(x) psi[1:-1] = x[1:-1]**2 - x[:-2] * x[2:] # Chapter 4, same shape as x if smooth > 1: psi = np.convolve(psi, np.ones(smooth) / smooth, "same") above = psi > l * psi.mean() cross = np.flatnonzero(above[1:] & ~above[:-1]) + 1 events, last = [], -dead for c in cross: # dead time, Chapter 3 if c - last >= dead: events.append(c); last = c return np.array(events) def bin_counts(events, n_samples, fs=30000, hz=60): # Chapter 6: one channel -> [n_bins] edges = np.arange(0, n_samples + fs // hz, fs // hz) return np.histogram(events, edges)[0]
Stack bin_counts for 96 channels and you hold the table of Chapter 6, [n_bins × 96]. A decoder starts there.
Spike detection is the first software step after the converter. Next comes either spike sorting (cluster the spike shapes with PCA and a mixture model such as EM for Gaussian mixtures; Quiroga and colleagues used wavelets, see the wavelet lesson) or straight binning into features. Decoders then use a Kalman filter, an adaptive Kalman filter, a generalised linear model, or a hidden Markov model. To remove artifacts across channels by learning, see ICA. For the converter side, see quantization noise.
This lesson has no slides. We read these primary sources ourselves:
All traces in this lesson are made up by the page, seeded so they repeat. Where a number comes from a paper, it is labelled with that paper.
Now press Present or Teach and explain, from memory, why the median beats the spread, where 0.6745 comes from, what a dead time is for, what an ROC curve shows, and why detection moves onto the implant. Then go back to the threshold at the top and try to predict each catch before you move the slider.