stat_buffer.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #ifndef MATH_UTIL_STAT_BUFFER_H_
31 #define MATH_UTIL_STAT_BUFFER_H_
32 
33 #ifndef PI
34 #define PI 3.14159265358979
35 #endif
36 
37 #include <cmath>
38 #include <algorithm>
39 
41 
42 namespace swri_math_util
43 {
44  template <class T>
45  class StatBuffer: public GenRingBuffer<T>
46  {
47  public:
48  void modifyBufferSize(int NumElements) // moved from private 04/02/2008 JJC
49  {
50  this->realloc_mem(NumElements);
51  }
52 
54  {
55  this->modifyBufferSize(30);
56  }
57 
58  explicit StatBuffer(int NumElements)
59  {
60  this->modifyBufferSize(NumElements);
61  }
62 
64  {
65  this->modifyBufferSize(0);
66  }
67 
68  bool UpdateStats()
69  {
70  return this->computeStats();
71  }
72 
74  {
75  return this->computeDiffStats();
76  }
77 
79  {
80  return RetainedDiffStats.mean;
81  }
82 
84  {
86  }
87 
89  {
90  return RetainedDiffStats.min;
91  }
92 
94  {
95  return RetainedDiffStats.max;
96  }
97 
99  {
100  return RetainedStats.mean;
101  }
102 
103  // Computes the mean of the last NumToAvg items in the buffer
104  T reportPartialMean(int NumToAvg)
105  {
106  return this->computeMean(NumToAvg);
107  }
108 
110  {
111  return RetainedStats.median;
112  }
113 
115  {
116  return RetainedStats.min;
117  }
118 
120  {
121  return RetainedStats.max;
122  }
124  {
125  return RetainedStats.std;
126  }
127 
129  {
130  return RetainedStats.variance;
131  }
133  {
134  return RetainedStats;
135  }
136 
137  private:
138  typedef struct
139  {
140  T mean;
141  T min;
142  T max;
144  T std;
146  } StatPack;
147 
150 
151  T computeMean(int NumToAvg)
152  {
153  int NumElems = this->size();
154  if (NumElems <= 0) return (T)(0.0);
155  T CurVal = *this->getTail(0);
156  T sum = 0;
157  NumToAvg = std::min(NumToAvg, NumElems);
158  for (int i = 0; i < NumToAvg; ++i)
159  {
160  CurVal = *this->getTail(i);
161  sum += CurVal;
162  }
163  T mean = sum/((T)NumToAvg);
164  return mean;
165  }
166 
168  {
169  int NumElems = this->size();
170  if (NumElems <= 0) return false;
171 
172  T sum = 0;
173  T &min = RetainedStats.min;
174  T &max = RetainedStats.max;
175  T &mean = RetainedStats.mean;
176  T &median = RetainedStats.median;
177  T &std = RetainedStats.std;
178  T &var = RetainedStats.variance;
179 
180  T CurVal = *this->get(0);
181  sum += CurVal;
182  min = CurVal;
183  max = CurVal;
184  mean = CurVal;
185  median = CurVal;
186  std = 0;
187  var = std*std;
188 
189  // compute mean, min and max
190  for (int i = 1; i < NumElems; i++)
191  {
192  CurVal = *this->get(i);
193  sum += CurVal;
194  if (CurVal > max) max = CurVal;
195  else if (CurVal < min) min = CurVal;
196  }
197  mean = sum/((T)NumElems);
198  sum = 0;
199 
200  // compute
201  if (NumElems > 1)
202  {
203  T *vec1 = new T[NumElems]; // for median calculation
204  for (int i = 0; i < NumElems; i++)
205  {
206  CurVal = *this->get(i);
207  sum += (CurVal-mean)*(CurVal-mean);
208  vec1[i] = CurVal; // for median calculation
209  }
210  std=(T)sqrt(static_cast<double>(sum/(NumElems-1)));
211  var = std*std;
212 
213  // Compute Median
214  std::sort(vec1, vec1+NumElems); // first sort the data
215  if (NumElems % 2 == 0)
216  {
217  median = (vec1[NumElems/2-1] + vec1[NumElems/2])/2;
218  }
219  else
220  {
221  median = vec1[NumElems/2];
222  }
223  if (NumElems <= 1)
224  {
225  delete vec1;
226  }
227  else
228  {
229  delete [] vec1;
230  }
231  }
232 
233  return true;
234  }
235 
237  {
238  int NumElems = this->size();
239  if (NumElems <= 1) return false;
240 
241  T sum = 0;
242  T &min = RetainedDiffStats.min;
243  T &max = RetainedDiffStats.max;
244  T &mean = RetainedDiffStats.mean;
245  T &median = RetainedDiffStats.median;
246 
247  T *vec1 = new T[NumElems];
248 
249  T CurVal1 = *this->get(0);
250  T CurVal2 = *this->get(1);
251  T CVDiff = CurVal2-CurVal1;
252 
253  vec1[0] = CVDiff;
254 
255  sum += CVDiff;
256  min = CVDiff;
257  max = CVDiff;
258  mean = CVDiff;
259  median = CVDiff;
260  for (int i = 1; i < NumElems-1; i++)
261  {
262  CurVal1 = *this->get(i);
263  CurVal2 = *this->get(i+1);
264  CVDiff = CurVal2-CurVal1;
265  vec1[i] = CVDiff;
266  sum += CVDiff;
267  if (CVDiff > max) max = CVDiff;
268  else if (CVDiff < min) min = CVDiff;
269  }
270  mean = sum/((T)NumElems);
271 
272  NumElems--; // we put in one fewer than NumElems into the vector
273  std::sort(vec1, vec1+NumElems); // first sort the data
274  if (NumElems % 2 == 0)
275  {
276  median = (vec1[NumElems/2-1] + vec1[NumElems/2])/2;
277  }
278  else
279  {
280  median = vec1[NumElems/2];
281  }
282 
283  delete [] vec1;
284 
285  return true;
286  }
287  };
288 }
289 
290 
291 #endif // MATH_UTIL_STAT_BUFFER_H_
void modifyBufferSize(int NumElements)
Definition: stat_buffer.h:48
void realloc_mem(int NumElements2Alloc)
T reportPartialMean(int NumToAvg)
Definition: stat_buffer.h:104
StatBuffer(int NumElements)
Definition: stat_buffer.h:58
T computeMean(int NumToAvg)
Definition: stat_buffer.h:151


swri_math_util
Author(s): Marc Alban
autogenerated on Fri Jun 7 2019 22:05:41