21 #define GRT_DLL_EXPORTS
29 TimeDomainFeatures::TimeDomainFeatures(UINT bufferLength,UINT numFrames,UINT numDimensions,
bool offsetInput,
bool useMean,
bool useStdDev,
bool useEuclideanNorm,
bool useRMS){
31 classType =
"TimeDomainFeatures";
32 featureExtractionType = classType;
33 debugLog.setProceedingText(
"[DEBUG TimeDomainFeatures]");
34 errorLog.setProceedingText(
"[ERROR TimeDomainFeatures]");
35 warningLog.setProceedingText(
"[WARNING TimeDomainFeatures]");
37 init(bufferLength,numFrames,numDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
42 classType =
"TimeDomainFeatures";
43 featureExtractionType = classType;
44 debugLog.setProceedingText(
"[DEBUG TimeDomainFeatures]");
45 errorLog.setProceedingText(
"[ERROR TimeDomainFeatures]");
46 warningLog.setProceedingText(
"[WARNING TimeDomainFeatures]");
58 this->bufferLength = rhs.bufferLength;
59 this->numFrames = rhs.numFrames;
60 this->offsetInput = rhs.offsetInput;
61 this->useMean = rhs.useMean;
62 this->useStdDev = rhs.useStdDev;
63 this->useEuclideanNorm = rhs.useEuclideanNorm;
64 this->useRMS = rhs.useRMS;
65 this->dataBuffer = rhs.dataBuffer;
75 if( featureExtraction == NULL )
return false;
85 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
93 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
97 if( inputVector.size() != numInputDimensions ){
98 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
102 featureVector =
update( inputVector );
109 return init(bufferLength,numFrames,numInputDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
117 file.open(filename.c_str(), std::ios::out);
131 file.open(filename.c_str(), std::ios::in);
145 if( !file.is_open() ){
146 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
151 file <<
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" << std::endl;
155 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
160 file <<
"BufferLength: " << bufferLength << std::endl;
161 file <<
"NumFrames: " << numFrames << std::endl;
162 file <<
"OffsetInput: " << offsetInput << std::endl;
163 file <<
"UseMean: " << useMean << std::endl;
164 file <<
"UseStdDev: " << useStdDev << std::endl;
165 file <<
"UseEuclideanNorm: " << useEuclideanNorm << std::endl;
166 file <<
"UseRMS: " << useRMS << std::endl;
173 if( !file.is_open() ){
174 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
183 if( word !=
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" ){
184 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
189 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
195 if( word !=
"BufferLength:" ){
196 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read BufferLength header!" << std::endl;
199 file >> bufferLength;
203 if( word !=
"NumFrames:" ){
204 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumFrames header!" << std::endl;
211 if( word !=
"OffsetInput:" ){
212 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read OffsetInput header!" << std::endl;
219 if( word !=
"UseMean:" ){
220 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseMean header!" << std::endl;
227 if( word !=
"UseStdDev:" ){
228 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseStdDev header!" << std::endl;
235 if( word !=
"UseEuclideanNorm:" ){
236 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseEuclideanNorm header!" << std::endl;
239 file >> useEuclideanNorm;
243 if( word !=
"UseRMS:" ){
244 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseRMS header!" << std::endl;
250 return init(bufferLength,numFrames,numInputDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
253 bool TimeDomainFeatures::init(UINT bufferLength,UINT numFrames,UINT numDimensions,
bool offsetInput,
bool useMean,
bool useStdDev,
bool useEuclideanNorm,
bool useRMS){
257 if( numFrames > bufferLength ){
258 errorLog <<
"init(...) - The number of numFrames parameter can not be larger than the buffer length parameter!" << std::endl;
261 if( bufferLength % numFrames != 0 ){
262 errorLog <<
"init(...) - The buffer length parameter must be divisible with no remainders by the number of numFrames parameter!" << std::endl;
266 this->bufferLength = bufferLength;
267 this->numFrames = numFrames;
268 this->numInputDimensions = numDimensions;
269 this->offsetInput = offsetInput;
270 this->useMean = useMean;
271 this->useStdDev = useStdDev;
272 this->useEuclideanNorm = useEuclideanNorm;
273 this->useRMS = useRMS;
274 featureDataReady =
false;
277 numOutputDimensions = 0;
279 numOutputDimensions += numInputDimensions*numFrames;
282 numOutputDimensions += numInputDimensions*numFrames;
284 if( useEuclideanNorm ){
285 numOutputDimensions += numInputDimensions*numFrames;
288 numOutputDimensions += numInputDimensions*numFrames;
290 if( numOutputDimensions == 0 ){
291 errorLog <<
"init(...) - The numOutputDimensions is zero!" << std::endl;
296 featureVector.
resize(numOutputDimensions);
315 errorLog <<
"update(const VectorFloat &x) - Not Initialized!" << std::endl;
319 if( x.
getSize() != numInputDimensions ){
320 errorLog <<
"update(const VectorFloat &x)- The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.
getSize() <<
")!" << std::endl;
329 featureDataReady =
true;
330 }
else featureDataReady =
false;
332 MatrixFloat meanFeatures(numInputDimensions,numFrames);
333 MatrixFloat stdDevFeatures(numInputDimensions,numFrames);
334 MatrixFloat normFeatures(numInputDimensions,numFrames);
335 MatrixFloat rmsFeatures(numInputDimensions,numFrames);
339 for(UINT n=0; n<numInputDimensions; n++){
340 data[0][n] = dataBuffer[0][n];
341 for(UINT i=1; i<bufferLength; i++){
342 data[i][n] = dataBuffer[i][n]-dataBuffer[0][n];
346 for(UINT n=0; n<numInputDimensions; n++){
347 for(UINT i=0; i<bufferLength; i++){
348 data[i][n] = dataBuffer[i][n];
357 UINT frameSize = bufferLength / numFrames;
360 for(UINT n=0; n<numInputDimensions; n++){
363 for(UINT i=0; i<bufferLength; i++){
365 meanFeatures[n][frame] += data[i][n];
368 if( useEuclideanNorm )
369 normFeatures[n][frame] += data[i][n]*data[i][n];
373 rmsFeatures[n][frame] += data[i][n]*data[i][n];
375 if( ++index == frameSize ){
382 for(UINT j=0; j<numFrames; j++){
383 meanFeatures[n][j] /= frameSize;
390 for(UINT i=0; i<bufferLength; i++){
391 stdDevFeatures[n][frame] += (data[i][n]-meanFeatures[n][frame]) * (data[i][n]-meanFeatures[n][frame]);
392 if( ++index == frameSize ){
397 Float norm = frameSize>1 ? frameSize-1 : 1;
398 for(UINT j=0; j<numFrames; j++){
399 stdDevFeatures[n][j] = sqrt( stdDevFeatures[n][j]/norm );
404 if( useEuclideanNorm ){
405 for(UINT j=0; j<numFrames; j++){
406 normFeatures[n][j] = sqrt( normFeatures[n][j] );
412 for(UINT j=0; j<numFrames; j++){
413 rmsFeatures[n][j] = sqrt( rmsFeatures[n][j] / frameSize );
421 for(UINT n=0; n<numInputDimensions; n++){
422 for(UINT j=0; j<numFrames; j++){
424 featureVector[index++] = meanFeatures[n][j];
427 featureVector[index++] = stdDevFeatures[n][j];
429 if( useEuclideanNorm ){
430 featureVector[index++] = normFeatures[n][j];
433 featureVector[index++] = rmsFeatures[n][j];
438 return featureVector;
bool push_back(const T &value)
bool getBufferFilled() const
virtual bool saveModelToFile(std::string filename) const
virtual bool resize(const unsigned int size)
virtual bool computeFeatures(const VectorFloat &inputVector)
CircularBuffer< VectorFloat > getBufferData()
virtual bool loadModelFromFile(std::string filename)
VectorFloat update(Float x)
bool setAllValues(const T &value)
virtual ~TimeDomainFeatures()
This class implements the TimeDomainFeatures feature extraction module.
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
TimeDomainFeatures & operator=(const TimeDomainFeatures &rhs)
bool resize(const unsigned int newBufferSize)