2016-06-19 18:45:24 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// SDRdaemon - send I/Q samples read from a SDR device over the network via UDP //
|
|
|
|
// with FEC protection. GNUradio interface. //
|
|
|
|
// //
|
|
|
|
// http://stackoverflow.com/questions/10990618/calculate-rolling-moving-average-in-c //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 2016 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// 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 //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-07-14 14:31:26 -04:00
|
|
|
#ifndef _UTIL_MOVINGAVERAGE_H_
|
|
|
|
#define _UTIL_MOVINGAVERAGE_H_
|
2016-06-19 18:45:24 -04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
template <typename T, typename Total, int N>
|
2017-11-05 19:02:20 -05:00
|
|
|
class MovingAverageUtil
|
2016-06-19 18:45:24 -04:00
|
|
|
{
|
|
|
|
public:
|
2017-11-05 19:02:20 -05:00
|
|
|
MovingAverageUtil()
|
2018-02-03 01:17:49 -05:00
|
|
|
: m_num_samples(0), m_index(0), m_total(0)
|
2016-06-19 18:45:24 -04:00
|
|
|
{ }
|
|
|
|
|
2018-02-03 04:33:02 -05:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_num_samples = 0;
|
|
|
|
m_index = 0;
|
|
|
|
m_total = 0;
|
|
|
|
}
|
|
|
|
|
2016-06-19 18:45:24 -04:00
|
|
|
void operator()(T sample)
|
|
|
|
{
|
2018-02-03 01:17:49 -05:00
|
|
|
if (m_num_samples < N) // fill up
|
2016-06-19 18:45:24 -04:00
|
|
|
{
|
|
|
|
m_samples[m_num_samples++] = sample;
|
|
|
|
m_total += sample;
|
|
|
|
}
|
2018-02-03 01:17:49 -05:00
|
|
|
else // roll
|
2016-06-19 18:45:24 -04:00
|
|
|
{
|
2018-02-03 01:17:49 -05:00
|
|
|
T& oldest = m_samples[m_index];
|
2016-06-19 18:45:24 -04:00
|
|
|
m_total += sample - oldest;
|
|
|
|
oldest = sample;
|
2018-02-03 04:33:02 -05:00
|
|
|
m_index = (m_index + 1) % N;
|
2016-06-19 18:45:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-03 04:33:02 -05:00
|
|
|
double asDouble() const { return m_total / N; }
|
|
|
|
float asFloat() const { return m_total / N; }
|
2018-01-31 20:45:55 -05:00
|
|
|
operator T() const { return m_total / N; }
|
2016-06-19 18:45:24 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
T m_samples[N];
|
|
|
|
int m_num_samples;
|
2018-02-03 01:17:49 -05:00
|
|
|
unsigned int m_index;
|
2016-06-19 18:45:24 -04:00
|
|
|
Total m_total;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* GR_SDRDAEMONFEC_LIB_MOVINGAVERAGE_H_ */
|