Git Product home page Git Product logo

array-smooth's Introduction

array-smooth

Codeship Status for ndelvalle/array-smooth Coverage Status dependencies Status devDependencies Status Codacy Badge code style: prettier

In smoothing, the data points of a signal are modified so individual points (presumably because of noise) are reduced, and points that are lower than the adjacent points are increased leading to a smoother signal. The algorithm used in this implementation is the moving average (rolling average or running average)

Install

$ npm install --save array-smooth
$ yarn add array-smooth

Use

const smooth = require('array-smooth')

const arr = [15, 2, 3, 14, 5, 6, 2, 8, 9, 10, 22, 3, 2, 11, 12]
const windowSize = 2
const arrSmoothed = smooth(arr, windowSize)

// arrSmoothed: [6.666666666666667, 8.5, 7.8, 6, 6, 7, 6, 7, 10.2, 10.4, 9.2, 9.6, 10, 7, 8.333333333333334]

API

smooth(array, windowSize, [getter], [setter])

array array

An array containing the values that we want to smooth, it can be an array of numbers or an array of objects defining a specific getter and / or setter

windowSize number

The smooth window option its a number that specifies the width of the moving average. It represents how many values to the right and left of the current index the algorithm will take in account when getting the sample to generate the smoothed value

getter(value) function

This function will receive the array value as an argument, it should return the attribute to smooth the array with. Is equivalent to calling array.map(getter)

Example:

const arr = [{ value: 15 }, { value: 2 }, { value: 3 }, { value: 14 }, { value: 5 }]

const getter = (item) => item.value
setter(value, smoothedValue) function

This function receives value and smoothedValue as arguments. The response will be the item that will populate the result array

Example:

const arr = [{ value: 15 }, { value: 2 }, { value: 3 }, { value: 14 }, { value: 5 }]

const getter = (item) => item.value
const setter = (item, itemSomoothed) => ({ value: item, valueSmoothed: itemSomoothed })

Playground

Edit array-smooth

array-smooth's People

Contributors

gillchristian avatar natelowry avatar ndelvalle avatar ughettoariel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

array-smooth's Issues

Inefficient algorithm

This algorithm has a worst case runtime of at least N^3, and an average of at least N^2. There are linear solutions for a moving average, so this stands to be improved drastically. In its current form, this would be unusable on large datasets, especially where performance is a concern.

While the functional style methods of Array.prototype are convenient, nesting them in this way is wildly inefficient. You should look into immutable.js sequences to chain operations efficiently, if you want to continue to use that style.

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.