2023-11-19 00:43:20 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
|
|
|
|
// written by Christian Daniel //
|
|
|
|
// Copyright (C) 2015-2018 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
2014-05-18 11:52:39 -04:00
|
|
|
#ifndef INCLUDE_MOVINGAVERAGE_H
|
|
|
|
#define INCLUDE_MOVINGAVERAGE_H
|
|
|
|
|
2016-08-25 20:01:29 -04:00
|
|
|
#include <stdint.h>
|
2014-05-18 11:52:39 -04:00
|
|
|
#include <vector>
|
2017-01-15 05:26:11 -05:00
|
|
|
#include <algorithm>
|
2014-05-18 11:52:39 -04:00
|
|
|
#include "dsp/dsptypes.h"
|
|
|
|
|
2017-02-27 18:58:26 -05:00
|
|
|
template<typename Type> class MovingAverage {
|
2014-05-18 11:52:39 -04:00
|
|
|
public:
|
2018-04-27 14:40:17 -04:00
|
|
|
MovingAverage(int historySize, Type initial) : m_index(0)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2017-05-11 12:39:00 -04:00
|
|
|
resize(historySize, initial);
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 03:28:57 -04:00
|
|
|
void resize(int historySize, Type initial)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
|
|
|
m_history.resize(historySize);
|
2017-01-15 05:26:11 -05:00
|
|
|
std::fill(m_history.begin(), m_history.end(), initial);
|
2016-08-25 20:01:29 -04:00
|
|
|
m_sum = (Type) m_history.size() * initial;
|
|
|
|
m_index = 0;
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 03:28:57 -04:00
|
|
|
void feed(Type value)
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2016-08-25 20:01:29 -04:00
|
|
|
Type& oldest = m_history[m_index];
|
|
|
|
m_sum += value - oldest;
|
|
|
|
oldest = value;
|
|
|
|
|
2018-02-15 10:35:43 -05:00
|
|
|
if (m_index < m_history.size() - 1) {
|
2017-01-15 05:26:11 -05:00
|
|
|
m_index++;
|
|
|
|
} else {
|
|
|
|
m_index = 0;
|
|
|
|
}
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 03:28:57 -04:00
|
|
|
void fill(Type value)
|
2015-05-12 06:12:13 -04:00
|
|
|
{
|
2017-01-15 05:26:11 -05:00
|
|
|
std::fill(m_history.begin(), m_history.end(), value);
|
2016-08-25 20:01:29 -04:00
|
|
|
m_sum = (Type) m_history.size() * value;
|
2015-05-12 06:12:13 -04:00
|
|
|
}
|
|
|
|
|
2015-06-20 03:28:57 -04:00
|
|
|
Type average() const
|
2014-05-18 11:52:39 -04:00
|
|
|
{
|
2016-08-25 20:01:29 -04:00
|
|
|
return m_sum / (Type) m_history.size();
|
2014-05-18 11:52:39 -04:00
|
|
|
}
|
|
|
|
|
2015-09-13 05:56:14 -04:00
|
|
|
Type sum() const
|
|
|
|
{
|
|
|
|
return m_sum;
|
|
|
|
}
|
|
|
|
|
2017-07-25 15:21:48 -04:00
|
|
|
int historySize() const
|
|
|
|
{
|
|
|
|
return m_history.size();
|
|
|
|
}
|
|
|
|
|
2014-05-18 11:52:39 -04:00
|
|
|
protected:
|
2015-06-20 03:28:57 -04:00
|
|
|
std::vector<Type> m_history;
|
|
|
|
Type m_sum;
|
2016-08-25 20:01:29 -04:00
|
|
|
uint32_t m_index;
|
2014-05-18 11:52:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDE_MOVINGAVERAGE_H
|