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!" << std::endl;
92 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
96 if( inputVector.size() != numInputDimensions ){
97 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::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!" << std::endl;
150 file <<
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" << std::endl;
154 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
159 file <<
"BufferLength: " << bufferLength << std::endl;
160 file <<
"NumFrames: " << numFrames << std::endl;
161 file <<
"OffsetInput: " << offsetInput << std::endl;
162 file <<
"UseMean: " << useMean << std::endl;
163 file <<
"UseStdDev: " << useStdDev << std::endl;
164 file <<
"UseEuclideanNorm: " << useEuclideanNorm << std::endl;
165 file <<
"UseRMS: " << useRMS << std::endl;
172 if( !file.is_open() ){
173 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
182 if( word !=
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" ){
183 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
188 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
194 if( word !=
"BufferLength:" ){
195 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read BufferLength header!" << std::endl;
198 file >> bufferLength;
202 if( word !=
"NumFrames:" ){
203 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumFrames header!" << std::endl;
210 if( word !=
"OffsetInput:" ){
211 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read OffsetInput header!" << std::endl;
218 if( word !=
"UseMean:" ){
219 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseMean header!" << std::endl;
226 if( word !=
"UseStdDev:" ){
227 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseStdDev header!" << std::endl;
234 if( word !=
"UseEuclideanNorm:" ){
235 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseEuclideanNorm header!" << std::endl;
238 file >> useEuclideanNorm;
242 if( word !=
"UseRMS:" ){
243 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read UseRMS header!" << std::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!" << std::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!" << std::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!" << std::endl;
295 featureVector.
resize(numOutputDimensions);
314 errorLog <<
"update(const VectorFloat &x) - Not Initialized!" << std::endl;
318 if( x.
getSize() != numInputDimensions ){
319 errorLog <<
"update(const VectorFloat &x)- The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.
getSize() <<
")!" << std::endl;
328 featureDataReady =
true;
329 }
else featureDataReady =
false;
331 MatrixFloat meanFeatures(numInputDimensions,numFrames);
332 MatrixFloat stdDevFeatures(numInputDimensions,numFrames);
333 MatrixFloat normFeatures(numInputDimensions,numFrames);
334 MatrixFloat rmsFeatures(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 Float 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 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)
unsigned int getSize() const
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)