From f34f5627cc8990b52d9a5907b6d05bf723f96963 Mon Sep 17 00:00:00 2001 From: Thomas Sommer Date: Tue, 7 Jun 2022 17:00:32 +0200 Subject: [PATCH] added filter::MovingAverage::reset(), maintenance of code --- src/modm/math/filter/moving_average.hpp | 47 +++++++++---------- .../math/filter/moving_average_float_impl.hpp | 45 ++++++++---------- 2 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/modm/math/filter/moving_average.hpp b/src/modm/math/filter/moving_average.hpp index 78a80746d4..3d1f844701 100644 --- a/src/modm/math/filter/moving_average.hpp +++ b/src/modm/math/filter/moving_average.hpp @@ -15,13 +15,13 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // ---------------------------------------------------------------------------- - -#ifndef MODM_MOVING_AVERAGE_HPP -#define MODM_MOVING_AVERAGE_HPP - +#pragma once #include #include -#include + +#include +#include +#include namespace modm { @@ -57,16 +57,13 @@ namespace modm template class MovingAverage { - private: - using Index = std::conditional_t< - (N >= 256), - uint_fast16_t, - uint_fast8_t - >; - public: MovingAverage(const T& initialValue = 0); + /// Reset whole buffer to 'input' + /// Next call of getValue() returns 'input' + void reset(const T& input); + /// Append new value void update(const T& input); @@ -76,7 +73,7 @@ namespace modm getValue() const; private: - Index index; + least_uint index{0}; T buffer[N]; T sum; }; @@ -85,16 +82,21 @@ namespace modm // ---------------------------------------------------------------------------- template -modm::filter::MovingAverage::MovingAverage(const T& initialValue) : - index(0), sum(N * initialValue) +modm::filter::MovingAverage::MovingAverage(const T& initialValue) { - for (Index i = 0; i < N; ++i) { - buffer[i] = initialValue; - } + reset(initialValue); +} + +// ---------------------------------------------------------------------------- +template +void +modm::filter::MovingAverage::reset(const T& input) +{ + std::fill(std::begin(buffer), std::end(buffer), input); + sum = N * input; } // ---------------------------------------------------------------------------- -// TODO implementierung für float anpassen template void modm::filter::MovingAverage::update(const T& input) @@ -104,15 +106,11 @@ modm::filter::MovingAverage::update(const T& input) buffer[index] = input; - index++; - if (index >= N) { + if (++index >= N) index = 0; - } } // ----------------------------------------------------------------------------- - - template const T modm::filter::MovingAverage::getValue() const @@ -122,4 +120,3 @@ modm::filter::MovingAverage::getValue() const #include "moving_average_float_impl.hpp" -#endif // MODM_MOVING_AVERAGE_HPP diff --git a/src/modm/math/filter/moving_average_float_impl.hpp b/src/modm/math/filter/moving_average_float_impl.hpp index d56ae6f7de..6bd7c6df39 100644 --- a/src/modm/math/filter/moving_average_float_impl.hpp +++ b/src/modm/math/filter/moving_average_float_impl.hpp @@ -12,10 +12,10 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // ---------------------------------------------------------------------------- +#pragma once +#include "moving_average.hpp" -#ifndef MODM_MOVING_AVERAGE_HPP - #error "Don't include this file directly, use 'moving_average.hpp' instead!" -#endif +#include namespace modm { @@ -23,17 +23,13 @@ namespace modm template class MovingAverage { - private: - using Index = std::conditional_t< - (N >= 256), - uint_fast16_t, - uint_fast8_t - >; - public: - MovingAverage(const double& initialValue = 0); + /// Reset whole buffer to 'input' + /// Next call of getValue() returns 'input' + void reset(const double& input); + /// Append new value void update(const double& input); @@ -43,7 +39,7 @@ namespace modm getValue() const; private: - Index index; + least_uint index{0}; double buffer[N]; double sum; }; @@ -53,14 +49,19 @@ namespace modm //--------------------------------------------------------------------------------- template -modm::filter::MovingAverage::MovingAverage(const double& initialValue) : - index(0), sum(N * initialValue) +modm::filter::MovingAverage::MovingAverage(const double& initialValue) { - for (Index i = 0; i < N; ++i) { - buffer[i] = initialValue; - } + reset(initialValue); } +// ---------------------------------------------------------------------------- +template +void +modm::filter::MovingAverage::reset(const double& input) +{ + std::fill(std::begin(buffer), std::end(buffer), input); + sum = N * input; +} //--------------------------------------------------------------------------------- template @@ -68,16 +69,10 @@ void modm::filter::MovingAverage::update(const double& input){ buffer[index] = input; - index++; - if (index >= N) { + if (++index >= N) index = 0; - } - - sum = 0; - for (Index i = 0; i < N; ++i) { - sum+= buffer[i]; - } + sum = std::accumulate(std::begin(buffer), std::end(buffer), 0); } // ------------------------------------------------------------------------------