mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.4.2 RunningAverage
This commit is contained in:
parent
d224b5670e
commit
a975ba8a86
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2012-2021 Rob Tillaart
|
||||
Copyright (c) 2012-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -109,8 +109,7 @@ See examples
|
||||
- default size for constructor
|
||||
- update documentation, explain better.
|
||||
- create a double based derived class? Template class?
|
||||
|
||||
- check for optimizations.
|
||||
- clear(bool zero = true) to suppress setting all to 0. ??
|
||||
-
|
||||
- separate releaseNotes.md
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: RunningAverage.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.4.1
|
||||
// VERSION: 0.4.2
|
||||
// DATE: 2015-July-10
|
||||
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
|
||||
// URL: https://github.com/RobTillaart/RunningAverage
|
||||
@ -38,7 +38,7 @@
|
||||
// 0.4.0 2021-05-18 increase size above 256 elements (16 bit version)
|
||||
// 0.4.1 2021-11-22 updated buil-CI, readme, badges
|
||||
// add getAverageLast() functions.
|
||||
//
|
||||
// 0.4.2 2021-12-28 license, minor edits
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
@ -103,7 +103,7 @@ void RunningAverage::addValue(const float value)
|
||||
// returns the average of the data-set added so far, NAN if no elements.
|
||||
float RunningAverage::getAverage()
|
||||
{
|
||||
if (_count == 0)
|
||||
if (_count == 0)
|
||||
{
|
||||
return NAN;
|
||||
}
|
||||
@ -175,14 +175,15 @@ float RunningAverage::getElement(uint16_t index) const
|
||||
}
|
||||
|
||||
|
||||
// Return standard deviation of running average.
|
||||
// Return standard deviation of running average.
|
||||
// If buffer is empty or has only one element, return NAN.
|
||||
float RunningAverage::getStandardDeviation() const
|
||||
{
|
||||
if (_count <= 1)
|
||||
{
|
||||
return NAN;
|
||||
}
|
||||
// see issue #13
|
||||
// need float _stddev = -1;
|
||||
// + patch add() and clear() to reset _stddev to -1;
|
||||
// if (_stddev != -1) return _stddev;
|
||||
if (_count <= 1) return NAN;
|
||||
|
||||
float temp = 0;
|
||||
float average = getFastAverage();
|
||||
@ -191,20 +192,17 @@ float RunningAverage::getStandardDeviation() const
|
||||
temp += pow((_array[i] - average), 2);
|
||||
}
|
||||
temp = sqrt(temp/(_count - 1));
|
||||
|
||||
return temp;
|
||||
// see issue #13
|
||||
// _stddev = temp; // cache the calculate value
|
||||
// return _stddev;
|
||||
}
|
||||
|
||||
|
||||
// Return standard error of running average.
|
||||
// Return standard error of running average.
|
||||
// If buffer is empty or has only one element, return NAN.
|
||||
float RunningAverage::getStandardError() const //++
|
||||
float RunningAverage::getStandardError() const
|
||||
{
|
||||
if (_count <= 1)
|
||||
{
|
||||
return NAN;
|
||||
}
|
||||
|
||||
float temp = getStandardDeviation();
|
||||
if (temp == NAN) return NAN;
|
||||
|
||||
@ -218,7 +216,7 @@ float RunningAverage::getStandardError() const //++
|
||||
|
||||
|
||||
// fill the average with the same value number times. (weight)
|
||||
// This is maximized to size times.
|
||||
// This is maximized to size times.
|
||||
// no need to fill the internal buffer over 100%
|
||||
void RunningAverage::fillValue(const float value, const uint16_t number)
|
||||
{
|
||||
@ -231,6 +229,25 @@ void RunningAverage::fillValue(const float value, const uint16_t number)
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/RobTillaart/RunningAverage/issues/13
|
||||
// - substantially faster version off fillValue()
|
||||
// - adds to program size
|
||||
// void RunningAverage::fillValue(const float value, const uint16_t number)
|
||||
// {
|
||||
// uint16_t s = number;
|
||||
// if (s > _size) s = _size;
|
||||
// for (uint16_t idx = 0; idx < s; idx++)
|
||||
// {
|
||||
// _array[idx] = value;
|
||||
// }
|
||||
// _index = s;
|
||||
// if (_index == _partial) _index = 0; // faster than %
|
||||
// _min = value;
|
||||
// _max = value;
|
||||
// _count = s;
|
||||
// _sum = s * value;
|
||||
// }
|
||||
|
||||
|
||||
float RunningAverage::getValue(const uint16_t position)
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: RunningAverage.h
|
||||
// AUTHOR: Rob.Tillaart@gmail.com
|
||||
// VERSION: 0.4.1
|
||||
// VERSION: 0.4.2
|
||||
// DATE: 2016-dec-01
|
||||
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
|
||||
// URL: https://github.com/RobTillaart/RunningAverage
|
||||
@ -13,7 +13,7 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.1"))
|
||||
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.2"))
|
||||
|
||||
|
||||
class RunningAverage
|
||||
@ -63,8 +63,6 @@ public:
|
||||
float getMaxInBufferLast(uint16_t count);
|
||||
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
uint16_t _size;
|
||||
uint16_t _count;
|
||||
|
@ -2,9 +2,7 @@
|
||||
// FILE: fillValue.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2012-12-30
|
||||
//
|
||||
// PUPROSE: demo + timing of fillValue
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_300.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2021-05-26
|
||||
//
|
||||
// PUPROSE: demonstrate large (16 bit) buffer
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_300.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2021-05-26
|
||||
//
|
||||
// PUPROSE: demonstrate large (16 bit) buffer
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
@ -45,9 +42,6 @@ void setup(void)
|
||||
Serial.print(myRA.getMaxInBufferLast(i));
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -57,3 +51,4 @@ void loop(void)
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_FastAverageTest.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.2.0
|
||||
// DATE: 2015-sep-04
|
||||
//
|
||||
// PUPROSE: demo to see if different average algorithm give different result
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_MinMaxBufferTest.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2015-09-04
|
||||
//
|
||||
// PUPROSE: demo
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: runningAverageMinMaxTest.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2015-apr-10
|
||||
//
|
||||
// PUPROSE: demo
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
@ -50,3 +47,4 @@ void loop(void)
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_getValue.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2020-01-15
|
||||
//
|
||||
// PUPROSE: demonstrate access in order of the values added
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -2,11 +2,9 @@
|
||||
// FILE: runningAverageHour.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2012-12-30
|
||||
//
|
||||
// PUPROSE: show working of runningAverage per hour
|
||||
// in 2 steps - last minute + last hour
|
||||
// 3 or more steps also possible
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_partial.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2021-05-26
|
||||
//
|
||||
// PUPROSE: demonstrate partial use of internal buffer
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_performance.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2020-04-16
|
||||
//
|
||||
// PUPROSE: timing of runningAverage
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
@ -17,7 +14,7 @@ uint32_t start, stop;
|
||||
volatile float x;
|
||||
|
||||
|
||||
void setup(void)
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.print("\nPerformance RunningAverage lib: ");
|
||||
@ -25,39 +22,39 @@ void setup(void)
|
||||
Serial.println();
|
||||
|
||||
myRA.clear(); // explicitly start clean
|
||||
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
myRA.addValue(random(1000) * 0.001);
|
||||
}
|
||||
|
||||
|
||||
test_clear();
|
||||
test_addvalue();
|
||||
test_fillValue();
|
||||
test_getValue();
|
||||
|
||||
|
||||
test_getAverage();
|
||||
test_getFastAverage();
|
||||
|
||||
|
||||
test_getStandardDeviation();
|
||||
test_getStandardError();
|
||||
|
||||
|
||||
test_getMin();
|
||||
test_getMax();
|
||||
test_getMinInBuffer();
|
||||
test_getMaxInBuffer();
|
||||
|
||||
|
||||
test_bufferIsFull();
|
||||
test_getElement();
|
||||
|
||||
|
||||
test_getSize();
|
||||
test_getCount();
|
||||
|
||||
|
||||
Serial.println("\ndone...\n");
|
||||
}
|
||||
|
||||
|
||||
void test_clear(void)
|
||||
void test_clear(void)
|
||||
{
|
||||
start = micros();
|
||||
myRA.clear();
|
||||
@ -99,8 +96,8 @@ void test_getValue()
|
||||
Serial.println(stop - start);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void test_getAverage()
|
||||
{
|
||||
start = micros();
|
||||
@ -110,8 +107,8 @@ void test_getAverage()
|
||||
Serial.println(stop - start);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void test_getFastAverage()
|
||||
{
|
||||
start = micros();
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: runningAverageTest.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.01
|
||||
// DATE: 2012-12-30
|
||||
//
|
||||
// PUPROSE: show working of runningAverage
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -1,11 +1,8 @@
|
||||
//
|
||||
// FILE: ra_two_sensors.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2020-12-06
|
||||
//
|
||||
// PUPROSE: show working of runningAverage for two sensors
|
||||
//
|
||||
|
||||
|
||||
#include "RunningAverage.h"
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/RunningAverage.git"
|
||||
},
|
||||
"version": "0.4.1",
|
||||
"version": "0.4.2",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=RunningAverage
|
||||
version=0.4.1
|
||||
version=0.4.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=The library stores the last N individual values in a circular buffer to calculate the running average.
|
||||
|
@ -38,6 +38,7 @@
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "RUNNINGAVERAGE_LIB_VERSION: %s\n", (char *) RUNNINGAVERAGE_LIB_VERSION);
|
||||
}
|
||||
|
||||
unittest_teardown()
|
||||
@ -47,8 +48,6 @@ unittest_teardown()
|
||||
|
||||
unittest(test_zero_elements)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", (char *) RUNNINGAVERAGE_LIB_VERSION);
|
||||
|
||||
RunningAverage myRA(10);
|
||||
myRA.clear();
|
||||
|
||||
@ -155,7 +154,7 @@ unittest(test_last)
|
||||
assertNAN(myRA.getMinInBufferLast(0));
|
||||
assertNAN(myRA.getAverageLast(0));
|
||||
assertNAN(myRA.getMaxInBufferLast(0));
|
||||
|
||||
|
||||
assertEqualFloat(999.0, myRA.getMinInBufferLast(1), 0.001);
|
||||
assertEqualFloat(999.0, myRA.getAverageLast(1), 0.001);
|
||||
assertEqualFloat(999.0, myRA.getMaxInBufferLast(1), 0.001);
|
||||
|
Loading…
Reference in New Issue
Block a user