Class: Toybox.Math.IirFilter

Overview

Infinite Impulse Response (IIR) filter implementation.

See Also:

Example:

Shows the constructor and implementation for filter use with accelerometer data.

import Toybox.Math;
import Toybox.System;
import Toybox.Sensor;

var mX as Array<Numeric> = [0];
var mY as Array<Numeric> = [0];
var mZ as Array<Numeric> = [0];
var mFilter as Math.IirFilter?;

// Constructor
function initialize() {
    // initialize IIR filter. Coefficients are for demonstration purposes only.
    var options = {
        :coefficients_a => [ -0.0278f, 0.9444f, -0.0278f ],
        :coefficients_b => [ 0.0278f, -0.9444f, 0.0278],
        :gain => 0.001f
    };

    try {
        mFilter = new IirFilter(options);
    }
    catch(e) {
        System.println(e.getErrorMessage());
    }
}

// Callback to receive accelerometer data
function accel_callback(sensorData as Sensor.SensorData) as Void {
    if (sensorData != null) {
        var accelerometerData = sensorData.accelerometerData;
        if (accelerometerData != null) {
            mX = mFilter.apply(accelerometerData.x);
            mY = accelerometerData.y;
            mZ = accelerometerData.z;
            onAccelData();
        }
    }
}

Since:

API Level 2.3.0

Instance Method Summary collapse

Instance Method Details

apply(data as Lang.Array<Lang.Numeric>) as Lang.Array<Lang.Float>

Since:

API Level 2.3.0

initialize(dictionary as { :coefficients_a as Lang.Array<Lang.Float> or Lang.ResourceId, :coefficients_b as Lang.Array<Lang.Float> or Lang.ResourceId, :gain as Lang.Float })

Constructor

Parameters:

  • dictionary — (Lang.Dictionary) —

    A Dictionary containing filter settings.

    • :coefficients_a — (Lang.Array, Lang.ResourceId) —

      An Array of Float values that specify the feedback filter coefficients. A ResourceId referencing a JSON Array resource can also be used here.

    • :coefficients_b — (Lang.Array, Lang.ResourceId) —

      An Array of Float values that specify the feed forward filter coefficients. A ResourceId referencing a JSON Array resource can also be used here.

    • :gain — (Lang.Float) —

      A Float value that specifies a multiplier to be applied to the coefficients.

Since:

API Level 2.3.0

Throws:

  • (Lang.InvalidOptionsException) —

    If the Dictionary does not have valid coefficients for filter or the :gain field is missing. Will also be thrown if an invalid JSON ResourceId is specified for :coefficients instead of an array


Generated Oct 30, 2026, 1:07:32 PM