29 classType =
"Derivative";
30 preProcessingType = classType;
31 debugLog.setProceedingText(
"[DEBUG Derivative]");
32 errorLog.setProceedingText(
"[ERROR Derivative]");
33 warningLog.setProceedingText(
"[WARNING Derivative]");
34 if( derivativeOrder == FIRST_DERIVATIVE || derivativeOrder == SECOND_DERIVATIVE ){
35 init(derivativeOrder,delta,numDimensions,filterData,filterSize);
49 classType =
"Derivative";
50 preProcessingType = classType;
51 debugLog.setProceedingText(
"[DEBUG Derivative]");
52 errorLog.setProceedingText(
"[ERROR Derivative]");
53 warningLog.setProceedingText(
"[WARNING Derivative]");
78 if( preProcessing == NULL )
return false;
97 errorLog <<
"clone(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
105 errorLog <<
"process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
109 if( inputVector.
getSize() != numInputDimensions ){
110 errorLog <<
"process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
116 if( processedData.size() == numOutputDimensions )
return true;
128 errorLog <<
"saveModelToFile(string filename) - The DeadZone has not been initialized" << std::endl;
133 file.open(filename.c_str(), std::ios::out);
147 if( !file.is_open() ){
148 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
152 file <<
"GRT_DERIVATIVE_FILE_V1.0" << std::endl;
154 file <<
"NumInputDimensions: " << numInputDimensions << std::endl;
155 file <<
"NumOutputDimensions: " << numOutputDimensions << std::endl;
157 file <<
"FilterSize: " <<
filterSize << std::endl;
158 file <<
"Delta: " <<
delta << std::endl;
159 file <<
"FilterData: " <<
filterData << std::endl;
167 file.open(filename.c_str(), std::ios::in);
182 if( !file.is_open() ){
183 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
192 if( word !=
"GRT_DERIVATIVE_FILE_V1.0" ){
193 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
199 if( word !=
"NumInputDimensions:" ){
200 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
203 file >> numInputDimensions;
207 if( word !=
"NumOutputDimensions:" ){
208 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
211 file >> numOutputDimensions;
215 if( word !=
"DerivativeOrder:" ){
216 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read DerivativeOrder header!" << std::endl;
223 if( word !=
"FilterSize:" ){
224 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FilterSize header!" << std::endl;
231 if( word !=
"Delta:" ){
232 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read Delta header!" << std::endl;
239 if( word !=
"FilterData:" ){
240 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FilterData header!" << std::endl;
246 return init(derivativeOrder,delta,numInputDimensions,filterData,filterSize);
249 bool Derivative::init(UINT derivativeOrder,Float delta,UINT numDimensions,
bool filterData,UINT filterSize){
253 if( derivativeOrder != FIRST_DERIVATIVE && derivativeOrder != SECOND_DERIVATIVE ){
254 errorLog <<
"init(UINT derivativeOrder,Float delta,UINT numDimensions,bool filterData,UINT filterSize) - Unknown derivativeOrder!" << std::endl;
258 if( numDimensions == 0 ){
259 errorLog <<
"init(UINT derivativeOrder,Float delta,UINT numDimensions,bool filterData,UINT filterSize) - NumDimensions must be greater than 0!" << std::endl;
264 errorLog <<
"init(UINT derivativeOrder,Float delta,UINT numDimensions,bool filterData,UINT filterSize) - Delta must be greater than 0!" << std::endl;
268 if( filterSize == 0 ){
269 errorLog <<
"init(UINT derivativeOrder,Float delta,UINT numDimensions,bool filterData,UINT filterSize) - FilterSize must be greater than zero!" << std::endl;
275 this->numInputDimensions = numDimensions;
276 this->numOutputDimensions = numDimensions;
284 processedData.clear();
285 processedData.
resize(numDimensions,0);
292 if( numInputDimensions != 1 ){
293 errorLog <<
"computeDerivative(const Float x) - The Number Of Input Dimensions is not 1! NumInputDimensions: " << numInputDimensions << std::endl;
299 if( y.size() == 0 )
return 0 ;
307 errorLog <<
"computeDerivative(const VectorFloat &x) - Not Initialized!" << std::endl;
311 if( x.size() != numInputDimensions ){
312 errorLog <<
"computeDerivative(const VectorFloat &x) - The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.size() <<
")!" << std::endl;
321 for(UINT n=0; n<numInputDimensions; n++){
322 processedData[n] = (y[n]-
yy[n])/
delta;
328 for(UINT n=0; n<numInputDimensions; n++){
329 tmp = processedData[n];
330 processedData[n] = (processedData[n]-
yyy[n])/
delta;
335 return processedData;
339 if( derivativeOrder == FIRST_DERIVATIVE || derivativeOrder == SECOND_DERIVATIVE ){
344 errorLog <<
"setDerivativeOrder(UINT derivativeOrder) - Unkown derivativeOrder" << std::endl;
349 if( filterSize > 0 ){
354 errorLog <<
"setFilterSize(UINT filterSize) - FilterSize must be greater than zero!" << std::endl;
366 switch( derivativeOrder ){
368 return processedData[0];
370 case( FIRST_DERIVATIVE ):
373 case( SECOND_DERIVATIVE ):
377 warningLog <<
"getDerivative(UINT derivativeOrder) - Unkown derivativeOrder: " << derivativeOrder << std::endl;
386 switch( derivativeOrder ){
388 return processedData;
390 case( FIRST_DERIVATIVE ):
393 case( SECOND_DERIVATIVE ):
397 warningLog <<
"getDerivative(UINT derivativeOrder) - Unkown derivativeOrder: " << derivativeOrder << std::endl;
bool init(UINT filterSize, UINT numDimensions)
UINT filterSize
The size of the filter used to filter the input data before the derivative is computed.
virtual bool resize(const unsigned int size)
bool setDerivativeOrder(UINT derivativeOrder)
VectorFloat yy
A buffer holding the previous input value(s)
Float getDerivative(UINT derivativeOrder=0)
VectorFloat yyy
A buffer holding the previous first derivative values.
Float computeDerivative(const Float x)
unsigned int getSize() const
virtual bool loadModelFromFile(std::string filename)
Derivative & operator=(const Derivative &rhs)
Float filter(const Float x)
VectorFloat getDerivatives(UINT derivativeOrder=0)
std::string getPreProcessingType() const
virtual bool process(const VectorFloat &inputVector)
bool filterData
Flags if the input data should be filtered before the derivative is computed.
Float delta
The estimated time between sensor samples.
virtual bool saveModelToFile(std::string filename) const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
bool enableFiltering(bool filterData)
UINT derivativeOrder
The order of the derivative that will be computed (either FIRST_DERIVATIVE or SECOND_DERIVATIVE) ...
Derivative(UINT derivativeOrder=FIRST_DERIVATIVE, Float delta=1, UINT numDimensions=1, bool filterData=true, UINT filterSize=3)
bool setFilterSize(UINT filterSize)
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
The Derivative class computes either the first or second order derivative of the input signal...
MovingAverageFilter filter
The filter used to low pass filter the input data.