CARMA C++
RMSTracker.h
1 #ifndef LLRMSTRACKER_H
2 #define LLRMSTRACKER_H
3 // $Id: RMSTracker.h,v 1.3 2011/08/30 23:12:39 iws Exp $
4 
5 #include <carma/util/ScopedPthreadMutexLock.h>
7 
8 #include <boost/foreach.hpp>
9 
10 #include <deque>
11 #include <vector>
12 #include <iostream>
13 #include <algorithm>
14 
15 #include <math.h>
16 
17 namespace carma {
18 namespace linelength {
19 
20 class RMSTracker
21 {
22  public:
23  RMSTracker(unsigned int max = 500)
24  : _max(max)
25  , _Sx(0.0)
26  , _SSx(0.0)
27  , _Sxx(0.0)
28  , _Ssize(0.0)
29  , _values()
30  , _squares()
31  { /* empty */ };
32  ~RMSTracker() { /* empty */ };
33 
34  unsigned int size() const {
36  return static_cast<unsigned int>(_values.size());
37  };
38 
39  void add(const double value) {
41  const unsigned int vs = _values.size();
42 
43  if (isnan(value) || isinf(value)) {
44  carma::util::programLogWarnIfPossible("RMSTracker::add got inf or nan, ignore");
45  return;
46  }
47 
48  if (vs == _max) {
49  const double v = _values.back();
50  const double Sv = _squares.back();
51 
52  _Sx -= v;
53  _Sxx -= Sv;
54 
55  _values.pop_back();
56  _squares.pop_back();
57  }
58 
59  const double s = value * value;
60 
61  _Sx += value;
62  _Sxx += s;
63 
64  _values.push_front(value);
65  _squares.push_front(s);
66 
67  _Ssize = _values.size();
68 
69  const double tSx = _Sx / _Ssize;
70  _SSx = tSx * tSx;
71  };
72 
73  double rms() const {
75  return sqrt((_Sxx / _Ssize) - _SSx);
76  };
77 
78  // As defined in Numerical Recipes
79  // this is the RMS Mean or sqrt((1/N-1)(sum((xj-xm)^2))
80  double rmsMean() const {
82 
83  const double xm = _Sx / _Ssize;
84  double ssx = 0.;
85 
86  BOOST_FOREACH(const double value, _values) {
87  const double sxm = value - xm;
88  ssx += (sxm * sxm);
89  }
90 
91  const double ret = sqrt(ssx / (_Ssize - 1));
92  if (isnan(ret) || isinf(ret))
93  return 0.0;
94 
95  return ret;
96  }
97 
98  double mean() const {
100  return _Sx / static_cast<double>(_values.size());
101  }
102 
103  double median() const {
104  std::vector<double> v;
105 
106  {
108  v.resize(_values.size());
109  copy(_values.begin(), _values.end(), v.begin());
110  }
111 
112  if (v.empty())
113  return 0.0;
114 
115  sort(v.begin(), v.end());
116  return v.at(v.size() / 2);
117  }
118 
119  double instantaneous() const {
121  return _values.front();
122  }
123 
124  private:
125  const unsigned int _max;
126 
127  double _Sx; // running sum of v for v in values
128  double _SSx; // (_Sx / _Ssize) ** 2
129  double _Sxx; // running sum of (v ** 2) for v in values
130  double _Ssize; // values.size()
131 
132  std::deque<double> _values;
133  std::deque<double> _squares;
134 
135  mutable carma::util::PthreadMutex _lock;
136 };
137 
138 } // namespace carma::linelength
139 } // namespace carma
140 
141 #endif // LLRMSTRACKER_H
142 // vim: set expandtab ts=4 sts=4 sw=4:
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...
Definition: PthreadMutex.h:41
This is the interface file for extra APIs for program logging.