RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
runningstat.cpp
Go to the documentation of this file.
1 #include "runningstat.h"
2 
3 // class constructor <- of all the comments this code could use, this is the one
4 // we chose...
6  : m_n(0)
7 {
8 }
9 
11 {
12 }
13 
14 void
16 {
17  m_n = 0;
18 }
19 
20 void
21 RunningStat::push(double x)
22 {
23  // double p = RT::System::getInstance()->getPeriod(); // gets period in
24  // nanoseconds
25  m_n++;
26 
27  if (m_n == 1) {
28  m_oldM = x;
29  m_oldS = 0.0;
30  } else {
31  m_newM = m_oldM + (x - m_oldM) / m_n;
32  // m_newS = (m_oldS*(m_n-1)/m_n) + (x-m_newM)*(x-m_newM)/(m_n-1); // Sample
33  // variance
34  m_newS = (m_n - 1.0) / m_n * m_oldS + (1.0 / m_n) * (x) * (x); // Variance
35  m_oldM = m_newM;
36  m_oldS = m_newS;
37  }
38 }
39 
40 int
42 {
43  return m_n;
44 }
45 
46 double
47 RunningStat::mean() const
48 {
49  return (m_n > 0) ? m_newM : 0.0;
50 }
51 
52 double
53 RunningStat::var() const
54 {
55  return ((m_n > 1) ? m_newS : 0.0);
56 }
57 
58 double
59 RunningStat::std() const
60 {
61  return sqrt(var());
62 }
double m_oldS
Definition: runningstat.h:23
void clear()
Definition: runningstat.cpp:15
double m_oldM
Definition: runningstat.h:23
double mean() const
Definition: runningstat.cpp:47
void push(double x)
Definition: runningstat.cpp:21
complex sqrt(const complex _z)
Definition: complex.h:148
double std() const
Definition: runningstat.cpp:59
double m_newM
Definition: runningstat.h:23
double m_newS
Definition: runningstat.h:23
int numValues() const
Definition: runningstat.cpp:41
double var() const
Definition: runningstat.cpp:53