Class Filter

Dynamic digital filter block with real-time parameter adjustment.

JSON configuration

Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.

JSON configuration:

{
  "LP50": {
    "Type": "FilterBlock",
    "Inputs": [ "EMG" ],
    "Params": [ "Lowpass", "IIR", 50, 150, 4, 64 ],
    "Path": "C:/Data/session1"
  }
}

Params:

IndexDescription
0FilterType (string) — Lowpass, Highpass, Bandpass, Bandstop.
1Implementation (string, default "IIR") — FIR or IIR.
2CutoffLow (double, default 50) — lower cutoff frequency in Hz.
3CutoffHigh (double, default 150) — upper cutoff for Bandpass/Bandstop.
4Order (int, default 4) — IIR filter order.
5FirTaps (int, default 64) — FIR filter length.

How Type, Inputs and Params work · Block catalogue and parameter reference

Inheritance
Filter
Namespace: MOSAIC.Models.SignalProcessing
Assembly: MOSAIC.dll
Syntax
public sealed class Filter : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks

Overview: Applies a per-channel IIR or FIR filter to incoming vectors. Filter parameters (type, cutoff, order) can be changed at runtime — the internal filter bank is automatically rebuilt on the next sample after any parameter change.

Inputs: Accepts MathNet.Numerics.LinearAlgebra.Vector<T> of double, double[], or scalar double. Each channel is filtered independently with its own filter instance.

Filter types: Lowpass, Highpass, Bandpass, Bandstop. Implementations: IIR (Butterworth, configurable order) or FIR (configurable tap count).

Bypass: When IsEnabled is false, input data is forwarded unchanged (passthrough mode).

Sample rate: Determined from DesiredRate (typically inherited from the upstream block via rate propagation). Falls back to 1000 Hz if unset.

CSV logging: When a Path is specified, Dumper automatically logs every published value via Publish(object).

Constructors

Filter(string, double, FilterType, FilterImplementation, double, double, int, int)

Initializes a new Filter block.

Declaration
public Filter(string name, double desiredRate = 0, FilterType filterType = FilterType.Lowpass, FilterImplementation implementation = FilterImplementation.IIR, double cutoffLow = 50, double cutoffHigh = 150, int order = 4, int firTaps = 64)
Parameters
Type Name Description
string name

Display name.

double desiredRate

Sample rate in Hz (typically inherited from upstream).

FilterType filterType

Initial filter type.

FilterImplementation implementation

Initial implementation (FIR or IIR).

double cutoffLow

Lower cutoff frequency in Hz.

double cutoffHigh

Upper cutoff frequency in Hz.

int order

IIR filter order.

int firTaps

FIR tap count.

Fields

_channelCount

Number of channels (set from first input vector dimension).

Declaration
private int _channelCount
Field Value
Type Description
int

_cutoffHigh

Upper cutoff frequency in Hz (used for Bandpass/Bandstop).

Declaration
private double _cutoffHigh
Field Value
Type Description
double

_cutoffLow

Lower cutoff frequency in Hz.

Declaration
private double _cutoffLow
Field Value
Type Description
double

_filterLock

Lock guarding _filters for concurrent parameter changes.

Declaration
private readonly object _filterLock
Field Value
Type Description
object

_filterType

Filter type (Lowpass, Highpass, Bandpass, Bandstop).

Declaration
private FilterType _filterType
Field Value
Type Description
FilterType

_filters

Per-channel filter instances, one per input channel.

Declaration
private IOnlineFilter[]? _filters
Field Value
Type Description
IOnlineFilter[]

_firTaps

FIR filter tap count.

Declaration
private int _firTaps
Field Value
Type Description
int

_implementation

Filter implementation (FIR or IIR).

Declaration
private FilterImplementation _implementation
Field Value
Type Description
FilterImplementation

_isEnabled

Whether filtering is active. When false, data passes through unchanged.

Declaration
[ObservableProperty]
private bool _isEnabled
Field Value
Type Description
bool

_needsRebuild

Flag indicating filters need to be rebuilt before the next sample.

Declaration
private bool _needsRebuild
Field Value
Type Description
bool

_order

IIR filter order (Butterworth).

Declaration
private int _order
Field Value
Type Description
int

_samplesProcessed

Total samples processed since creation.

Declaration
[ObservableProperty]
private long _samplesProcessed
Field Value
Type Description
long

Properties

CutoffHigh

Declaration
public double CutoffHigh { get; set; }
Property Value
Type Description
double

CutoffLow

Declaration
public double CutoffLow { get; set; }
Property Value
Type Description
double

DumpFilePrefix

Recordings from this block are named <block>_filtered.csv.

Declaration
protected override string DumpFilePrefix { get; }
Property Value
Type Description
string
Overrides
BaseBlock.DumpFilePrefix

FilterDescription

Human-readable filter description for UI display.

Declaration
public string FilterDescription { get; }
Property Value
Type Description
string

FilterType

Declaration
public FilterType FilterType { get; set; }
Property Value
Type Description
FilterType

FirTaps

Declaration
public int FirTaps { get; set; }
Property Value
Type Description
int

Implementation

Declaration
public FilterImplementation Implementation { get; set; }
Property Value
Type Description
FilterImplementation

IsEnabled

Whether filtering is active. When false, data passes through unchanged.

Declaration
[ExcludeFromCodeCoverage]
public bool IsEnabled { get; set; }
Property Value
Type Description
bool

JsonTypeName

Block type identifier for JSON serialization. Override for a custom name.

Declaration
protected override string JsonTypeName { get; }
Property Value
Type Description
string
Overrides
BaseBlock.JsonTypeName

Order

Declaration
public int Order { get; set; }
Property Value
Type Description
int

SampleRate

Sample rate for filter design. Inherited from DesiredRate; falls back to 1000 Hz if unset.

Declaration
public double SampleRate { get; }
Property Value
Type Description
double

SamplesProcessed

Total samples processed since creation.

Declaration
[ExcludeFromCodeCoverage]
public long SamplesProcessed { get; set; }
Property Value
Type Description
long

Visualization

This block's visualization, or null when it has none.

Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
Type Description
BlockVisualization
Overrides
BaseBlock.Visualization
Remarks

Blocks own their scope as a public Viz property rather than inheriting one, so the base class has no way to reach it. Overriding this is what lets the rate below be wired up automatically. With correct publication metadata, the bundle derives the time axis for one vector per publication or fixed-size contiguous sample packets from the feed shape.

When a plot needs a rate that publication frequency and feed shape cannot express (for example, variable-sized packets or overlapping windows), declare it with Viz.UpdateSignalRate. An explicit declaration wins over the derived rate. A block with several bundles must configure each one; this hook exposes only one bundle.

Viz

Declaration
public BlockVisualization Viz { get; }
Property Value
Type Description
BlockVisualization

Methods

ConfigureInput(IServiceProvider, JsonModel)

Creates a Filter from a JSON pipeline definition.

Declaration
public static Filter ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
Type Name Description
IServiceProvider sp

Service provider for dependency injection.

JsonModel m

JSON model. See the class-level example for Params layout.

Returns
Type Description
Filter

A configured Filter instance.

CreateFilter()

Creates a single filter instance based on current parameters.

Declaration
private IOnlineFilter CreateFilter()
Returns
Type Description
IOnlineFilter

CreateFirFilter()

Creates a FIR filter based on current type and parameters.

Declaration
private IOnlineFilter CreateFirFilter()
Returns
Type Description
IOnlineFilter

CreateIirFilter()

Creates an IIR (Butterworth) filter based on current type and parameters.

Declaration
private IOnlineFilter CreateIirFilter()
Returns
Type Description
IOnlineFilter

GetFilterDescription()

Builds the human-readable filter description string.

Declaration
private string GetFilterDescription()
Returns
Type Description
string

GetJsonParams()

Block-specific parameters for JSON. Override to provide custom parameters.

Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
Type Description
IReadOnlyList<object>

The values for Params, in the order this block's ConfigureInput reads them, or null when the block takes no parameters.

Overrides
BaseBlock.GetJsonParams()

InvalidateFilters()

Forces a filter rebuild on the next sample.

Declaration
public void InvalidateFilters()

OnDesiredRateChanged(double, double)

Rebuilds filters when the sample rate changes.

Declaration
protected override void OnDesiredRateChanged(double oldRate, double newRate)
Parameters
Type Name Description
double oldRate
double newRate
Overrides
BaseBlock.OnDesiredRateChanged(double, double)

OnIsEnabledChanged(bool)

Executes the logic for when IsEnabled just changed.

Declaration
private void OnIsEnabledChanged(bool value)
Parameters
Type Name Description
bool value

The new property value that was set.

Remarks

This method is invoked right after the value of IsEnabled is changed.

OnIsEnabledChanged(bool, bool)

Executes the logic for when IsEnabled just changed.

Declaration
private void OnIsEnabledChanged(bool oldValue, bool newValue)
Parameters
Type Name Description
bool oldValue

The previous property value that was replaced.

bool newValue

The new property value that was set.

Remarks

This method is invoked right after the value of IsEnabled is changed.

OnIsEnabledChanging(bool)

Executes the logic for when IsEnabled is changing.

Declaration
private void OnIsEnabledChanging(bool value)
Parameters
Type Name Description
bool value

The new property value being set.

Remarks

This method is invoked right before the value of IsEnabled is changed.

OnIsEnabledChanging(bool, bool)

Executes the logic for when IsEnabled is changing.

Declaration
private void OnIsEnabledChanging(bool oldValue, bool newValue)
Parameters
Type Name Description
bool oldValue

The previous property value that is being replaced.

bool newValue

The new property value being set.

Remarks

This method is invoked right before the value of IsEnabled is changed.

OnReceive(object, object)

Processes incoming data. When disabled, passes through unchanged.

Declaration
protected override void OnReceive(object sender, object data)
Parameters
Type Name Description
object sender

The upstream block.

object data

A MathNet.Numerics.LinearAlgebra.Vector<T>, double[], or scalar double.

Overrides
BaseBlock.OnReceive(object, object)

OnSamplesProcessedChanged(long)

Executes the logic for when SamplesProcessed just changed.

Declaration
private void OnSamplesProcessedChanged(long value)
Parameters
Type Name Description
long value

The new property value that was set.

Remarks

This method is invoked right after the value of SamplesProcessed is changed.

OnSamplesProcessedChanged(long, long)

Executes the logic for when SamplesProcessed just changed.

Declaration
private void OnSamplesProcessedChanged(long oldValue, long newValue)
Parameters
Type Name Description
long oldValue

The previous property value that was replaced.

long newValue

The new property value that was set.

Remarks

This method is invoked right after the value of SamplesProcessed is changed.

OnSamplesProcessedChanging(long)

Executes the logic for when SamplesProcessed is changing.

Declaration
private void OnSamplesProcessedChanging(long value)
Parameters
Type Name Description
long value

The new property value being set.

Remarks

This method is invoked right before the value of SamplesProcessed is changed.

OnSamplesProcessedChanging(long, long)

Executes the logic for when SamplesProcessed is changing.

Declaration
private void OnSamplesProcessedChanging(long oldValue, long newValue)
Parameters
Type Name Description
long oldValue

The previous property value that is being replaced.

long newValue

The new property value being set.

Remarks

This method is invoked right before the value of SamplesProcessed is changed.

OnSignalRateChanged(double)

Called when SignalRate changes. Override to rebuild DSP coefficients.

Declaration
protected override void OnSignalRateChanged(double newRate)
Parameters
Type Name Description
double newRate

The new true sample rate in Hz.

Overrides
BaseBlock.OnSignalRateChanged(double)

ProcessMatrix(Matrix<double>)

Filters a batch of timesteps packed as a [timesteps × channels] matrix. Each row is one timestep; rows are fed through the per-channel filters in order so state carries correctly across the batch.

Declaration
private Matrix<double> ProcessMatrix(Matrix<double> input)
Parameters
Type Name Description
Matrix<double> input
Returns
Type Description
Matrix<double>

ProcessSingle(double, int)

Filters a single scalar value on the specified channel.

Declaration
private double ProcessSingle(double input, int channel)
Parameters
Type Name Description
double input
int channel
Returns
Type Description
double

ProcessVector(Vector<double>)

Filters a multi-channel vector, one filter per channel.

Declaration
private Vector<double> ProcessVector(Vector<double> input)
Parameters
Type Name Description
Vector<double> input
Returns
Type Description
Vector<double>

RebuildFilters(int)

Rebuilds the per-channel filter bank.

Declaration
private void RebuildFilters(int channels)
Parameters
Type Name Description
int channels

ResetFilters()

Resets all filter states (clears history) without changing parameters.

Declaration
public void ResetFilters()

SetBand(double, double)

Sets the band frequencies (for Bandpass/Bandstop).

Declaration
public void SetBand(double lowFreq, double highFreq)
Parameters
Type Name Description
double lowFreq

Lower band edge in Hz.

double highFreq

Upper band edge in Hz.

SetCutoff(double)

Sets the cutoff frequency (for Lowpass/Highpass).

Declaration
public void SetCutoff(double frequency)
Parameters
Type Name Description
double frequency

Cutoff frequency in Hz.

UpdateParameters(FilterType?, FilterImplementation?, double?, double?, int?, int?)

Updates filter parameters and triggers a rebuild.

Declaration
public void UpdateParameters(FilterType? filterType = null, FilterImplementation? implementation = null, double? cutoffLow = null, double? cutoffHigh = null, int? order = null, int? firTaps = null)
Parameters
Type Name Description
FilterType? filterType

New filter type, or null to keep current.

FilterImplementation? implementation

New implementation, or null to keep current.

double? cutoffLow

New lower cutoff, or null to keep current.

double? cutoffHigh

New upper cutoff, or null to keep current.

int? order

New order, or null to keep current.

int? firTaps

New FIR taps, or null to keep current.

Events

OnParametersChanged

Fired when filter parameters change (type, implementation, cutoffs, order).

Declaration
public event Action<FilterType, FilterImplementation, double, double, int>? OnParametersChanged
Event Type
Type Description
Action<FilterType, FilterImplementation, double, double, int>

OnPublish

Fired when filtered data is published (for ViewModel subscription).

Declaration
public event Action<object>? OnPublish
Event Type
Type Description
Action<dynamic>

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.