Simple descriptive statistics of the signal amplitude are a quick way to check the quality of a recording: channels or epochs with unusually large variance, a heavy-tailed or skewed amplitude distribution are usually contaminated by artifacts.
The example recording is cut into 100 epochs of 10 seconds; only the 19 EEG channels are kept, so that the statistics are not mixed with the EOG and ECG channels (which have different amplitudes and units).
Moments of the Amplitude Distribution
The shape of the distribution of signal amplitudes is described by its moments:
mean: the DC offset of the signal; close to 0 μV for a high-pass filtered EEG
variance (or standard deviation): the power of the signal; increased by movement, muscle and electrode artifacts
skewness: the asymmetry of the distribution; 0 for a symmetric distribution, positive or negative when large deflections occur mostly in one direction (e.g. eye blinks in the frontal channels)
kurtosis: the heaviness of the tails; NeuroAnalyzer reports the excess kurtosis, which is 0 for a Gaussian distribution; large values indicate rare, large transients (spikes, blinks, electrode pops)
Ongoing EEG has an approximately Gaussian amplitude distribution, so large skewness or kurtosis is a simple marker of artifacts.
Epoch Statistics
epoch_stats() calculates the statistics of each epoch, pooled over all channels:
Field
Statistic
e_mean
mean
e_median
median
e_std
standard deviation
e_var
variance
e_kurt
excess kurtosis
e_skew
skewness
e_mean_diff
mean of the sample-to-sample differences
e_median_diff
median of the sample-to-sample differences
e_max_dif
peak-to-peak amplitude (maximum − minimum)
e_dev_mean
deviation of the epoch mean from the mean of all epochs
The result is a named tuple of vectors (one value per epoch), which converts directly to a data frame:
es =epoch_stats(e10)first(DataFrame(es), 5)
5×10 DataFrame
Row
e_mean
e_median
e_std
e_var
e_kurt
e_skew
e_mean_diff
e_median_diff
e_max_dif
e_dev_mean
Float64
Float64
Float64
Float64
Float64
Float64
Float64
Float64
Float64
Float64
1
0.262673
0.156511
3.72436
13.8708
1.83299
0.235342
-0.000137904
-0.00907629
39.7908
0.194048
2
0.217593
0.172844
3.55527
12.6399
1.36136
0.125725
-0.00118306
0.0130693
41.1441
0.148968
3
0.224465
0.179478
3.63923
13.244
1.64926
0.14501
0.0010521
-0.00910187
37.7936
0.155841
4
0.236562
0.201819
3.9098
15.2865
1.48063
0.145955
0.000348184
0.0142653
40.9086
0.167938
5
0.135671
0.159475
3.45228
11.9183
1.45295
-0.0134948
-0.000170523
-0.00644083
34.5689
0.067046
Plotting a statistic across epochs shows segments of the recording that differ from the rest:
fig, ax, _ =scatterlines(1:nepochs(e10), es.e_std; color =:black, markersize =6, figure = (size = fig_size,), axis = (ax_style..., xlabel ="Epoch", ylabel ="SD [μV]", title ="Standard deviation of epochs"))fig
The standard deviation is increased in epochs 6–12 and 27–31. Epochs that differ from the median by more than 3 robust standard deviations (median absolute deviation × 1.4826) are candidates for rejection:
m =median(es.e_std)z = (es.e_std .- m) ./ (1.4826*median(abs.(es.e_std .- m)))println("Outlying epochs: ", findall(>(3), z))
Tip: see the bad channels and epochs tutorial for methods designed for detecting and rejecting bad epochs.
Channel Statistics
channel_stats() calculates the same statistics separately for each channel in each epoch; the fields (c_mean, c_std, c_kurt, …) are matrices of shape (channels, epochs). c_dev_mean is the deviation of the channel mean from the mean of all channels in the same epoch.
Epoch 31 contains a transient visible in most channels, while F3 and F7 show frequent transients in the second half of the recording (e.g. eye movements or a loose electrode).
Signal-to-Noise Ratio
snr() estimates the signal-to-noise ratio (SNR) of each frequency from an epoched signal. For each channel and frequency, the amplitude spectrum is calculated in each epoch, and the SNR describes how consistent the amplitude is across epochs:
type = :mean: mean amplitude divided by its standard deviation across epochs
type = :rms (default): range of amplitudes (maximum − minimum) divided by their RMS across epochs
At least 2 epochs are required. The result is a named tuple: sn (channels × frequencies) and f (frequencies in Hz).
For random noise, the spectral amplitude has a Rayleigh distribution, for which the mean-based SNR is about 1.91; higher values indicate activity at that frequency that is stable across epochs:
sn =snr(e10; ch ="eeg",type=:mean)fig =Figure(size = fig_size)ax =Axis(fig[1, 1]; ax_style..., xlabel ="Frequency [Hz]", ylabel ="SNR", title ="Mean-based SNR")for ch in ["Fp1", "C3", "O1", "O2"] ch_idx =get_channel(e10; ch = ch)[1]lines!(ax, sn.f, sn.sn[ch_idx, :]; label = ch)end# SNR of random noise (Rayleigh-distributed amplitude)hlines!(ax, sqrt(pi/ (4-pi)); color =:black, linestyle =:dash)xlims!(ax, 0, 40)axislegend(ax)fig
In resting-state EEG the SNR stays close to the noise level at all frequencies: the amplitude of ongoing activity fluctuates between epochs much like random noise. Values below the noise level (here below 2 Hz and around 12–22 Hz) indicate an amplitude that varies more than noise would, e.g. bursts of activity or artifacts in some epochs. The SNR is most informative for responses with a stable amplitude across epochs, e.g. steady-state evoked potentials (SSVEP, ASSR), where the stimulation frequency and its harmonics stand out clearly above the noise level.