Skip to main content

Module filters

Module filters 

Source
Expand description

Noise filters applied to exported data (never to the archive).

The same pipeline, with the same arithmetic, runs in the webapp’s live plot (webapp/src/lib/filters.ts); the shared vectors in testdata/filter-vectors.json check that both give the same output. Per sample, in order:

  1. Despike (raw volts): grey opening, a running minimum then a running maximum over w = ceil(2.5 ms × fs) samples. It removes upward spikes narrower than w and keeps wider pulses and anything downward. Output j needs input j + w - 1.
  2. and 3. Remove the 10 Hz and 11.906 Hz square waves (raw volts): subtract running templates of one period in phase bins, indexed by the sample count since the segment started (both sources run on the LabJack clock). Each bin keeps a running mean (cumulative at first, then exponential with a 30 s memory); the template is the bin mean minus the running mean of all samples. Templates are read before the sample updates them, and each learns from the sample minus the other’s current template.
  3. Calibration.
  4. High-pass, then low-pass: 2nd-order Butterworth biquads. The live plot runs them causally; exports run them forward and backward (ExportFilter).

The channel settings come from the export request (the webapp copies them from the channel’s sensor_settings.filters entry); see docs/src/reference/kv-config.md.

Structs§

Biquad 🔒
One biquad, transposed direct form II. The first finite sample after a reset sets the steady state for that value; NaN in gives NaN out and keeps the state.
BiquadCoefficients 🔒
Normalised biquad coefficients (a0 = 1).
ChannelFilterSettings
Filters of one channel, cleaned: true switches and positive cutoffs only.
Despiker 🔒
Streaming grey opening over w samples: output j is the largest of the minima of the w-sample windows containing j, windows cut at the segment’s ends and NaN left out. Emitted once input j + w - 1 has arrived, or by Self::flush.
Entry 🔒
One queued sample of ExportFilter.
ExportFilter
Rows of one channel, in time order, filtered with the zero-phase pipeline.
FilterPlan
What runs for one channel at one sample rate; from plan_filters.
FilteredRow
A filtered row handed back by ExportFilter.
LinearFilterChain
High-pass then low-pass on calibrated values.
PeriodicTemplate 🔒
Running template in phase bins; see the module documentation. A bin with no sample yet uses the mean of its group of group bins (the coarse bin).
RawFilterChain
Despike and template stages, on raw volts. Appends outputs in input order: none for the first despike_window - 1 inputs of a segment, then one per input; Self::flush appends the rest.
Segment 🔒
Filter state of one contiguous segment.

Constants§

DESPIKE_WINDOW_MS
Length of the despike window, ms.
ELEVEN_NINE_HZ
Frequency of the second square wave, Hz, measured on MU1 and MU2 (lines at 11.906, 35.72 and 59.53 Hz). It also runs on the LabJack clock.
FRACTIONAL_BINS_PER_SAMPLE
Phase bins per sample of period in the fractional template.
MAX_CUTOFF_FRACTION
Highest usable cutoff, as a fraction of the sample rate.
MAX_FILLED_GAP_SAMPLES
Longest run of missing samples filtered through as NaN. A longer gap ends the segment and every filter starts over.
MIN_FRACTIONAL_BINS
Fewest phase bins of the fractional (11.9 Hz) template.
MIN_TEMPLATE_PERIOD_SAMPLES
Fewest samples per period for a template.
PERIOD_TOLERANCE_SAMPLES
How far fs / 10 may be from a whole number of samples.
PROBE_ROWS 🔒
Rows used to estimate the sample interval of a segment.
TEMPLATE_MEMORY_S
Memory of the template running means, seconds.
TEN_HZ
Frequency of the first square wave, Hz. Its period must be a whole number of samples.

Functions§

butterworth 🔒
2nd-order Butterworth by the bilinear transform with prewarping (the audio-EQ cookbook with Q = 1/√2).
cutoff 🔒
Reads a cutoff: a finite number above 0, or a string holding one.
plan_filters
Works out which filters run at a sample rate; filters that cannot run (too low a rate, a 10 Hz period that is not a whole number of samples, a cutoff at or above 0.45 × the rate) are left off. Same rules as planFilters in the webapp.
settle_seconds
Samples the backward pass needs past a sample for the result to have settled: twenty time constants of the lowest cutoff (a 2nd-order Butterworth pole decays with time constant √2 / (2π fc)), at least one second and at most two minutes.