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.
movingaverage2d.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 _UTIL_MOVINGAVERAGE2D_H_
20 #define _UTIL_MOVINGAVERAGE2D_H_
21 
22 #include <algorithm>
23 
24 template<typename T>
26 {
27 public:
29 
31  {
32  if (m_data) {
33  delete[] m_data;
34  }
35 
36  if (m_sum) {
37  delete[] m_sum;
38  }
39  }
40 
41  void resize(unsigned int width, unsigned int depth)
42  {
43  if (width*depth > m_dataSize)
44  {
45  m_dataSize = width*depth;
46  if (m_data) {
47  delete[] m_data;
48  }
49  m_data = new T[m_dataSize];
50  }
51 
52  if (width > m_sumSize)
53  {
54  m_sumSize = width;
55  if (m_sum) {
56  delete[] m_sum;
57  }
58  m_sum = new T[m_sumSize];
59  }
60 
61  m_width = width;
62  m_depth = depth;
63 
64  std::fill(m_data, m_data+(m_width*m_depth), 0.0);
65  std::fill(m_sum, m_sum+m_width, 0.0);
66 
67  m_avgIndex = 0;
68  }
69 
70  T storeAndGetAvg(T v, unsigned int index)
71  {
72  if (m_depth <= 1)
73  {
74  return v;
75  }
76  else if (index < m_width)
77  {
78  T first = m_data[m_avgIndex*m_width+index];
79  m_sum[index] += (v - first);
80  m_data[m_avgIndex*m_width+index] = v;
81  return m_sum[index] / m_depth;
82  }
83  else
84  {
85  return 0;
86  }
87  }
88 
89  T storeAndGetSum(T v, unsigned int index)
90  {
91  if (m_depth == 1)
92  {
93  return v;
94  }
95  else if (index < m_width)
96  {
97  T first = m_data[m_avgIndex*m_width+index];
98  m_sum[index] += (v - first);
99  m_data[m_avgIndex*m_width+index] = v;
100  return m_sum[index];
101  }
102  else
103  {
104  return 0;
105  }
106  }
107 
108  void nextAverage() {
109  m_avgIndex = m_avgIndex == m_depth-1 ? 0 : m_avgIndex+1;
110  }
111 
112 private:
113  T *m_data;
114  T *m_sum;
115  unsigned int m_dataSize;
116  unsigned int m_sumSize;
117  unsigned int m_width;
118  unsigned int m_depth;
119  unsigned int m_avgIndex;
120 };
121 
122 
123 #endif
unsigned int m_sumSize
void resize(unsigned int width, unsigned int depth)
T storeAndGetSum(T v, unsigned int index)
unsigned int m_width
T storeAndGetAvg(T v, unsigned int index)
unsigned int m_dataSize
unsigned int m_avgIndex
unsigned int m_depth