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.
fixedaverage2d.h
Go to the documentation of this file.
1 // Copyright (C) 2018 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // This program is free software; you can redistribute it and/or modify //
6 // it under the terms of the GNU General Public License as published by //
7 // the Free Software Foundation as version 3 of the License, or //
8 // (at your option) any later version. //
9 // //
10 // This program is distributed in the hope that it will be useful, //
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13 // GNU General Public License V3 for more details. //
14 // //
15 // You should have received a copy of the GNU General Public License //
16 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
18 
19 #ifndef SDRBASE_UTIL_FIXEDAVERAGE2D_H_
20 #define SDRBASE_UTIL_FIXEDAVERAGE2D_H_
21 
22 #include <algorithm>
23 
24 template<typename T>
26 {
27 public:
29 
31  {
32  if (m_sum) {
33  delete[] m_sum;
34  }
35  }
36 
37  void resize(unsigned int width, unsigned int size)
38  {
39  if (width > m_maxSize)
40  {
41  m_maxSize = width;
42  if (m_sum) {
43  delete[] m_sum;
44  }
45  m_sum = new T[m_maxSize];
46  }
47 
48  m_width = width;
49  m_size = size;
50 
51  std::fill(m_sum, m_sum+m_width, 0);
52  m_maxIndex = 0;
53  }
54 
55  bool storeAndGetAvg(T& avg, T v, unsigned int index)
56  {
57  if (m_size <= 1)
58  {
59  avg = v;
60  return true;
61  }
62 
63  m_sum[index] += v;
64 
65  if (m_maxIndex == m_size - 1)
66  {
67  avg = m_sum[index]/m_size;
68  return true;
69  }
70  else
71  {
72  return false;
73  }
74  }
75 
76  bool storeAndGetSum(T& sum, T v, unsigned int index)
77  {
78  if (m_size <= 1)
79  {
80  sum = v;
81  return true;
82  }
83 
84  m_sum[index] += v;
85 
86  if (m_maxIndex < m_size - 1)
87  {
88  sum = m_sum[index];
89  return true;
90  }
91  else
92  {
93  return false;
94  }
95  }
96 
97  bool nextAverage()
98  {
99  if (m_size <= 1) {
100  return true;
101  }
102 
103  if (m_maxIndex == m_size - 1)
104  {
105  m_maxIndex = 0;
106  std::fill(m_sum, m_sum+m_width, 0);
107  return true;
108  }
109  else
110  {
111  m_maxIndex++;
112  return false;
113  }
114  }
115 
116 private:
117  T *m_sum;
118  unsigned int m_maxSize;
119  unsigned int m_width;
120  unsigned int m_size;
121  unsigned int m_maxIndex;
122 };
123 
124 
125 
126 #endif /* SDRBASE_UTIL_FIXEDAVERAGE2D_H_ */
unsigned int m_maxSize
bool storeAndGetSum(T &sum, T v, unsigned int index)
bool storeAndGetAvg(T &avg, T v, unsigned int index)
void resize(unsigned int width, unsigned int size)
unsigned int m_width
unsigned int m_maxIndex
unsigned int m_size