Class Function
Applies element-wise mathematical functions to input vectors.
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:
{
"Rectify": {
"Type": "Function",
"Inputs": [ "EMG" ],
"Params": [ "abs" ],
"Path": "C:/Data/session1"
}
}
Params:
| Index | Description |
|---|---|
| 0 | FunctionType (string) — abs, add, multiply, power, clip, threshold. |
| 1 | Param1 (double, optional) — first parameter. |
| 2 | Param2 (double, optional) — second parameter (clip upper bound). |
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.SignalProcessing
Assembly: MOSAIC.dll
Syntax
public class Function : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks
Overview: Each element of the incoming MathNet.Numerics.LinearAlgebra.Vector<T> is transformed by the configured function and the result is published downstream. The function type and parameters can be changed at runtime — the internal delegate is rebuilt automatically.
Supported functions:
| Type | Formula |
|---|---|
abs | |x| |
add | x + P1 |
multiply | x × P1 |
power | x^P1 |
clip | clamp(x, P1, P2) |
threshold | sign(x) × max(0, |x| − P1) |
CSV logging: When a Path is specified, Dumper
automatically logs the output vector via Publish(object).
Constructors
Function(string, double, Func<double, double>)
Initializes a new Function block.
Declaration
public Function(string name, double desiredRate, Func<double, double> function)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Display name. |
| double | desiredRate | Processing rate in Hz (typically inherited). |
| Func<double, double> | function | The element-wise function to apply. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown if |
Fields
_function
The compiled element-wise function delegate.
Declaration
private Func<double, double> _function
Field Value
| Type | Description |
|---|---|
| Func<double, double> |
_functionType
The function type string (abs, add, multiply, power, clip, threshold).
Declaration
[ObservableProperty]
private string _functionType
Field Value
| Type | Description |
|---|---|
| string |
_param1
First parameter (used by add, multiply, power, threshold, clip).
Declaration
[ObservableProperty]
private double _param1
Field Value
| Type | Description |
|---|---|
| double |
_param2
Second parameter (used by clip for the upper bound).
Declaration
[ObservableProperty]
private double _param2
Field Value
| Type | Description |
|---|---|
| double |
_viz
Optional visualization bundle. Set by the ViewModel so the block can feed data to all monitors in one call.
Declaration
private BlockVisualization? _viz
Field Value
| Type | Description |
|---|---|
| BlockVisualization |
Properties
AvailableFunctionTypes
Available function type names for UI dropdowns.
Declaration
public static IReadOnlyList<string> AvailableFunctionTypes { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<string> |
DumpFilePrefix
Recordings from this block are named <block>_fn.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
FunctionType
The function type string (abs, add, multiply, power, clip, threshold).
Declaration
[ExcludeFromCodeCoverage]
public string FunctionType { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
JsonTypeName
Block type identifier for JSON serialization. Override for a custom name.
Declaration
protected override string JsonTypeName { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
Param1
First parameter (used by add, multiply, power, threshold, clip).
Declaration
[ExcludeFromCodeCoverage]
public double Param1 { get; set; }
Property Value
| Type | Description |
|---|---|
| double |
Param2
Second parameter (used by clip for the upper bound).
Declaration
[ExcludeFromCodeCoverage]
public double Param2 { get; set; }
Property Value
| Type | Description |
|---|---|
| double |
Visualization
This block's visualization, or null when it has none.
Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Overrides
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
Live plot for this block. Assigned by the ViewModel, which owns the scope.
Declaration
public BlockVisualization? Viz { get; set; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Remarks
Setting this re-pushes the publish rate: the rate is normally pushed when it changes, which for these blocks happens during construction — before the ViewModel has handed over the scope — so without this the scope would never learn its time base.
Methods
BuildFunction(JsonModel)
Parses the JSON params and builds the initial function delegate.
Declaration
private static (Func<double, double> function, string type, double p1, double p2) BuildFunction(JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonModel | m |
Returns
| Type | Description |
|---|---|
| (Func<double, double> function, string type, double p1, double p2) |
ConfigureInput(IServiceProvider, JsonModel)
Creates a Function from a JSON pipeline definition.
Declaration
public static Function 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 |
Returns
| Type | Description |
|---|---|
| Function | A configured Function instance. |
Dispose()
Releases visualization and base class resources (including CSV dumper if configured).
Declaration
public override void Dispose()
Overrides
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 |
Overrides
OnFunctionTypeChanged(string)
Rebuilds the function delegate when the type changes.
Declaration
private void OnFunctionTypeChanged(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value |
OnFunctionTypeChanged(string?, string)
Executes the logic for when FunctionType just changed.
Declaration
private void OnFunctionTypeChanged(string? oldValue, string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | oldValue | The previous property value that was replaced. |
| string | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of FunctionType is changed.
OnFunctionTypeChanging(string)
Executes the logic for when FunctionType is changing.
Declaration
private void OnFunctionTypeChanging(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The new property value being set. |
Remarks
This method is invoked right before the value of FunctionType is changed.
OnFunctionTypeChanging(string?, string)
Executes the logic for when FunctionType is changing.
Declaration
private void OnFunctionTypeChanging(string? oldValue, string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | oldValue | The previous property value that is being replaced. |
| string | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of FunctionType is changed.
OnParam1Changed(double)
Rebuilds the function delegate when Param1 changes.
Declaration
private void OnParam1Changed(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value |
OnParam1Changed(double, double)
Executes the logic for when Param1 just changed.
Declaration
private void OnParam1Changed(double oldValue, double newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| double | oldValue | The previous property value that was replaced. |
| double | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of Param1 is changed.
OnParam1Changing(double)
Executes the logic for when Param1 is changing.
Declaration
private void OnParam1Changing(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value | The new property value being set. |
Remarks
This method is invoked right before the value of Param1 is changed.
OnParam1Changing(double, double)
Executes the logic for when Param1 is changing.
Declaration
private void OnParam1Changing(double oldValue, double newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| double | oldValue | The previous property value that is being replaced. |
| double | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of Param1 is changed.
OnParam2Changed(double)
Rebuilds the function delegate when Param2 changes.
Declaration
private void OnParam2Changed(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value |
OnParam2Changed(double, double)
Executes the logic for when Param2 just changed.
Declaration
private void OnParam2Changed(double oldValue, double newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| double | oldValue | The previous property value that was replaced. |
| double | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of Param2 is changed.
OnParam2Changing(double)
Executes the logic for when Param2 is changing.
Declaration
private void OnParam2Changing(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value | The new property value being set. |
Remarks
This method is invoked right before the value of Param2 is changed.
OnParam2Changing(double, double)
Executes the logic for when Param2 is changing.
Declaration
private void OnParam2Changing(double oldValue, double newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| double | oldValue | The previous property value that is being replaced. |
| double | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of Param2 is changed.
OnReceive(object, object)
Applies the configured function element-wise to the input vector and publishes the result.
Declaration
protected override void OnReceive(object sender, object data)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | The upstream block. |
| object | data | Expected to be a MathNet.Numerics.LinearAlgebra.Vector<T> of double. Non-vector inputs are silently ignored. |
Overrides
RebuildFunction()
Recompiles the element-wise delegate from current property values.
Declaration
private void RebuildFunction()
SetFunction(string, double, double)
Sets function type and parameters programmatically and notifies the UI.
Declaration
public void SetFunction(string type, double param1 = 0, double param2 = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | type | Function type string. |
| double | param1 | First parameter. |
| double | param2 | Second parameter. |