Blame view

src/ExternLib/SlidingAverage/SlidingAverage.hh 4.8 KB
fbe3c2bb   Benjamin Renard   First commit
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
/*
 * 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.
	 */
8e83433f   Benjamin Renard   Fix side effect w...
37
  SlidingAverage(Process& pProcess, TParamData& paramInput, TimeIntervalListSPtr pTimeIntervalList, double window)
fbe3c2bb   Benjamin Renard   First commit
38
39
40
	: Operation(pProcess),
	  _paramInput(paramInput),
	  _paramOutput(new TParamData()),
8e83433f   Benjamin Renard   Fix side effect w...
41
42
          _timeIntervalList(pTimeIntervalList),
          _currentTimeInterval(_timeIntervalList->begin()),
e24ecd40   Benjamin Renard   Rework of the sli...
43
	  _window(window), _nanVal() {
fbe3c2bb   Benjamin Renard   First commit
44
45
46
47
48
49
50
	  _paramDataOutput=_paramOutput;
  }

	/**
	 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
	 */
	void write(ParamDataIndexInfo &pParamDataIndexInfo) {
e24ecd40   Benjamin Renard   Rework of the sli...
51
52
53
54
55
56
57
58
59
60
61
62
            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();
8e83433f   Benjamin Renard   Fix side effect w...
63
64
65
66
                    if ((crtTime >= (*_currentTimeInterval)._startTime) && (crtTime <= (*_currentTimeInterval)._stopTime)) {
                        _paramOutput->pushTime(crtTime);
                        _paramOutput->getDataList().push_back(computeAverage(crtTime));
                    }
e24ecd40   Benjamin Renard   Rework of the sli...
67
68
69
70
71
72
73
74
75
76
77
78
79
                    _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();
8e83433f   Benjamin Renard   Fix side effect w...
80
81
82
83
                    if ((crtTime >= (*_currentTimeInterval)._startTime) && (crtTime <= (*_currentTimeInterval)._stopTime)) {
                        _paramOutput->pushTime(crtTime);
                        _paramOutput->getDataList().push_back(computeAverage(crtTime));
                    }
e24ecd40   Benjamin Renard   Rework of the sli...
84
85
86
87
                    _memTime.pop_front();
                }
            }
           
fbe3c2bb   Benjamin Renard   First commit
88
	}
e24ecd40   Benjamin Renard   Rework of the sli...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        
        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;
        }
fbe3c2bb   Benjamin Renard   First commit
103
104
105
106
107
108
109

	/**
	 * @overload Operation::reset(double pStartTime, double pTimeInt)
	 * @brief reset static data to process another TimeInterval
	 */
	virtual  void  reset() {
		Operation::reset();
fbe3c2bb   Benjamin Renard   First commit
110
111
		_memTime.clear();
		_mem.clear();
8e83433f   Benjamin Renard   Fix side effect w...
112
113
		if (_currentTimeInterval != _timeIntervalList->end())
			++_currentTimeInterval;
fbe3c2bb   Benjamin Renard   First commit
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	  }

private:


	/**
	 * @brief It is the channel of data to shift.
	 */
  TParamData &_paramInput;

	/**
	 * @brief It is the channel of the data shifted.
	 */
  TParamData *_paramOutput;

8e83433f   Benjamin Renard   Fix side effect w...
129
130
131
132
  TimeIntervalListSPtr _timeIntervalList;

  TimeIntervalList::iterator _currentTimeInterval;

fbe3c2bb   Benjamin Renard   First commit
133
  /**
e24ecd40   Benjamin Renard   Rework of the sli...
134
   * @brief window size for sliding average.
fbe3c2bb   Benjamin Renard   First commit
135
   */
e24ecd40   Benjamin Renard   Rework of the sli...
136
137
138
  double _window;
  
  ElemType _nanVal;
fbe3c2bb   Benjamin Renard   First commit
139
140

  /**
e24ecd40   Benjamin Renard   Rework of the sli...
141
   * @brief keep some data in memory.
fbe3c2bb   Benjamin Renard   First commit
142
   */
e24ecd40   Benjamin Renard   Rework of the sli...
143
  std::list<std::pair<double, ElemType>> _mem;
fbe3c2bb   Benjamin Renard   First commit
144
145
146


  /**
e24ecd40   Benjamin Renard   Rework of the sli...
147
   * @brief list of times over which sliding average must be computed
fbe3c2bb   Benjamin Renard   First commit
148
149
150
   */
  std::list<double> _memTime;

fbe3c2bb   Benjamin Renard   First commit
151
152
153
154
155
};

} /* namespace Parameters */
} /* namespace AMDA */
#endif /* SlidingAverage_HH_ */