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.

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.FirFilter?;

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

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

Constructor

Parameters:

  • dictionary — (Lang.Dictionary) —

    A Dictionary containing filter settings.

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

      An Array of Float values that specify the 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