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 // //
3 // http://stackoverflow.com/questions/10990618/calculate-rolling-moving-average-in-c //
4 // //
5 // Copyright (C) 2016 Edouard Griffiths, F4EXB //
6 // //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation as version 3 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // This program is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15 // GNU General Public License V3 for more details. //
16 // //
17 // You should have received a copy of the GNU General Public License //
18 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
20 
21 #ifndef _UTIL_MOVINGAVERAGE_H_
22 #define _UTIL_MOVINGAVERAGE_H_
23 
24 #include <algorithm>
25 
26 template <typename T, typename Total, int N>
28 {
29  public:
31  : m_num_samples(0), m_index(0), m_total(0)
32  { }
33 
34  void reset()
35  {
36  m_num_samples = 0;
37  m_index = 0;
38  m_total = 0;
39  }
40 
41  void operator()(T sample)
42  {
43  if (m_num_samples < N) // fill up
44  {
45  m_samples[m_num_samples++] = sample;
46  m_total += sample;
47  }
48  else // roll
49  {
50  T& oldest = m_samples[m_index];
51  m_total += sample - oldest;
52  oldest = sample;
53  m_index = (m_index + 1) % N;
54  }
55  }
56 
57  double asDouble() const { return ((double)m_total) / N; }
58  float asFloat() const { return ((float)m_total) / N; }
59  operator T() const { return m_total / N; }
60  T instantAverage() const { return m_total / (m_num_samples == 0 ? 1 : m_num_samples); }
61 
62  private:
63  T m_samples[N];
65  unsigned int m_index;
66  Total m_total;
67 };
68 
69 
70 template <typename T, typename Total>
72 {
73  public:
74  MovingAverageUtilVar(unsigned int size)
75  : m_num_samples(0), m_index(0), m_total(0)
76  {
77  m_samples.resize(size);
78  }
79 
80  void reset()
81  {
82  m_num_samples = 0;
83  m_index = 0;
84  m_total = 0;
85  }
86 
87  void resize(unsigned int size)
88  {
89  reset();
90  m_samples.resize(size);
91  }
92 
93  unsigned int size() const
94  {
95  return m_samples.size();
96  }
97 
98  void operator()(T sample)
99  {
100  if (m_num_samples < m_samples.size()) // fill up
101  {
102  m_samples[m_num_samples++] = sample;
103  m_total += sample;
104  }
105  else // roll
106  {
107  T& oldest = m_samples[m_index];
108  m_total += sample - oldest;
109  oldest = sample;
110  m_index = (m_index + 1) % m_samples.size();
111  }
112  }
113 
114  double asDouble() const { return ((double)m_total) / m_samples.size(); }
115  float asFloat() const { return ((float)m_total) / m_samples.size(); }
116  operator T() const { return m_total / m_samples.size(); }
117 
118  private:
119  std::vector<T> m_samples;
120  unsigned int m_num_samples;
121  unsigned int m_index;
122  Total m_total;
123 };
124 
125 
126 #endif /* _UTIL_MOVINGAVERAGE_H_ */
unsigned int m_index
Definition: movingaverage.h:65
unsigned int size() const
Definition: movingaverage.h:93
double asDouble() const
void resize(unsigned int size)
Definition: movingaverage.h:87
void operator()(T sample)
Definition: movingaverage.h:41
unsigned int m_index
void operator()(T sample)
Definition: movingaverage.h:98
float asFloat() const
Definition: movingaverage.h:58
float asFloat() const
unsigned int m_num_samples
MovingAverageUtilVar(unsigned int size)
Definition: movingaverage.h:74
std::vector< T > m_samples
double asDouble() const
Definition: movingaverage.h:57
T instantAverage() const
Definition: movingaverage.h:60