Noise Covariance

Initialize NeuroAnalyzer
using NeuroAnalyzer
using LinearAlgebra

Minimum norm estimates and beamformers weight the channels by the noise covariance: the covariance of the signals without the activity of interest, estimated from the baseline of the epochs (e.g. the pre-stimulus interval) or from an empty-room MEG recording.

Estimating the noise covariance

noise_cov() concatenates the samples of all epochs in the given time window and returns a NOISECOV object (export_cov() saves it as an MNE-Python -cov.fif file, import_cov() loads one):

eeg = import_edf("files/eeg/eeg-test-edf.edf")
e2 = epoch(eeg; ep_len = 2)
ch = get_channel(e2; type = "eeg")
nc = noise_cov(e2; ch = ch, seg = (0, 0.5))
NeuroAnalyzer NOISECOV
  channels: 19 (eeg 19)
  degrees of freedom: 77657
  method: empirical

Shrinkage and regularization

With few samples or many channels, the empirical covariance is poorly conditioned; Ledoit-Wolf shrinkage pulls it towards a scaled identity matrix with the optimal amount of shrinkage (small here, with many samples), and regularization (reg) adds a fraction of the mean variance of each channel type to the diagonal:

nc_lw = noise_cov(e2; ch = ch, seg = (0, 0.5), method = :ledoit_wolf)
nc_reg = noise_cov(e2; ch = ch, seg = (0, 0.5), reg = 0.1)
for (name, c) in (("empirical", nc),
                  ("Ledoit-Wolf", nc_lw),
                  ("reg = 0.1", nc_reg))
    println(rpad(name, 12), "condition number: ",
            round(cond(c.cov); sigdigits = 3))
end
empirical   condition number: 194000.0
Ledoit-Wolf condition number: 161000.0
reg = 0.1   condition number: 170.0

The noise covariance whitens the data and the lead fields of channels of different types (MEG, EEG) in dipole_fit(), see the Dipole Fitting tutorial.