SlidingAverage.hh
4.8 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* SlidingAverage.hh
*
* Created on: Dec 3, 2012
* Author: f.casimir
*/
#ifndef SlidingAverage_HH_
#define SlidingAverage_HH_
#include <iostream>
#include <list>
#include "Parameter.hh"
#include "ParamData.hh"
#include "DataTypeMath.hh"
#include "Operation.hh"
namespace AMDA {
namespace Parameters {
/**
* @class SlidingAverage
* @brief It is responsible to shift data of any ParamData type.
* @details This class implement the interface Operation.
*/
template<class TParamData>
class SlidingAverage : public Operation {
typedef typename TParamData::ElementType ElemType;
public:
/**
* @brief Constructor.
* @details Create the ParamData type of the input ParamData.
*/
SlidingAverage(Process& pProcess, TParamData& paramInput, TimeIntervalListSPtr pTimeIntervalList, double window)
: Operation(pProcess),
_paramInput(paramInput),
_paramOutput(new TParamData()),
_timeIntervalList(pTimeIntervalList),
_currentTimeInterval(_timeIntervalList->begin()),
_window(window), _nanVal() {
_paramDataOutput=_paramOutput;
}
/**
* @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
*/
void write(ParamDataIndexInfo &pParamDataIndexInfo) {
if ((pParamDataIndexInfo._nbDataToProcess > 0)) {
if (pParamDataIndexInfo._startIndex == 0) {
//init nan value (to init dimensions)
_nanVal = _paramInput.get(pParamDataIndexInfo._startIndex);
_nanVal << NotANumber();
}
}
for (unsigned int index = pParamDataIndexInfo._startIndex; index < pParamDataIndexInfo._startIndex
+ pParamDataIndexInfo._nbDataToProcess; index++) {
while (!_memTime.empty() && (_paramInput.getTime(index) > _memTime.front() + _window / 2)) {
double crtTime = _memTime.front();
if ((crtTime >= (*_currentTimeInterval)._startTime) && (crtTime <= (*_currentTimeInterval)._stopTime)) {
_paramOutput->pushTime(crtTime);
_paramOutput->getDataList().push_back(computeAverage(crtTime));
}
_memTime.pop_front();
}
//Cleanup unnecessary data
while (!_memTime.empty() && !_mem.empty() && (_mem.front().first < _memTime.front() - _window / 2)) {
_mem.pop_front();
}
_memTime.push_back(_paramInput.getTime(index));
_mem.push_back(std::make_pair(_paramInput.getTime(index), _paramInput.getDataList()[index]));
}
if (pParamDataIndexInfo._timeIntToProcessChanged || pParamDataIndexInfo._noMoreTimeInt) {
while (!_memTime.empty()) {
double crtTime = _memTime.front();
if ((crtTime >= (*_currentTimeInterval)._startTime) && (crtTime <= (*_currentTimeInterval)._stopTime)) {
_paramOutput->pushTime(crtTime);
_paramOutput->getDataList().push_back(computeAverage(crtTime));
}
_memTime.pop_front();
}
}
}
ElemType computeAverage(double crtTime) {
ElemType mean = _nanVal;
std::list<ElemType> averageDataList;
for (typename std::list<std::pair<double, ElemType>>::iterator it = _mem.begin(); it != _mem.end(); ++it) {
if (it->first >= crtTime - _window / 2. && it->first <= crtTime + _window / 2.) {
averageDataList.push_back(it->second);
}
}
if (!averageDataList.empty()) {
mean = average(averageDataList, _paramInput.getDim1(), _paramInput.getDim2());
}
return mean;
}
/**
* @overload Operation::reset(double pStartTime, double pTimeInt)
* @brief reset static data to process another TimeInterval
*/
virtual void reset() {
Operation::reset();
_memTime.clear();
_mem.clear();
if (_currentTimeInterval != _timeIntervalList->end())
++_currentTimeInterval;
}
private:
/**
* @brief It is the channel of data to shift.
*/
TParamData &_paramInput;
/**
* @brief It is the channel of the data shifted.
*/
TParamData *_paramOutput;
TimeIntervalListSPtr _timeIntervalList;
TimeIntervalList::iterator _currentTimeInterval;
/**
* @brief window size for sliding average.
*/
double _window;
ElemType _nanVal;
/**
* @brief keep some data in memory.
*/
std::list<std::pair<double, ElemType>> _mem;
/**
* @brief list of times over which sliding average must be computed
*/
std::list<double> _memTime;
};
} /* namespace Parameters */
} /* namespace AMDA */
#endif /* SlidingAverage_HH_ */