Class SlidingWindow

Sliding window over a stream of row samples, publishing a windowed MathNet.Numerics.LinearAlgebra.Matrix<T> of BufferSize × Channels every Stride rows.

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:

{
  "Window256": {
    "Type": "SlidingWindow",
    "Inputs": [ "EMG" ],
    "DesiredRate": 2000,
    "Params": [ 256, 128, "Hamming" ],
    "Path": "C:/Data/session1"
  }
}

Params:

IndexDescription
0BufferSize (int) — number of samples in the window.
1Stride (int) — number of samples between consecutive windows.
2WindowType (string) — Rectangular, Hamming, or Hann.

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

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

Overview: Incoming data (vectors or matrix batches) is appended row by row into an internal buffer. When the buffer reaches BufferSize rows, a windowed matrix is published downstream and the buffer is advanced by Stride rows, producing overlap of BufferSize − Stride rows between consecutive windows.

Zero pre-fill: On first data arrival the buffer is pre-filled with BufferSize − 1 zero vectors so that the first window publishes immediately. This ensures multiple sliding windows with different sizes start outputting at the same time rather than waiting for their respective buffers to fill.

Rate propagation: The output rate is automatically calculated as incoming rows per second divided by Stride and propagated when it changes. The local BlockVisualization scope is informed of the upstream sample rate (not the output rate) so its time-axis and y-axis match the data being fed.

Window functions: Rectangular (no weighting), Hamming, or Hann. Each sample row is element-wise multiplied by the window coefficients before output.

Input types: Accepts MathNet.Numerics.LinearAlgebra.Vector<T> (single row), MathNet.Numerics.LinearAlgebra.Matrix<T> (batch of rows), or tagged tuples wrapping either.

Thread safety: All buffer access is synchronised via a Lock.

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

Constructors

SlidingWindow(string, int, int, WindowType, double)

Initializes a new SlidingWindow block.

Declaration
public SlidingWindow(string name, int bufferSize = 100, int stride = 25, SlidingWindow.WindowType window = WindowType.Hamming, double desiredRate = 0)
Parameters
Type Name Description
string name

Display name.

int bufferSize

Window size in samples. Must be positive.

int stride

Stride in samples. Must be in [1, bufferSize].

SlidingWindow.WindowType window

Window function type.

double desiredRate

Incoming sample rate in Hz. Used to compute the initial output rate (desiredRate / stride) for downstream propagation.

Exceptions
Type Condition
ArgumentOutOfRangeException

Thrown if bufferSize ≤ 0 or stride is out of range.

Fields

DefaultBufferSize

Declaration
public const int DefaultBufferSize = 100
Field Value
Type Description
int

DefaultStride

Declaration
public const int DefaultStride = 25
Field Value
Type Description
int

DefaultWindow

Declaration
public const SlidingWindow.WindowType DefaultWindow = Hamming
Field Value
Type Description
SlidingWindow.WindowType

_bufferSize

Number of samples in the window buffer.

Declaration
[ObservableProperty]
private int _bufferSize
Field Value
Type Description
int

_channels

Number of channels (columns), set from the first received row.

Declaration
private int _channels
Field Value
Type Description
int

_count

Declaration
private int _count
Field Value
Type Description
int

_gate

Lock guarding all buffer access.

Declaration
private readonly object _gate
Field Value
Type Description
object

_head

Declaration
private int _head
Field Value
Type Description
int

_receivesVectors

Declaration
private bool _receivesVectors
Field Value
Type Description
bool

_rows

Accumulated rows waiting to form a complete window.

Declaration
private double[,]? _rows
Field Value
Type Description
double[,]

_storedInputRate

Stored input rate for use before upstream rate propagation arrives.

Declaration
private double _storedInputRate
Field Value
Type Description
double

_stride

Number of samples between consecutive window outputs (overlap = BufferSize − Stride).

Declaration
[ObservableProperty]
private int _stride
Field Value
Type Description
int

_window

Precomputed window coefficients.

Declaration
private Vector<double>? _window
Field Value
Type Description
Vector<double>

_windowKind

Window function applied to each output matrix.

Declaration
[ObservableProperty]
private SlidingWindow.WindowType _windowKind
Field Value
Type Description
SlidingWindow.WindowType

Properties

BufferSize

Number of samples in the window buffer.

Declaration
[ExcludeFromCodeCoverage]
public int BufferSize { get; set; }
Property Value
Type Description
int

ConfigSummary

Human-readable configuration summary.

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

DumpFilePrefix

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

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

EffectiveInputRate

Effective input rate: upstream-propagated or stored from constructor.

Declaration
private double EffectiveInputRate { get; }
Property Value
Type Description
double

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

OutputRate

Calculated output rate (incoming rows per second divided by stride).

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

OverlapPercent

Overlap percentage between consecutive windows.

Declaration
public int OverlapPercent { get; }
Property Value
Type Description
int

RowRate

Declaration
private double RowRate { get; }
Property Value
Type Description
double

Stride

Number of samples between consecutive window outputs (overlap = BufferSize − Stride).

Declaration
[ExcludeFromCodeCoverage]
public int Stride { get; set; }
Property Value
Type Description
int

TransformsPublicationRate

True when this block computes its own output cadence from its input.

Declaration
protected override bool TransformsPublicationRate { get; }
Property Value
Type Description
bool
Overrides
BaseBlock.TransformsPublicationRate

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

Visualization helper for binding the latest input to the UI scope.

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

WindowKind

Window function applied to each output matrix.

Declaration
[ExcludeFromCodeCoverage]
public SlidingWindow.WindowType WindowKind { get; set; }
Property Value
Type Description
SlidingWindow.WindowType

Methods

AppendRow(Vector<double>?, Matrix<double>?, int, int)

Declaration
private void AppendRow(Vector<double>? vector, Matrix<double>? matrix, int row, int channels)
Parameters
Type Name Description
Vector<double> vector
Matrix<double> matrix
int row
int channels

BuildWindow(int, WindowType)

Computes window function coefficients for the given size and type.

Declaration
private static Vector<double> BuildWindow(int n, SlidingWindow.WindowType wt)
Parameters
Type Name Description
int n
SlidingWindow.WindowType wt
Returns
Type Description
Vector<double>

BuildWindowedMatrix(int)

Allocates only the stable matrix that is published downstream.

Declaration
private Matrix<double> BuildWindowedMatrix(int bufferSize)
Parameters
Type Name Description
int bufferSize
Returns
Type Description
Matrix<double>

ConfigureInput(IServiceProvider, JsonModel)

Creates a SlidingWindow from a JSON pipeline definition.

Declaration
public static SlidingWindow 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
SlidingWindow

A configured SlidingWindow instance.

Exceptions
Type Condition
ArgumentException

Thrown if required parameters are missing or invalid.

Dispose()

Disposes visualization and base class resources.

Declaration
public override void Dispose()
Overrides
BaseBlock.Dispose()

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()

NotifyComputedPropertiesChanged()

Raises property-changed for all computed properties.

Declaration
private void NotifyComputedPropertiesChanged()

OnBufferSizeChanged(int)

Rebuilds window, re-pads buffer with zeros, and keeps most recent rows when buffer size changes.

Declaration
private void OnBufferSizeChanged(int value)
Parameters
Type Name Description
int value

OnBufferSizeChanged(int, int)

Executes the logic for when BufferSize just changed.

Declaration
private void OnBufferSizeChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

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

OnBufferSizeChanging(int)

Executes the logic for when BufferSize is changing.

Declaration
private void OnBufferSizeChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

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

OnBufferSizeChanging(int, int)

Executes the logic for when BufferSize is changing.

Declaration
private void OnBufferSizeChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

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

OnInputRateChanged(double)

Recalculates output rate when the upstream input rate changes.

Declaration
protected override void OnInputRateChanged(double newInputRate)
Parameters
Type Name Description
double newInputRate
Overrides
BaseBlock.OnInputRateChanged(double)

OnReceive(object, object)

Processes incoming data: appends rows to the buffer and publishes windowed matrices when the buffer is full. On first arrival, the buffer is pre-filled with zeros so the first window publishes immediately.

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>, MathNet.Numerics.LinearAlgebra.Matrix<T>, or tagged tuple wrapping either.

Overrides
BaseBlock.OnReceive(object, object)

OnSignalRateChanged(double)

Re-derives the rates once the source declares its true sample rate.

Declaration
protected override void OnSignalRateChanged(double newRate)
Parameters
Type Name Description
double newRate
Overrides
BaseBlock.OnSignalRateChanged(double)
Remarks

A batching source propagates SignalRate separately from, and possibly later than, the publish rate that drives OnInputRateChanged(double). When it lands, RowRate changes from the publish rate to the real one, so the scope has to be told again — otherwise it keeps whichever value happened to arrive first.

OnStrideChanged(int)

Recalculates output rate when stride changes.

Declaration
private void OnStrideChanged(int value)
Parameters
Type Name Description
int value

OnStrideChanged(int, int)

Executes the logic for when Stride just changed.

Declaration
private void OnStrideChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

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

OnStrideChanging(int)

Executes the logic for when Stride is changing.

Declaration
private void OnStrideChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

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

OnStrideChanging(int, int)

Executes the logic for when Stride is changing.

Declaration
private void OnStrideChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

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

OnWindowKindChanged(WindowType)

Rebuilds window coefficients when window type changes.

Declaration
private void OnWindowKindChanged(SlidingWindow.WindowType value)
Parameters
Type Name Description
SlidingWindow.WindowType value

OnWindowKindChanged(WindowType, WindowType)

Executes the logic for when WindowKind just changed.

Declaration
private void OnWindowKindChanged(SlidingWindow.WindowType oldValue, SlidingWindow.WindowType newValue)
Parameters
Type Name Description
SlidingWindow.WindowType oldValue

The previous property value that was replaced.

SlidingWindow.WindowType newValue

The new property value that was set.

Remarks

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

OnWindowKindChanging(WindowType)

Executes the logic for when WindowKind is changing.

Declaration
private void OnWindowKindChanging(SlidingWindow.WindowType value)
Parameters
Type Name Description
SlidingWindow.WindowType value

The new property value being set.

Remarks

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

OnWindowKindChanging(WindowType, WindowType)

Executes the logic for when WindowKind is changing.

Declaration
private void OnWindowKindChanging(SlidingWindow.WindowType oldValue, SlidingWindow.WindowType newValue)
Parameters
Type Name Description
SlidingWindow.WindowType oldValue

The previous property value that is being replaced.

SlidingWindow.WindowType newValue

The new property value being set.

Remarks

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

PublishReadyWindow()

Declaration
private void PublishReadyWindow()

RecalculateOutputRate()

Computes output rate as incoming rows per second divided by stride and propagates downstream.

Declaration
private void RecalculateOutputRate()

Reconfigure(int, int, WindowType)

Reconfigures buffer size, stride, and window type at runtime. Triggers rate recalculation and downstream propagation.

Declaration
public void Reconfigure(int bufferSize, int stride, SlidingWindow.WindowType window)
Parameters
Type Name Description
int bufferSize

New window size. Must be positive.

int stride

New stride. Must be in [1, bufferSize].

SlidingWindow.WindowType window

New window function type.

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.