26 RegisterFeatureExtractionModule< TimeDomainFeatures > TimeDomainFeatures::registerModule(
"TimeDomainFeatures");
28 TimeDomainFeatures::TimeDomainFeatures(UINT bufferLength,UINT numFrames,UINT numDimensions,
bool offsetInput,
bool useMean,
bool useStdDev,
bool useEuclideanNorm,
bool useRMS){
30 classType =
"TimeDomainFeatures";
31 featureExtractionType = classType;
32 debugLog.setProceedingText(
"[DEBUG TimeDomainFeatures]");
33 errorLog.setProceedingText(
"[ERROR TimeDomainFeatures]");
34 warningLog.setProceedingText(
"[WARNING TimeDomainFeatures]");
36 init(bufferLength,numFrames,numDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
41 classType =
"TimeDomainFeatures";
42 featureExtractionType = classType;
43 debugLog.setProceedingText(
"[DEBUG TimeDomainFeatures]");
44 errorLog.setProceedingText(
"[ERROR TimeDomainFeatures]");
45 warningLog.setProceedingText(
"[WARNING TimeDomainFeatures]");
57 this->bufferLength = rhs.bufferLength;
58 this->numFrames = rhs.numFrames;
59 this->offsetInput = rhs.offsetInput;
60 this->useMean = rhs.useMean;
61 this->useStdDev = rhs.useStdDev;
62 this->useEuclideanNorm = rhs.useEuclideanNorm;
63 this->useRMS = rhs.useRMS;
64 this->dataBuffer = rhs.dataBuffer;
74 if( featureExtraction == NULL )
return false;
84 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << endl;
92 errorLog <<
"computeFeatures(const VectorDouble &inputVector) - Not initialized!" << endl;
96 if( inputVector.size() != numInputDimensions ){
97 errorLog <<
"computeFeatures(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << endl;
101 featureVector =
update( inputVector );
108 return init(bufferLength,numFrames,numInputDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
116 file.open(filename.c_str(), std::ios::out);
130 file.open(filename.c_str(), std::ios::in);
144 if( !file.is_open() ){
145 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << endl;
150 file <<
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" << endl;
154 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl;
159 file <<
"BufferLength: " << bufferLength << endl;
160 file <<
"NumFrames: " << numFrames << endl;
161 file <<
"OffsetInput: " << offsetInput << endl;
162 file <<
"UseMean: " << useMean << endl;
163 file <<
"UseStdDev: " << useStdDev << endl;
164 file <<
"UseEuclideanNorm: " << useEuclideanNorm << endl;
165 file <<
"UseRMS: " << useRMS << endl;
172 if( !file.is_open() ){
173 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << endl;
182 if( word !=
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" ){
183 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << endl;
188 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl;
194 if( word !=
"BufferLength:" ){
195 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read BufferLength header!" << endl;
198 file >> bufferLength;
202 if( word !=
"NumFrames:" ){
203 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumFrames header!" << endl;
210 if( word !=
"OffsetInput:" ){
211 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read OffsetInput header!" << endl;
218 if( word !=
"UseMean:" ){
219 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseMean header!" << endl;
226 if( word !=
"UseStdDev:" ){
227 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseStdDev header!" << endl;
234 if( word !=
"UseEuclideanNorm:" ){
235 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseEuclideanNorm header!" << endl;
238 file >> useEuclideanNorm;
242 if( word !=
"UseRMS:" ){
243 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseRMS header!" << endl;
249 return init(bufferLength,numFrames,numInputDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
252 bool TimeDomainFeatures::init(UINT bufferLength,UINT numFrames,UINT numDimensions,
bool offsetInput,
bool useMean,
bool useStdDev,
bool useEuclideanNorm,
bool useRMS){
256 if( numFrames > bufferLength ){
257 errorLog <<
"init(...) - The number of numFrames parameter can not be larger than the buffer length parameter!" << endl;
260 if( bufferLength % numFrames != 0 ){
261 errorLog <<
"init(...) - The buffer length parameter must be divisible with no remainders by the number of numFrames parameter!" << endl;
265 this->bufferLength = bufferLength;
266 this->numFrames = numFrames;
267 this->numInputDimensions = numDimensions;
268 this->offsetInput = offsetInput;
269 this->useMean = useMean;
270 this->useStdDev = useStdDev;
271 this->useEuclideanNorm = useEuclideanNorm;
272 this->useRMS = useRMS;
273 featureDataReady =
false;
276 numOutputDimensions = 0;
278 numOutputDimensions += numInputDimensions*numFrames;
281 numOutputDimensions += numInputDimensions*numFrames;
283 if( useEuclideanNorm ){
284 numOutputDimensions += numInputDimensions*numFrames;
287 numOutputDimensions += numInputDimensions*numFrames;
289 if( numOutputDimensions == 0 ){
290 errorLog <<
"init(...) - The numOutputDimensions is zero!" << endl;
295 featureVector.resize(numOutputDimensions);
298 dataBuffer.
resize( bufferLength, VectorDouble(numInputDimensions,0) );
308 return update(VectorDouble(1,x));
314 errorLog <<
"update(const VectorDouble &x) - Not Initialized!" << endl;
315 return vector<double>();
318 if( x.size() != numInputDimensions ){
319 errorLog <<
"update(const VectorDouble &x)- The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.size() <<
")!" << endl;
320 return vector<double>();
328 featureDataReady =
true;
329 }
else featureDataReady =
false;
331 MatrixDouble meanFeatures(numInputDimensions,numFrames);
332 MatrixDouble stdDevFeatures(numInputDimensions,numFrames);
333 MatrixDouble normFeatures(numInputDimensions,numFrames);
338 for(UINT n=0; n<numInputDimensions; n++){
339 data[0][n] = dataBuffer[0][n];
340 for(UINT i=1; i<bufferLength; i++){
341 data[i][n] = dataBuffer[i][n]-dataBuffer[0][n];
345 for(UINT n=0; n<numInputDimensions; n++){
346 for(UINT i=0; i<bufferLength; i++){
347 data[i][n] = dataBuffer[i][n];
356 UINT frameSize = bufferLength / numFrames;
359 for(UINT n=0; n<numInputDimensions; n++){
362 for(UINT i=0; i<bufferLength; i++){
364 meanFeatures[n][frame] += data[i][n];
367 if( useEuclideanNorm )
368 normFeatures[n][frame] += data[i][n]*data[i][n];
372 rmsFeatures[n][frame] += data[i][n]*data[i][n];
374 if( ++index == frameSize ){
381 for(UINT j=0; j<numFrames; j++){
382 meanFeatures[n][j] /= frameSize;
389 for(UINT i=0; i<bufferLength; i++){
390 stdDevFeatures[n][frame] += (data[i][n]-meanFeatures[n][frame]) * (data[i][n]-meanFeatures[n][frame]);
391 if( ++index == frameSize ){
396 double norm = frameSize>1 ? frameSize-1 : 1;
397 for(UINT j=0; j<numFrames; j++){
398 stdDevFeatures[n][j] = sqrt( stdDevFeatures[n][j]/norm );
403 if( useEuclideanNorm ){
404 for(UINT j=0; j<numFrames; j++){
405 normFeatures[n][j] = sqrt( normFeatures[n][j] );
411 for(UINT j=0; j<numFrames; j++){
412 rmsFeatures[n][j] = sqrt( rmsFeatures[n][j] / frameSize );
420 for(UINT n=0; n<numInputDimensions; n++){
421 for(UINT j=0; j<numFrames; j++){
423 featureVector[index++] = meanFeatures[n][j];
426 featureVector[index++] = stdDevFeatures[n][j];
428 if( useEuclideanNorm ){
429 featureVector[index++] = normFeatures[n][j];
432 featureVector[index++] = rmsFeatures[n][j];
437 return featureVector;
bool setAllValues(const T &value)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
bool push_back(const T &value)
bool resize(const unsigned int newBufferSize)
TimeDomainFeatures & operator=(const TimeDomainFeatures &rhs)
virtual ~TimeDomainFeatures()
virtual bool loadModelFromFile(string filename)
virtual bool computeFeatures(const VectorDouble &inputVector)
CircularBuffer< VectorDouble > getBufferData()
bool getBufferFilled() const
virtual bool saveModelToFile(string filename) const
This class implements the TimeDomainFeatures feature extraction module.
VectorDouble update(double x)