SlidingAverage.hh 3.05 KB
/*
 * 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_ */