Page MenuHomec4science

moving_average.hpp
No OneTemporary

File Metadata

Created
Sat, Nov 16, 23:00

moving_average.hpp

#ifndef SPECMICP_UTILS_MOVINGAVERAGE_HPP
#define SPECMICP_UTILS_MOVINGAVERAGE_HPP
#include "common.hpp"
namespace specmicp {
namespace utils {
//! \brief Exponential moving average
//!
//! https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
//!
//! @param alpha coefficient between 0 and 1
//! @param init initial value of the average
class ExponentialMovingAverage
{
public:
ExponentialMovingAverage(scalar_t alpha, scalar_t init):
m_alpha(alpha),
m_current_value(init)
{
}
//! \brief Add a point in the series, return current average value
scalar_t add_point(scalar_t value){
m_current_value = m_alpha*value + (1-m_alpha)*m_current_value;
return m_current_value;
}
//! \brief Return the current average value
scalar_t current_value() {
return m_current_value;
}
//! \brief Reset the average to 'value'
void reset(scalar_t value) {
m_current_value = value;
}
private:
scalar_t m_alpha;
scalar_t m_current_value;
};
} // end namespace utils
} // end namespace specmicp
#endif // SPECMICP_UTILS_MOVINGAVERAGE_HPP

Event Timeline