filter

Provides a consistent API to a collection of real-time signal processing filters.

The MotionStack filter object contains a collection of common real-time signal processing filters. These filters are optimized to operate on digital signals as they occur with near zero time delay.

Most filters work by first creating a new filter instance, and then using that instance as new data is produced.

bandGapFilter

If the absolute value of the data is less than the band threshold, |datum| < band, then the datum is set to 0. Use this method directly on the data without creating a new filter method instance.

Method

bandGapFilter(datum, band)

  • datum {Number or Object} - The data to filter.
  • band {Number} - The positive threshold value.

Example

var accelerometer = new MotionStack.Accelerometer();
accelerometer.start(function(e) {
  var filteredAccel = MotionStack.filter.bandGapFilter({x: e.x, y: e.y, z: e.z}, 0.35);
  console.log(filteredAccel.x, filteredAccel.y, filteredAccel.z);
});

movingSum

Sums the values over the last stencilSize number of input values. First create a new filter method instance.

Constructor

movingSum(initialDatum, stencilSize)

  • initialDatum {Number or Object} - The initial data to filter.
  • stencilSize {Number} - The positive integer size of the stencil.

This creates a new movingSum method.

Method

movingSumInstance(datum)

  • datum {Number or Object} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filter = new MotionStack.filter.movingSum({x:0, y:0, z:0}, 5);

accelerometer.start(function(e) {
  var accelSum = filter({x: e.x, y: e.y, z: e.z});
  console.log(accelSum.x, accelSum.y, accelSum.z);
});

movingAverage

Averages the values over the last stencilSize number of input values. First create a new filter method.

Constructor

movingAverage(initialDatum, stencilSize)

  • initialDatum {Number or Object} - The initial data to filter.
  • stencilSize {Number} - The positive integer size of the stencil.

This creates a new movingAverage method.

Method

movingAverageInstance(datum)

  • datum {Number or Object} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filter = new MotionStack.filter.movingAverage({x:0, y:0, z:0}, 5);

accelerometer.start(function(e) {
  var averageAccel = filter({x: e.x, y: e.y, z: e.z});
  console.log(averageAccel.x, averageAccel.y, averageAccel.z);
});

dynamicSmoother

Constructor

dynamicSmoother(initialDatum, lowerThresh, upperThresh, minSmooth, maxSmooth)

  • initialDatum {Number or Object} - The initial data to filter.
  • lowerThresh {Number} - The lower bound threshold value of the data corresponding to the minSmooth value of the gain parameter.
  • upperThresh {Number} - The upper bound threshold value of the data corresponding to the maxSmooth value of the gain parameter.
  • minSmooth {Number} - The minimum value of the filter gain.
  • maxSmooth {Number} - The maximum value of the filter gain.

This creates a new dynamicSmoother method.

Method

dynamicSmootherInstance(datum)

  • datum {Number or Object} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filter = new MotionStack.filter.dynamicSmoother({x: 0, y: 0, z: 0}, 0.005, 0.5, 0, 1);

accelerometer.start(function(e) {
  var filteredData = filter({x: e.x, y: e.y, z: e.z});
  console.log(filteredData);
});

highPassFilter

Removes low frequencies from the input signal using the given static gain. First create a new filter method instance.

Constructor

highPassFilter(initialDatum, gain)

  • initialDatum {Number} - The initial data to filter.
  • gain {Number} - The filter static gain in the range [0, 1].

This creates a new highPassFilter method.

Method

highPassFilterInstance(datum)

  • datum {Number} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filterX = new MotionStack.filter.highPassFilter(0, 0.9);

accelerometer.start(function(e) {
  var filteredAccelX = filterX(e.x);
  console.log(filteredAccelX);
});

highPassDynamicFilter

Removes low frequencies from the input signal based on a calculated dynamic gain. The dynamic gain is scaled based on the magnitude of the input signal. First create a new filter method instance.

Constructor

highPassDynamicFilter(initialDatum, lowerGain, upperGain, lowerSignal, upperSignal)

  • initialDatum {Number} - The initial data to filter.
  • lowerGain {Number} - The lower bound of the dynamic gain value.
  • upperGain {Number} - The upper bound of the dynamic gain value.
  • lowerSignal {Number} - The lower bound value of the data that corresponds to the lowerGain value of the gain.
  • upperSignal {Number} - The upper bound value of the data that corresponds to the upperGain value of the gain.

This creates a new highPassDynamicFilter method.

Method

highPassDynamicFilterInstance(datum)

  • datum {Number} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filterX = new MotionStack.filter.highPassDynamicFilter(0, 0.9, 1, 0, 1);

accelerometer.start(function(e) {
  var filteredAccelX = filterX(e.x);
  console.log(filteredAccelX);
});

lowPassFilter

Removes high frequencies from the input signal using the given static gain. First create a new filter method instance.

Constructor

lowPassFilter(initialDatum, gain)

  • initialDatum {Number} - The initial data to filter.
  • gain {Number} - The filter static gain in the range [0, 1].

This creates a new lowPassFilter method.

Method

lowPassFilterFilterInstance(datum)

  • datum {Number} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filterX = new MotionStack.filter.lowPassFilter(0, 0.9);

accelerometer.start(function(e) {
  var filteredAccelX = filterX(e.x);
  console.log(filteredAccelX);
});

getIntervalFromTimeStamps

Calculates the time difference between successive timeStamps and optionally applies a movingAverage filter to smooth the result. First create a new filter method instance. If any given timeStamps are undefined, then the interval is set to 16. If the calculated interval is less then 10, then the interval is set to 10.

Constructor

getIntervalFromTimeStamps(stencilSize)

  • stencilSize {Number} - A positive integer indicating the size of the stencil for a movingAverage on the interval.

This creates a new getIntervalFromTimeStamps method.

Method

getIntervalFromTimeStampsInstance(timeStamp)

  • timeStamp {Number} - The real-time sequential timeStamp.

Example

var accelerometer = new MotionStack.Accelerometer();
var intervalFilter = new MotionStack.filter.getIntervalFromTimeStamps(0, 5);

accelerometer.start(function(e) {
  var interval = intervalFilter(e.timeStamp);
  console.log(interval);
});

extremeValueFilter

Returns only the local extreme values of the input signal. Local extreme values are any substantial localized minimum or maximum values. First create a new filter method instance.

Constructor

extremeValueFilter(changeFromPivotThres, changeFromExtremeThres, stationaryTimeThres)

  • changeFromPivotThres {Number} - The threshold amount the signal must vary from its previous value to allow for a new extreme candidate.
  • changeFromExtremeThres {Number} - The threshold amount the signal must vary from an extreme value for it to be considered a new extreme value.
  • stationaryTimeThres {Number} - The amount of time the signal must remain relatively constant to be considered stationary.

This creates a new extremeValueFilter method.

Method

extremeValueFilterInstance(datum)

  • datum {Number} - The real-time sequential data to filter.

Example

var accelerometer = new MotionStack.Accelerometer();
var filterX = new MotionStack.filter.extremeValueFilter(0.55, 0.8, 500);

accelerometer.start(function(e) {
  var filteredAccelX = filterX(e.x);
  console.log(filteredAccelX);
});

createMotionActivityDetector

Detects substantial motion based on the accelerometer. Returns true if in motion, and false otherwise. First create a new filter method instance.

Constructor

createMotionActivityDetector()

This creates a new createMotionActivityDetector method.

Method

motionActivityDetectorInstance(datum)

  • datum {Object} - The real-time sequential acceleration data.

Example

var accelerometer = new MotionStack.Accelerometer();
var detectMotion = new MotionStack.filter.createMotionActivityDetector();

accelerometer.start(function(e) {
  var isInMotion = detectMotion({x: e.x, y: e.y, z: e.z});
  console.log(isInMotion);
});

Source

src/helpers/filter.js