1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-07-02 00:45:23 -04:00

Fixed MovingAverage uint to uint32_t and template type conversions

This commit is contained in:
f4exb 2016-08-26 02:01:29 +02:00
parent b601fae2e7
commit 9d5e5c76c7

View File

@ -1,6 +1,7 @@
#ifndef INCLUDE_MOVINGAVERAGE_H #ifndef INCLUDE_MOVINGAVERAGE_H
#define INCLUDE_MOVINGAVERAGE_H #define INCLUDE_MOVINGAVERAGE_H
#include <stdint.h>
#include <vector> #include <vector>
#include "dsp/dsptypes.h" #include "dsp/dsptypes.h"
@ -9,46 +10,54 @@ public:
MovingAverage() : MovingAverage() :
m_history(), m_history(),
m_sum(0), m_sum(0),
m_ptr(0) m_index(0)
{ {
} }
MovingAverage(int historySize, Type initial) : MovingAverage(int historySize, Type initial) :
m_history(historySize, initial), m_history(historySize, initial),
m_sum((float) historySize * initial), m_sum((float) historySize * initial),
m_ptr(0) m_index(0)
{ {
} }
void resize(int historySize, Type initial) void resize(int historySize, Type initial)
{ {
m_history.resize(historySize); m_history.resize(historySize);
for(size_t i = 0; i < m_history.size(); i++)
for(size_t i = 0; i < m_history.size(); i++) {
m_history[i] = initial; m_history[i] = initial;
m_sum = (float) m_history.size() * initial; }
m_ptr = 0;
m_sum = (Type) m_history.size() * initial;
m_index = 0;
} }
void feed(Type value) void feed(Type value)
{ {
m_sum -= m_history[m_ptr]; Type& oldest = m_history[m_index];
m_history[m_ptr] = value; m_sum += value - oldest;
m_sum += value; oldest = value;
m_ptr++;
if(m_ptr >= m_history.size()) m_index++;
m_ptr = 0;
if(m_index >= m_history.size()) {
m_index = 0;
}
} }
void fill(Type value) void fill(Type value)
{ {
for(size_t i = 0; i < m_history.size(); i++) for(size_t i = 0; i < m_history.size(); i++) {
m_history[i] = value; m_history[i] = value;
m_sum = (float) m_history.size() * value; }
m_sum = (Type) m_history.size() * value;
} }
Type average() const Type average() const
{ {
return m_sum / (float) m_history.size(); return m_sum / (Type) m_history.size();
} }
Type sum() const Type sum() const
@ -59,7 +68,7 @@ public:
protected: protected:
std::vector<Type> m_history; std::vector<Type> m_history;
Type m_sum; Type m_sum;
uint m_ptr; uint32_t m_index;
}; };
#endif // INCLUDE_MOVINGAVERAGE_H #endif // INCLUDE_MOVINGAVERAGE_H