---
title: EEG, EMG and processing biopotentials
heading: The amplitudes and bandwidths of the ECG, EEG and EMG, EEG rhythms, choosing sample rates and converter resolution, high-pass corners and baseline wander, notch filters, ensemble averaging of evoked potentials, and detecting QRS complexes
eyebrow: S15 · Lesson 18 · EEG, EMG and processing biopotentials
xp: 50
---

::: hook
The brain's electrical activity reaches the scalp at tens of microvolts, filtered and smeared by the skull. Muscles produce millivolt bursts with energy up to several hundred hertz. A visual or auditory stimulus evokes a brain response of a microvolt or less, completely hidden in the ongoing EEG. Each signal sets different demands on the front end and on the processing after it, and the tools come straight from C9 and S7: anti-aliasing and sampling, high-pass and notch filters, and the averaging that pulls a repeatable response out of noise.
:::

::: play
- A 2 µV evoked response buried in 20 µV of background EEG, after averaging N repetitions time-locked to the stimulus.
- The response adds coherently while the background, uncorrelated from sweep to sweep, grows only as √N, so the noise in the average falls as $1/\sqrt{N}$.
- Four times the sweeps halves the noise.
:::

```sim plot
view: random
x: v = -30..30
slider: N = 1..10000 = 1 log
sample: 2*exp(-((i - 100)/12)^2) + 20*randn(1)/sqrt(N)
trials: 4000
bins: 60
trace: 200
readout: noise after averaging, µV = 20/sqrt(N) | digits: 3
readout: peak signal-to-noise ratio = 2/(20/sqrt(N)) | digits: 3
```

::: predict
## An evoked potential of 1 µV sits in 20 µV (RMS) of background EEG. How many stimulus repetitions must be averaged to reach a signal-to-noise ratio of 5?

- [ ] 100
- [ ] 2,000
- [x] 10,000
- [ ] 400

::: reveal
Before averaging the ratio is 1/20 = 0.05. Averaging N sweeps raises it by √N, so reaching 5 needs √N = 100, or N = 10,000. At a few stimuli per second, that's about an hour, which is why evoked-potential tests use fast stimulus rates and the largest responses available.
:::
:::

::: nameit
## The signals

| Signal | Typical amplitude | Bandwidth |
|---|---|---|
| ECG | 0.5 to 4 mV | 0.05 to 150 Hz (diagnostic) |
| EEG | 5 to 300 µV | 0.5 to about 100 Hz |
| Surface EMG | 0.1 to 5 mV | about 20 to 500 Hz |
| Evoked potentials | 0.1 to 10 µV | depends on the test |

EEG rhythms are named by band: delta below 4 Hz, theta 4 to 8, alpha 8 to 13 (eyes closed, relaxed), beta 13 to 30, gamma above.

## Sampling and resolution

Sample at least twice the highest frequency kept, with an anti-aliasing filter below half the sample rate (C9): 500 samples/s is common for diagnostic ECG. Modern front ends use 24-bit delta-sigma converters, whose step over ±2.5 V is 0.3 µV, fine enough to skip most analog gain. A recorder's raw data rate is channels × sample rate × bits.

## Filters

- **High-pass** filters remove electrode offset and the slow baseline wander from breathing and movement. A first-order corner $f_c$ has time constant $1/(2\pi f_c)$ and adds phase lead $\tan^{-1}(f_c/f)$, which distorts the ST segment; diagnostic ECG therefore uses 0.05 Hz, or a linear-phase digital filter.
- **Notch** filters at 50 or 60 Hz remove residual line interference; a notch with quality factor Q removes a band about $f_0/Q$ wide, and distorts any signal energy inside it.

## Ensemble averaging

For a response that repeats identically after each stimulus, averaging N sweeps keeps the response and reduces uncorrelated noise by $\sqrt{N}$: $\text{SNR}_N = \sqrt{N}\,\text{SNR}_1$.

## QRS detection

Heart-rate monitors find each beat with algorithms like Pan and Tompkins': a 5 to 15 Hz band-pass that favors the QRS over P and T waves, a derivative for slope, squaring, a moving-window integral about 150 ms long, and an adaptive threshold.
:::

::: example
## A 64-channel EEG system samples at 1000 per second with 24-bit converters spanning ±2.5 V. What's its step size and its raw data rate?

::: solution
1. **Step size.** The ±2.5 V span is 5 V:
   $$\begin{aligned} q &= \frac{5\ \text{V}}{2^{24}} \\ &= 0.298\ \mu\text{V} \end{aligned}$$
   about a hundredth of the smallest EEG.
2. **Raw data rate.**
   $$\begin{aligned} R &= 64 \times 1000 \times 24 \\ &= 1.536\ \text{Mbit/s} \end{aligned}$$
   about 0.7 GB per hour.

**Answer:** the step is 0.298 µV, and the raw data rate is 1.536 Mbit/s, about 0.7 GB an hour.
:::
:::

::: example
## What time constant does a 0.05 Hz high-pass have, and how much phase lead does it give a 1 Hz component? How wide is a 60 Hz notch with Q = 30?

::: solution
1. **Time constant.**
   $$\begin{aligned} \tau &= \frac{1}{2\pi \times 0.05} \\ &= 3.18\ \text{s} \end{aligned}$$
2. **Phase lead at 1 Hz.**
   $$\begin{aligned} \phi &= \tan^{-1}(0.05) \\ &= 2.9° \end{aligned}$$
   small enough to keep slow ECG features intact.
3. **Notch width.**
   $$\begin{aligned} \Delta f &= \frac{60}{30} \\ &= 2\ \text{Hz} \end{aligned}$$
   removed around 60 Hz.

**Answer:** the high-pass has τ = 3.18 s and gives 2.9° of lead at 1 Hz, and the notch is about 2 Hz wide.
:::
:::

::: example
## Neural spikes are detected by a threshold at 4 standard deviations of the noise, which is flat from 0 to 5 kHz. How often does noise alone cross the threshold?

::: solution
1. **Rice's formula.** For band-limited Gaussian noise flat from 0 to B, upward crossings of a level a come at the rate
   $$\nu = \frac{B}{\sqrt3}e^{-a^2/2\sigma^2}$$
2. **At 4σ.** With B = 5 kHz, the exponent is −16/2 = −8:
   $$\begin{aligned} \nu &= \frac{5000}{\sqrt3} \times e^{-8} \\ &= 0.97\ \text{per second} \end{aligned}$$
3. **Against real spikes.** A real neuron firing at 10 spikes a second would see about one false spike in ten.
4. **At 5σ.** False crossings fall to 0.011 per second, but small spikes are missed.
5. **The trade-off.** The threshold trades misses against false alarms, as in any detector.

**Answer:** noise alone crosses a 4σ threshold about 0.97 times a second.
:::
:::

::: check
given: s = 0.5 | 1 | 2; n = 10 | 20 | 50; t = 3 | 5 | 10
answer: (t*n/s)^2
tolerance: 0.5%

## A {s} µV evoked response sits in {n} µV RMS of background. How many sweeps must be averaged for a signal-to-noise ratio of {t}?

::: right
Right. $N = (\text{SNR}_\text{target}/\text{SNR}_1)^2$ = {answer}.
:::

::: misses
- [t*n/s] The improvement is √N, so square the ratio.
- [any] Not yet. Averaging improves SNR by √N.
:::
:::

::: check
given: R = 2 | 5; B = 16 | 24
answer: R/2^B*1e6
unit: µV
tolerance: 0.5%

## A {B}-bit converter spans {R} V. What's its step size?

::: right
Right. $R/2^B$ = {answer} µV.
:::

::: misses
- [any] Not yet. Full scale over the number of codes.
:::
:::

::: check
given: c = 8 | 64 | 256; f = 250 | 1000 | 4000; b = 16 | 24
answer: c*f*b/1e6
unit: Mb/s
tolerance: 0.5%

## A recorder has {c} channels sampled at {f} per second with {b}-bit samples. What's its raw data rate?

::: right
Right. Channels × rate × bits = {answer} Mbit/s.
:::

::: misses
- [c*f*b/8/1e6] That divides by 8, which gives megabytes; the question asks for megabits.
- [any] Not yet. Multiply them.
:::
:::

::: check
given: f = 0.05 | 0.5 | 1
answer: 1/(2*pi*f)
unit: s
tolerance: 0.5%

## What's the time constant of a first-order high-pass filter with a {f} Hz corner?

::: right
Right. $1/(2\pi f_c)$ = {answer} s.
:::

::: misses
- [1/f] Divide by 2π as well.
- [any] Not yet. τ = 1/ω_c.
:::
:::

::: check
given: c = 0.05 | 0.5; f = 1 | 2 | 5
answer: atan(c/f)*180/pi
unit: °
tolerance: 0.5%

## A first-order high-pass has its corner at {c} Hz. How much phase lead does it give a {f} Hz component?

::: right
Right. $\tan^{-1}(f_c/f)$ = {answer}°.
:::

::: misses
- [atan(f/c)*180/pi] The lead shrinks as f rises above the corner: use $f_c/f$.
- [any] Not yet. High-pass phase.
:::
:::

::: check
given: F = 50 | 60; Q = 10 | 30 | 100
answer: F/Q
unit: Hz
tolerance: 0.5%

## About how wide a band does a {F} Hz notch with Q = {Q} remove?

::: right
Right. $f_0/Q$ = {answer} Hz.
:::

::: misses
- [any] Not yet. Bandwidth is center frequency over Q.
:::
:::

::: check
kind: parts
given: B = 3000 | 5000 | 10000; k = 3 | 4 | 5

## A spike detector's noise is Gaussian and flat from 0 to {B} Hz; its threshold sits at {k} standard deviations.

::: part
answer: B/sqrt(3)*exp(-k^2/2)
unit: Hz
tolerance: 0.5%

### How often does noise alone cross the threshold upward?

::: misses
- [2*B/sqrt(3)*exp(-k^2/2)] That counts crossings both ways; count upward crossings only.
- [any] Rice: $\frac B{\sqrt3}e^{-k^2/2}$.
:::
:::

::: part
answer: 2*B/sqrt(3)
unit: Hz
tolerance: 0.5%

### How often does the noise cross zero?

::: misses
- [any] $2B/\sqrt3$ for a flat band from 0 to B.
:::
:::
:::

::: check
kind: derivation
vars: s = 0.1..20, n = 1..100, N = 1..10000, t = 1..20

## An evoked response of s µV sits in n µV RMS of background, uncorrelated from sweep to sweep.

::: step
answer: s*sqrt(N)/n

### What signal-to-noise ratio do N averaged sweeps give?
:::

::: step
answer: (t*n/s)^2

### How many sweeps reach a ratio of t?
:::

::: right
Right. Averaging gains as the square root of the number of sweeps, so each doubling of the ratio costs four times the recording time.
:::
:::

::: explain
## Why does averaging many stimulus-locked sweeps reveal an evoked potential that's invisible in any single sweep?

::: model
An evoked potential has the same shape and timing after every stimulus, while the background EEG and other noise are unrelated to the stimulus and differ from sweep to sweep. When N sweeps aligned to the stimulus are added, the evoked response adds coherently and grows N times, but the noise adds like a random walk and grows only √N times, as its variance adds. Dividing by N, the response keeps its size while the noise shrinks by √N, so the signal-to-noise ratio improves by √N. The method fails if the response varies in latency from sweep to sweep, which smears it, or if the noise is itself locked to the stimulus, like a stimulus artifact.
:::
:::

::: practice

::: check
given: s = 0.1 | 5; n = 5 | 100; t = 1 | 20
answer: (t*n/s)^2
tolerance: 0.5%

## Sweeps for SNR {t}: {s} µV in {n} µV?

::: misses
- [any] $(\text{target}\times n/s)^2$.
:::
:::

::: check
given: c = 1 | 512; f = 100 | 30000; b = 12 | 32
answer: c*f*b/1e6
unit: Mb/s
tolerance: 0.5%

## Data rate: {c} channels, {f} /s, {b} bits?

::: misses
- [any] Product.
:::
:::

::: check
given: c = 0.01 | 2; f = 0.1 | 20
answer: atan(c/f)*180/pi
unit: °
tolerance: 0.5%

## High-pass lead: corner {c} Hz at {f} Hz?

::: misses
- [any] $\tan^{-1}(f_c/f)$.
:::
:::
:::
