Git Product home page Git Product logo

hysteresis's Introduction

hysteresis Circle CI

An implementation of hysteresis in JavaScript

Installation

$ npm install hysteresis
$ bower install littlebits/hysteresis

Example

There are a variety of use-cases for hysteresis. One use-case at littleBits is that we use it to avoid jittery event inference in the cloudBit's data stream.

Imagine detecting a rising-edge event in a jittery but generally upward-growing value. As the value crosses our target threshold value, it may actually go slightly above and then below multiple times in quick succession, triggering many events instead of just one.


|                               /
|                        /\    /
|=======================/==\==/=============
|       /\         /\  /    \/
|------/--\-------/--\/---------------------
|   /\/    \_____/
|  /
| /
|-------------------------------------------
                        ✓     ×

✓ rising-edge event detected
× ignore insignificant threshold cross

= target threshold
- hyteresis buffer

Here is a naive example:

var Hysteresis = require('hysteresis')
var createServer = require('net').createServer

var server = createServer(9500, function(socket){
  var check = Hysteresis([68,70])
  socket.on('data', function(data){
    var didCross = check(Number(data))
    if (didCross) socket.emit(['release', 'ignite'][didCross - 1], data)
  })
})

API

Hysteresis

Hysteresis(threshold, config) -> (check(number) -> 0 | 1 | 2)`

Instantiate a hysteresis instance. You must provide a threshold and may optionally provide a config object that tweaks behaviour.

config exposes the following options:

  • initialSide – May be 1 or 0, defaults to null causing side bootstrapping to be resolved using first received number
  • initialBias – May be 1 or 0, defaults to 1
  • initialIsChange – May be Boolean, defaults to true
  • checkType – May be 'crosses' or 'crossesOrMeets', defaults to 'crossesOrMeets'

The constructor returns a check function that accepts number and returns one of the following codes:

check(number) -> 0 | 1 | 2
  • 0 – the number did not cross the threshold
  • 1 – the number fell below the threshold
  • 2 – the number rose above the threshold

For more details please read the annotated source code and review the tests.

hysteresis's People

Contributors

jasonkuhrt avatar

Stargazers

Calin Tamas avatar  avatar Michael Keller avatar Mattt Eliot avatar

Watchers

Colin avatar  avatar James Cloos avatar  avatar  avatar Alin Cosmanescu avatar

hysteresis's Issues

More tests

Better test coverage for behaviour under different configuration settings would be welcome. In theory fuzzy testing would be probably awesome here but require quite a bit more work so may just go manual in practice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.