Class: Toybox.Math.FirFilter

Overview

Finite Impulse Response (FIR) filter implementation.

See Also:

Example:

Shows the constructor and implementation for filter use with accelerometer data. Borrowed from the PitchCounter sample included in the SDK.

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

// Constructor
function initialize() {
    // initialize FIR filter
    var options = {
        :coefficients => [ -0.0278f, 0.9444f, -0.0278f ],
        :gain => 0.001f
    };

    try {
        mFilter = new Math.FirFilter(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 as Lang.Array<Lang.Float> or Lang.Symbol, :gain as Lang.Float })

Constructor

Parameters:

  • dictionary(Lang.Dictionary)

    A Dictionary containing filter settings.

    • :coefficients(Lang.Array, Lang.Symbol)

      An Array of Float values that specify the filter coefficients. A resource ID 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 resource ID is specified for coefficients instead of an array


Generated Aug 9, 2022 9:12:30 AM