#149 improve accuracy of getFastAverage()

This commit is contained in:
RobTillaart 2020-04-16 12:56:15 +02:00
parent 7d77ca1216
commit 6e867f06aa
6 changed files with 16 additions and 27 deletions

View File

@ -1,7 +1,7 @@
MIT License # MIT License
Copyright (c) 2010-2018 Rob Tillaart Copyright (c) 2010-2020 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -19,4 +19,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.

View File

@ -1,7 +1,7 @@
// //
// FILE: RunningAverage.cpp // FILE: RunningAverage.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.2.15 // VERSION: 0.2.16
// DATE: 2015-July-10 // DATE: 2015-July-10
// PURPOSE: RunningAverage library for Arduino // PURPOSE: RunningAverage library for Arduino
// //
@ -30,6 +30,7 @@
// refactored a bit; marked some TODO's; all function names to camelCase // refactored a bit; marked some TODO's; all function names to camelCase
// 0.2.14 - 2020-01-15 added getValue(n) to retrieve elements in order of addition - see issue #132 // 0.2.14 - 2020-01-15 added getValue(n) to retrieve elements in order of addition - see issue #132
// 0.2.15 - 2020-01-17 fix overflow in getValue - see issue #139 // 0.2.15 - 2020-01-17 fix overflow in getValue - see issue #139
// 0.2.16 2020-04-16 improve _sum - see issue #149 (bourkemcrobbo)
// //
// Released to the public domain // Released to the public domain
// //
@ -89,16 +90,16 @@ void RunningAverage::addValue(const float value)
} }
// returns the average of the data-set added sofar // returns the average of the data-set added sofar
float RunningAverage::getAverage() const float RunningAverage::getAverage()
{ {
if (_cnt == 0) return NAN; if (_cnt == 0) return NAN;
float sum = 0; _sum = 0;
for (uint8_t i = 0; i < _cnt; i++) for (uint8_t i = 0; i < _cnt; i++)
{ {
sum += _ar[i]; _sum += _ar[i];
} }
return sum / _cnt; return _sum / _cnt;
} }
float RunningAverage::getFastAverage() const float RunningAverage::getFastAverage() const

View File

@ -1,7 +1,7 @@
// //
// FILE: RunningAverage.h // FILE: RunningAverage.h
// AUTHOR: Rob.Tillaart@gmail.com // AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.2.15 // VERSION: 0.2.16
// DATE: 2016-dec-01 // DATE: 2016-dec-01
// PURPOSE: RunningAverage library for Arduino // PURPOSE: RunningAverage library for Arduino
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/RunningAverage // URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/RunningAverage
@ -17,7 +17,7 @@
#ifndef RunningAverage_h #ifndef RunningAverage_h
#define RunningAverage_h #define RunningAverage_h
#define RUNNINGAVERAGE_LIB_VERSION "0.2.15" #define RUNNINGAVERAGE_LIB_VERSION "0.2.16"
#include "Arduino.h" #include "Arduino.h"
@ -33,7 +33,7 @@ public:
void fillValue(const float, const uint8_t); void fillValue(const float, const uint8_t);
float getValue(const uint8_t); float getValue(const uint8_t);
float getAverage() const; // iterates over all elements. float getAverage(); // iterates over all elements.
float getFastAverage() const; // reuses previous values. float getFastAverage() const; // reuses previous values.
// return statistical characteristics of the running average // return statistical characteristics of the running average

View File

@ -1,17 +1,9 @@
#######################################
# Syntax Coloring Map For RunningAverage # Syntax Coloring Map For RunningAverage
#######################################
#######################################
# Datatypes (KEYWORD1) # Datatypes (KEYWORD1)
#######################################
RunningAverage KEYWORD1 RunningAverage KEYWORD1
#######################################
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
#######################################
clear KEYWORD2 clear KEYWORD2
addValue KEYWORD2 addValue KEYWORD2
getAverage KEYWORD2 getAverage KEYWORD2
@ -29,13 +21,7 @@ getMinInBuffer KEYWORD2
getMaxInBuffer KEYWORD2 getMaxInBuffer KEYWORD2
getValue KEYWORD2 getValue KEYWORD2
#######################################
# Instances (KEYWORD2) # Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1) # Constants (LITERAL1)
#######################################
RUNNINGAVERAGE_LIB_VERSION LITERAL1 RUNNINGAVERAGE_LIB_VERSION LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/Arduino.git" "url": "https://github.com/RobTillaart/Arduino.git"
}, },
"version": "0.2.15", "version": "0.2.16",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",
"export": { "export": {

View File

@ -1,5 +1,5 @@
name=RunningAverage name=RunningAverage
version=0.2.15 version=0.2.16
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=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. sentence=The library stores the last N individual values in a circular buffer to calculate the running average.
@ -7,3 +7,5 @@ paragraph=Supports min max average
category=Data Processing category=Data Processing
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/ url=https://github.com/RobTillaart/Arduino/tree/master/libraries/
architectures=* architectures=*
includes=RunningAverager.h
depends=