Signal Statistics

Initialize NeuroAnalyzer
using NeuroAnalyzer
using CairoMakie
CairoMakie.activate!(type = "png")
# Quarto sets a default figure size (672 × 480 px), which overrides the sizes of
# NeuroAnalyzer figures: remove it
delete!(CairoMakie.Makie.CURRENT_DEFAULT_THEME, :resolution)
using DataFrames
using Statistics
NeuroAnalyzer.progress_bar = false

# Makie figure and axis settings matching NeuroAnalyzer plots
fig_size = (900, 450)
ax_style = (titlesize = 18,
            xlabelsize = 18,
            ylabelsize = 18,
            xticklabelsize = 12,
            yticklabelsize = 12,
            xticks = LinearTicks(10),
            xminorticksvisible = true,
            xminorticks = IntervalsBetween(10),
            xautolimitmargin = (0, 0),
            yautolimitmargin = (0.1, 0.1))

# EEG channels only, 10-second epochs
eeg = load_neuro("files/eeg/eeg.hdf")
eeg = keep_channel(eeg; ch = "eeg")
e10 = epoch(eeg; ep_len = 10)

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:

  1. mean: the DC offset of the signal; close to 0 μV for a high-pass filtered EEG
  2. variance (or standard deviation): the power of the signal; increased by movement, muscle and electrode artifacts
  3. 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)
  4. 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))
Outlying epochs: [6, 7, 8, 10, 11, 12, 27, 29, 31]

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.

Statistics of all channels in the first epoch:

cs = channel_stats(e10)
DataFrame(channel = labels(e10),
          mean = cs.c_mean[:, 1],
          sd = cs.c_std[:, 1],
          skewness = cs.c_skew[:, 1],
          kurtosis = cs.c_kurt[:, 1],
          range = cs.c_max_dif[:, 1])
19×6 DataFrame
Row channel mean sd skewness kurtosis range
String Float64 Float64 Float64 Float64 Float64
1 Fp1 0.252018 2.34462 0.341966 1.52483 22.2164
2 Fp2 0.35834 1.88546 0.151465 0.393726 16.8472
3 F3 0.294785 2.3786 0.286318 1.05704 21.3573
4 F4 0.266678 1.57267 -0.122827 0.529581 14.5587
5 C3 0.310326 3.68728 0.309647 0.491415 27.9881
6 C4 0.315275 2.93509 0.28651 0.838936 23.5596
7 P3 0.258708 4.76101 0.199586 0.495145 36.4612
8 P4 0.263771 4.7574 0.0956344 0.65469 38.4075
9 O1 0.260214 4.89378 0.243834 0.551801 39.2159
10 O2 0.247429 5.31115 0.150588 0.265933 38.1118
11 F7 0.277641 2.61407 0.428877 1.16687 23.0023
12 F8 0.268876 3.03235 0.124856 0.429851 22.6833
13 T3 0.329308 3.95585 0.207514 0.677213 31.5536
14 T4 0.0615553 3.9455 0.173532 0.764194 31.9624
15 T5 0.311383 4.51036 0.124796 0.834244 37.1932
16 T6 0.303844 4.68208 0.111395 0.664882 37.1335
17 Fz -0.00986916 1.61091 0.220468 0.0723712 11.7297
18 Cz 0.338131 3.54372 0.461558 0.956486 26.8953
19 Pz 0.282365 4.68875 0.316673 0.740857 33.6324

The kurtosis of all channels and epochs shown as a heatmap makes the artifacts easy to locate (values above 5 are shown in red):

ch_n = nchannels(e10)
fig = Figure(size = fig_size)
ax = Axis(fig[1, 1];
          ax_style...,
          xlabel = "Epoch",
          title = "Excess kurtosis",
          yticks = (1:ch_n, labels(e10)),
          yreversed = true)
hm = heatmap!(ax,
              1:nepochs(e10),
              1:ch_n,
              cs.c_kurt';
              colorrange = (0, 5),
              highclip = :red)
Colorbar(fig[1, 2], hm)
fig

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.