1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-26 01:39:05 -05:00

Fixed moving average in utils to avoid index overflow

This commit is contained in:
f4exb 2018-02-03 07:17:49 +01:00
parent a171d82e4c
commit bcdaf91288

View File

@ -29,19 +29,20 @@ class MovingAverageUtil
{
public:
MovingAverageUtil()
: m_num_samples(0), m_total(0)
: m_num_samples(0), m_index(0), m_total(0)
{ }
void operator()(T sample)
{
if (m_num_samples < N)
if (m_num_samples < N) // fill up
{
m_samples[m_num_samples++] = sample;
m_total += sample;
}
else
else // roll
{
T& oldest = m_samples[m_num_samples++ % N];
T& oldest = m_samples[m_index];
m_index = (m_index + 1) % N;
m_total += sample - oldest;
oldest = sample;
}
@ -54,6 +55,7 @@ class MovingAverageUtil
private:
T m_samples[N];
int m_num_samples;
unsigned int m_index;
Total m_total;
};