SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
movingaverage.h
Go to the documentation of this file.
1 #ifndef INCLUDE_MOVINGAVERAGE_H
2 #define INCLUDE_MOVINGAVERAGE_H
3 
4 #include <stdint.h>
5 #include <vector>
6 #include <algorithm>
7 #include "dsp/dsptypes.h"
8 
9 template<typename Type> class MovingAverage {
10 public:
11  MovingAverage(int historySize, Type initial) : m_index(0)
12  {
13  resize(historySize, initial);
14  }
15 
16  void resize(int historySize, Type initial)
17  {
18  m_history.resize(historySize);
19  std::fill(m_history.begin(), m_history.end(), initial);
20  m_sum = (Type) m_history.size() * initial;
21  m_index = 0;
22  }
23 
24  void feed(Type value)
25  {
26  Type& oldest = m_history[m_index];
27  m_sum += value - oldest;
28  oldest = value;
29 
30  if (m_index < m_history.size() - 1) {
31  m_index++;
32  } else {
33  m_index = 0;
34  }
35  }
36 
37  void fill(Type value)
38  {
39  std::fill(m_history.begin(), m_history.end(), value);
40  m_sum = (Type) m_history.size() * value;
41  }
42 
43  Type average() const
44  {
45  return m_sum / (Type) m_history.size();
46  }
47 
48  Type sum() const
49  {
50  return m_sum;
51  }
52 
53  int historySize() const
54  {
55  return m_history.size();
56  }
57 
58 protected:
59  std::vector<Type> m_history;
60  Type m_sum;
62 };
63 
64 #endif // INCLUDE_MOVINGAVERAGE_H
void feed(Type value)
Definition: movingaverage.h:24
uint32_t m_index
Definition: movingaverage.h:61
Type sum() const
Definition: movingaverage.h:48
int historySize() const
Definition: movingaverage.h:53
void fill(Type value)
Definition: movingaverage.h:37
Type average() const
Definition: movingaverage.h:43
unsigned int uint32_t
Definition: rtptypes_win.h:46
std::vector< Type > m_history
Definition: movingaverage.h:59
MovingAverage(int historySize, Type initial)
Definition: movingaverage.h:11
void resize(int historySize, Type initial)
Definition: movingaverage.h:16