Blame view

src/ExternLib/SlidingAverage/SlidingAverage.hh 3.05 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
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
/*
 * 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, double second)
	: Operation(pProcess),
	  _paramInput(paramInput),
	  _paramOutput(new TParamData()),
	  _second(second),
	  _startTime(0.0),
	  _index(0) {
	  _paramDataOutput=_paramOutput;
  }

	/**
	 * @overload Operation::write(ParamDataIndexInfo &pParamDataIndexInfo)
	 */
	void write(ParamDataIndexInfo &pParamDataIndexInfo) {
		unsigned int _index = pParamDataIndexInfo._startIndex;
		//first call
		if (_startTime == 0.0) {
			_startTime = _paramInput.getTime(_index);
		}
		if (pParamDataIndexInfo._nbDataToProcess > 0) {
			for (;_index < pParamDataIndexInfo._startIndex
									+ pParamDataIndexInfo._nbDataToProcess;
					_index++) {
				if (_paramInput.getTime(_index) - _startTime > _second / 2) {
						_paramOutput->pushTime(_memTime.front());
						_memTime.pop_front();
					_paramOutput->getDataList().push_back(average(_mem));
				}
				ElemType lVal = _paramInput.getDataList()[_index];
				_mem.push_back(lVal);
				_memTime.push_back(_paramInput.getTime(_index));
				if (_paramInput.getTime(_index) - _startTime >= _second) {
					_mem.pop_front();
				}


			}
		}
		//end of data
		else if(!_mem. empty()  ){
			//_mem.pop_front();
			for(unsigned int i = _mem.size(); i > 0; --i) {
				if(!_memTime.empty()) {
					_paramOutput->pushTime(_memTime.front());
					_memTime.pop_front();
					_paramOutput->getDataList().push_back(average(_mem));
				}
				++_index;
				_mem.pop_front();
			}
		}
	}

	/**
	 * @overload Operation::reset(double pStartTime, double pTimeInt)
	 * @brief reset static data to process another TimeInterval
	 */
	virtual  void  reset() {
		Operation::reset();
		_startTime = 0.0;
		_index = 0;
		_memTime.clear();
		_mem.clear();
	  }

private:


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

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

  /**
   * @brief length of average in second.
   */
  double _second;

  /**
   * @brief last start time of mean compute .
   */
  double _startTime;

  /**
   * @brief memory data for compute average.
   */
  std::list<ElemType> _mem;


  /**
   * @brief memory time for put data in past.
   */
  std::list<double> _memTime;

  /**
   * @brief index of current read data.
   */
	unsigned int _index;

};

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