From 990764d85577ed5f9e167b3fab91bbb6f1176407 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 26 Oct 2015 13:29:13 -0700 Subject: [PATCH] Really fixing the MovingAverage class... --- libraries/shared/src/SimpleMovingAverage.h | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/libraries/shared/src/SimpleMovingAverage.h b/libraries/shared/src/SimpleMovingAverage.h index 5f4e7367fc..53754ae241 100644 --- a/libraries/shared/src/SimpleMovingAverage.h +++ b/libraries/shared/src/SimpleMovingAverage.h @@ -15,7 +15,6 @@ #define hifi_SimpleMovingAverage_h #include -#include class SimpleMovingAverage { public: @@ -44,27 +43,24 @@ private: template class MovingAverage { public: - using Samples = std::list< T >; - Samples samples; + const float WEIGHTING = 1.0f / (float)MAX_NUM_SAMPLES; + const float ONE_MINUS_WEIGHTING = 1.0f - WEIGHTING; + int numSamples{ 0 }; T average; void clear() { - samples.clear(); + numSamples = 0; } - bool isAverageValid() const { return !samples.empty(); } + bool isAverageValid() const { return (numSamples > 0); } void addSample(T sample) { - samples.push_front(sample); - int numSamples = samples.size(); - - if (numSamples < MAX_NUM_SAMPLES) { - average = (sample + average * (float)(numSamples - 1)) / (float)(numSamples); + if (numSamples > 0) { + average = (sample * WEIGHTING) + (average * ONE_MINUS_WEIGHTING); } else { - T tail = samples.back(); - samples.pop_back(); - average = average + (sample - tail) / (float)(numSamples); + average = sample; } + numSamples++; } };