Skip to content

Commit

Permalink
added filter::MovingAverage::reset(), maintenance of code
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Jun 7, 2022
1 parent 04df702 commit f34f562
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
47 changes: 22 additions & 25 deletions src/modm/math/filter/moving_average.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstddef>
#include <cstdint>
#include <type_traits>

#include <span>
#include <algorithm>
#include <modm/math/utils/integer_traits.hpp>

namespace modm
{
Expand Down Expand Up @@ -57,16 +57,13 @@ namespace modm
template<typename T, std::size_t N>
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);
Expand All @@ -76,7 +73,7 @@ namespace modm
getValue() const;

private:
Index index;
least_uint<N> index{0};
T buffer[N];
T sum;
};
Expand All @@ -85,16 +82,21 @@ namespace modm

// ----------------------------------------------------------------------------
template<typename T, std::size_t N>
modm::filter::MovingAverage<T, N>::MovingAverage(const T& initialValue) :
index(0), sum(N * initialValue)
modm::filter::MovingAverage<T, N>::MovingAverage(const T& initialValue)
{
for (Index i = 0; i < N; ++i) {
buffer[i] = initialValue;
}
reset(initialValue);
}

// ----------------------------------------------------------------------------
template<typename T, std::size_t N>
void
modm::filter::MovingAverage<T, N>::reset(const T& input)
{
std::fill(std::begin(buffer), std::end(buffer), input);
sum = N * input;
}

// ----------------------------------------------------------------------------
// TODO implementierung für float anpassen
template<typename T, std::size_t N>
void
modm::filter::MovingAverage<T, N>::update(const T& input)
Expand All @@ -104,15 +106,11 @@ modm::filter::MovingAverage<T, N>::update(const T& input)

buffer[index] = input;

index++;
if (index >= N) {
if (++index >= N)
index = 0;
}
}

// -----------------------------------------------------------------------------


template<typename T, std::size_t N>
const T
modm::filter::MovingAverage<T, N>::getValue() const
Expand All @@ -122,4 +120,3 @@ modm::filter::MovingAverage<T, N>::getValue() const


#include "moving_average_float_impl.hpp"
#endif // MODM_MOVING_AVERAGE_HPP
45 changes: 20 additions & 25 deletions src/modm/math/filter/moving_average_float_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,24 @@
* 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 <numeric>

namespace modm
{
namespace filter{
template<std::size_t N>
class MovingAverage<double, N >
{
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);
Expand All @@ -43,7 +39,7 @@ namespace modm
getValue() const;

private:
Index index;
least_uint<N> index{0};
double buffer[N];
double sum;
};
Expand All @@ -53,31 +49,30 @@ namespace modm
//---------------------------------------------------------------------------------

template<std::size_t N>
modm::filter::MovingAverage<double, N>::MovingAverage(const double& initialValue) :
index(0), sum(N * initialValue)
modm::filter::MovingAverage<double, N>::MovingAverage(const double& initialValue)
{
for (Index i = 0; i < N; ++i) {
buffer[i] = initialValue;
}
reset(initialValue);
}

// ----------------------------------------------------------------------------
template<std::size_t N>
void
modm::filter::MovingAverage<double, N>::reset(const double& input)
{
std::fill(std::begin(buffer), std::end(buffer), input);
sum = N * input;
}

//---------------------------------------------------------------------------------
template<std::size_t N>
void
modm::filter::MovingAverage<double, N>::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);
}

// ------------------------------------------------------------------------------
Expand Down

0 comments on commit f34f562

Please sign in to comment.