Referencing
Common average and median reference
Section titled “Common average and median reference”At every sample, subtract the mean (CAR) or the median (CMR) over channels. The median survives a bad channel, where the mean would spread it to all the others.
y = dspk.ops.spatial.cmr(x) # or carpipeline.add(PipelineStage::common_median_reference()); // or common_average_reference(){"op": "cmr"}Reference → CMR or CAR.
Surface Laplacian
Section titled “Surface Laplacian”Each electrode minus the mean of its neighbours, on a grid. It removes what spreads across the whole
array and keeps what is local: useful for HD-EMG and ECoG. Inside the grid a channel has 4 neighbours,
on an edge 3, and in a corner 2. Channels are row-major: channel row * cols + col.
lap = dspk.ops.spatial.Laplacian.from_grid(8, 8)y = lap.apply(x)pipeline.add(PipelineStage::SurfaceLaplacian(SurfaceLaplacian::from_grid_2d(8, 8)));{"op": "laplacian", "grid": [8, 8]}Reference → Laplacian, with presets for 4×8, 8×4, 8×8 and 16×4 grids.
A grid of another size than the chain’s channels is refused, with the reason. Python also has a
distance-weighted version from electrode positions, Laplacian.from_positions.
Whitening
Section titled “Whitening”Decorrelates the channels, as sorters do before matching: the result has uncorrelated channels of unit variance. It is a matrix fitted on the signal.
w = dspk.ops.spatial.Whitening.fit_zca(x)y = w.apply(x)let white = SpatialWhitening::fit_zca::<f32>(&client, &data, channels, samples, epsilon);pipeline.add(PipelineStage::SpatialWhitening(white));The fit runs before the pipeline, on the signal of the nodes before it: fit_s seconds, in chunks
pieces spread over the recording.
{"op": "whiten"}Reference → Whiten. The fit runs in the background, and the raw signal shows until it’s done.
Python also has Whitening.fit_local_knn, which whitens each channel over its nearest channels, as
Kilosort4 does.
Worked examples: Referencing, Spatial.