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.

using Toybox.Math;
var mX = [0];
var mY = [0];
var mZ = [0];
var mFilter;

// 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 Math.IirFilter(options);
    }
    catch(e) {
        System.println(e.getErrorMessage());
    }
}

// Callback to receive accelerometer data
function accel_callback(sensorData) {
    mX = mFilter.apply(sensorData.accelerometerData.x);
    mY = sensorData.accelerometerData.y;
    mZ = sensorData.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 Sep 23, 2024, 3:32:49 PM