blob: e7d9508fcb0d9b5405a069be8130e01d9aa71681 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#ifndef C_3D_POINT_SMOOTHER_H_
#define C_3D_POINT_SMOOTHER_H_
#include <iostream>
#include <list>
#include <iostream>
#include <iomanip>
using namespace std;
typedef enum
{ // k-1
SimpMovingAvg, //S(t) = (1/k)*sum(X(t-n)) = S(t-1) + (X(t) - X(t-k))/k
// n=0
ExpMovingAvg //S(0) = X(0)
//S(t) = a*X(t) + (1 - a)*S(t-1) = S(t-1) + a*(X(t) - S(t-1))
} SmoothingMethod;
template<typename T>
class C_Smoother
{
public:
C_Smoother(T f_ZeroVal) :
m_Method(SimpMovingAvg),
m_iWindowSize(2),
m_iSampCount(0),
m_ZeroVal(f_ZeroVal),
m_dExponent(0.0) {}
void SetSimpleMovingAverage(int f_iWindowSize);
void SetExponentialMovingAverage(double f_dExponent);
T Smooth(const T &f_Xt);
private:
SmoothingMethod m_Method;
int m_iWindowSize, //window size for SimpMovingAvg
m_iSampCount; //number of processed samples
list<T> m_SampWindow;
double m_dExponent; //exponent for ExpMovingAvg
T m_ZeroVal, //the value that is supposed to be zero
m_St; //last smoothed statistic S(t)
T SmoothSample_SimpMovingAvg(const T &f_Xt);
T SmoothSample_ExpMovingAvg(const T &f_Xt);
};
template <typename T>
void C_Smoother<T>::SetSimpleMovingAverage(int f_iWindowSize)
{
//reset smoothing stuff for Simple moving average smoothing
m_Method = SimpMovingAvg;
m_iWindowSize = f_iWindowSize;
m_iSampCount = 0;
m_St = m_ZeroVal;
m_SampWindow.clear();
}
template <typename T>
void C_Smoother<T>::SetExponentialMovingAverage(double f_dExponent)
{
m_Method = ExpMovingAvg;
m_dExponent = f_dExponent;
m_iSampCount = 0;
m_St = m_ZeroVal;
}
template <typename T>
T C_Smoother<T>::SmoothSample_SimpMovingAvg(const T &f_Xt)
{
//add sample to the window
m_SampWindow.push_front(f_Xt);
if (m_iSampCount < m_iWindowSize)
{
//we don't have enough cmopute the average of the points currently in the window
T l_sum = m_ZeroVal;
for (std::list<T>::iterator it = m_SampWindow.begin(); it != m_SampWindow.end(); it++)
l_sum += *it;
return l_sum / (double)m_SampWindow.size();
}
else
{
//there are mote than m_iWindowSize samples, incrementally update smoothed sample
T l_Xtk = m_SampWindow.back(); //pop the last point from the (window) list
m_SampWindow.pop_back();
return m_St + ((f_Xt - l_Xtk)/(double)m_iWindowSize);
}
}
template <typename T>
T C_Smoother<T>::SmoothSample_ExpMovingAvg(const T &f_Xt)
{
if (m_iSampCount == 0)
return f_Xt;
else
return m_St + m_dExponent * (f_Xt - m_St);
}
template <typename T>
T C_Smoother<T>::Smooth(const T &f_Xt)
{
switch(m_Method)
{
case SimpMovingAvg:
m_St = SmoothSample_SimpMovingAvg(f_Xt);
break;
case ExpMovingAvg:
m_St = SmoothSample_ExpMovingAvg(f_Xt);
break;
}
m_iSampCount++;
return m_St;
}
#endif //C_3D_POINT_SMOOTHER_H_
|