Giter Site home page Giter Site logo

gnalbandian / runningaverage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robtillaart/runningaverage

3.0 2.0 1.0 21 KB

Arduino library to calculate the running average by means of a statically allocated circular buffer.

License: MIT License

C++ 100.00%

runningaverage's Introduction

Arduino CI License: MIT GitHub release

ATENTION

This library is based upon RobTillaart lib with modification to make use of static allocation by using circular buffer library provided by rlogiacco

RunningAverage original library: https://github.com/RobTillaart/RunningAverage

Example's files haven't been updated. Sorry!

RunningAverage

Arduino library to calculate the running average by means of a circular buffer.

Description

The RunningAverage object gives a running average of the last N numbers, giving them all equal weight. This is done by feeding new data to a internal circular buffer, popping out the oldest dataitem and replacing it by the newest. The size of the internal buffer can be set in the constructor.

By keeping track of the _sum the runningAverage can be calculated fast (only 1 division). This is done with getFastAverage(). However the constant adding/subtracting when adding new elements introduces an accumulating error. In tests adding up to 1500000 numbers this error was always small. But that is no proof. In version 0.2.16 there is a fix added that uses the calculation of the sum in getAverage() to update the internal _sum.

Interface

Constructor

  • RunningAverage(const uint8_t size) allocates dynamic memory, one float (4 bytes) per element. No default size (yet).
  • ~RunningAverage() deconstructor to free the memory allocated.

Basic

  • clear() empties internal buffer.
  • add(value) wrapper for addValue()
  • addValue(value) adds a new value to the object, if internal buffer is full, the oldest element is removed.
  • fillValue(value, nr) adds nr elements of value. Good for initializing system.
  • getValue(pos) returns element at position pos.
  • getAverage() iterates over all elements to get the average, slower but accurate
  • getFastAverage() reuses previous calculated values, therefor faster. Accuracy can drift.

Extended functions

  • getStandardDeviation() returns the stddev of the current content. Needs more than one element.
  • getStandardError() returns the stderror of the current content.
  • getMin() returns minimum since last clear, does not need to be in the buffer.
  • getMax() returns maximum since last clear, does not need to be in the buffer.
  • getMinInBuffer() returns minimum in the internal buffer.
  • getMaxInBuffer() returns maxumum in the internal buffer.

Admin functions

  • bufferIsFull() returns true if buffer is full.
  • getElement(uint8_t idx) get element directly from internal buffer. (debug)
  • getSize() idem.
  • getCount() idem.

Operation

See examples

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.