21 #define GRT_DLL_EXPORTS 27 std::string TimeDomainFeatures::id =
"TimeDomainFeatures";
33 TimeDomainFeatures::TimeDomainFeatures(
const UINT bufferLength,
const UINT numFrames,
const UINT numDimensions,
const bool offsetInput,
const bool useMean,
const bool useStdDev,
const bool useEuclideanNorm,
const bool useRMS) :
FeatureExtraction(
TimeDomainFeatures::getId() )
35 init(bufferLength,numFrames,numDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
50 this->bufferLength = rhs.bufferLength;
51 this->numFrames = rhs.numFrames;
52 this->offsetInput = rhs.offsetInput;
53 this->useMean = rhs.useMean;
54 this->useStdDev = rhs.useStdDev;
55 this->useEuclideanNorm = rhs.useEuclideanNorm;
56 this->useRMS = rhs.useRMS;
57 this->dataBuffer = rhs.dataBuffer;
67 if( featureExtraction == NULL )
return false;
69 if( this->
getId() == featureExtraction->
getId() ){
77 errorLog <<
"deepCopyFrom(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
85 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
89 if( inputVector.
getSize() != numInputDimensions ){
90 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
94 featureVector =
update( inputVector );
101 return init(bufferLength,numFrames,numInputDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
108 if( !file.is_open() ){
109 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
114 file <<
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" << std::endl;
118 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
123 file <<
"BufferLength: " << bufferLength << std::endl;
124 file <<
"NumFrames: " << numFrames << std::endl;
125 file <<
"OffsetInput: " << offsetInput << std::endl;
126 file <<
"UseMean: " << useMean << std::endl;
127 file <<
"UseStdDev: " << useStdDev << std::endl;
128 file <<
"UseEuclideanNorm: " << useEuclideanNorm << std::endl;
129 file <<
"UseRMS: " << useRMS << std::endl;
136 if( !file.is_open() ){
137 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
146 if( word !=
"GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" ){
147 errorLog <<
"load(fstream &file) - Invalid file format!" << std::endl;
152 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
158 if( word !=
"BufferLength:" ){
159 errorLog <<
"load(fstream &file) - Failed to read BufferLength header!" << std::endl;
162 file >> bufferLength;
166 if( word !=
"NumFrames:" ){
167 errorLog <<
"load(fstream &file) - Failed to read NumFrames header!" << std::endl;
174 if( word !=
"OffsetInput:" ){
175 errorLog <<
"load(fstream &file) - Failed to read OffsetInput header!" << std::endl;
182 if( word !=
"UseMean:" ){
183 errorLog <<
"load(fstream &file) - Failed to read UseMean header!" << std::endl;
190 if( word !=
"UseStdDev:" ){
191 errorLog <<
"load(fstream &file) - Failed to read UseStdDev header!" << std::endl;
198 if( word !=
"UseEuclideanNorm:" ){
199 errorLog <<
"load(fstream &file) - Failed to read UseEuclideanNorm header!" << std::endl;
202 file >> useEuclideanNorm;
206 if( word !=
"UseRMS:" ){
207 errorLog <<
"load(fstream &file) - Failed to read UseRMS header!" << std::endl;
213 return init(bufferLength,numFrames,numInputDimensions,offsetInput,useMean,useStdDev,useEuclideanNorm,useRMS);
216 bool TimeDomainFeatures::init(
const UINT bufferLength,
const UINT numFrames,
const UINT numDimensions,
const bool offsetInput,
const bool useMean,
const bool useStdDev,
const bool useEuclideanNorm,
const bool useRMS){
220 if( numFrames > bufferLength ){
221 errorLog <<
"init(...) - The number of numFrames parameter can not be larger than the buffer length parameter!" << std::endl;
224 if( bufferLength % numFrames != 0 ){
225 errorLog <<
"init(...) - The buffer length parameter must be divisible with no remainders by the number of numFrames parameter!" << std::endl;
229 this->bufferLength = bufferLength;
230 this->numFrames = numFrames;
231 this->numInputDimensions = numDimensions;
232 this->offsetInput = offsetInput;
233 this->useMean = useMean;
234 this->useStdDev = useStdDev;
235 this->useEuclideanNorm = useEuclideanNorm;
236 this->useRMS = useRMS;
237 featureDataReady =
false;
240 numOutputDimensions = 0;
242 numOutputDimensions += numInputDimensions*numFrames;
245 numOutputDimensions += numInputDimensions*numFrames;
247 if( useEuclideanNorm ){
248 numOutputDimensions += numInputDimensions*numFrames;
251 numOutputDimensions += numInputDimensions*numFrames;
253 if( numOutputDimensions == 0 ){
254 errorLog <<
"init(...) - The numOutputDimensions is zero!" << std::endl;
259 featureVector.
resize(numOutputDimensions);
278 errorLog <<
"update(const VectorFloat &x) - Not Initialized!" << std::endl;
282 if( x.
getSize() != numInputDimensions ){
283 errorLog <<
"update(const VectorFloat &x)- The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.
getSize() <<
")!" << std::endl;
292 featureDataReady =
true;
293 }
else featureDataReady =
false;
295 MatrixFloat meanFeatures(numInputDimensions,numFrames);
296 MatrixFloat stdDevFeatures(numInputDimensions,numFrames);
297 MatrixFloat normFeatures(numInputDimensions,numFrames);
298 MatrixFloat rmsFeatures(numInputDimensions,numFrames);
302 for(UINT n=0; n<numInputDimensions; n++){
303 data[0][n] = dataBuffer[0][n];
304 for(UINT i=1; i<bufferLength; i++){
305 data[i][n] = dataBuffer[i][n]-dataBuffer[0][n];
309 for(UINT n=0; n<numInputDimensions; n++){
310 for(UINT i=0; i<bufferLength; i++){
311 data[i][n] = dataBuffer[i][n];
320 UINT frameSize = bufferLength / numFrames;
323 for(UINT n=0; n<numInputDimensions; n++){
326 for(UINT i=0; i<bufferLength; i++){
328 meanFeatures[n][frame] += data[i][n];
331 if( useEuclideanNorm )
332 normFeatures[n][frame] += data[i][n]*data[i][n];
336 rmsFeatures[n][frame] += data[i][n]*data[i][n];
338 if( ++index == frameSize ){
345 for(UINT j=0; j<numFrames; j++){
346 meanFeatures[n][j] /= frameSize;
353 for(UINT i=0; i<bufferLength; i++){
354 stdDevFeatures[n][frame] += (data[i][n]-meanFeatures[n][frame]) * (data[i][n]-meanFeatures[n][frame]);
355 if( ++index == frameSize ){
360 Float norm = frameSize>1 ? frameSize-1 : 1;
361 for(UINT j=0; j<numFrames; j++){
362 stdDevFeatures[n][j] = sqrt( stdDevFeatures[n][j]/norm );
367 if( useEuclideanNorm ){
368 for(UINT j=0; j<numFrames; j++){
369 normFeatures[n][j] = sqrt( normFeatures[n][j] );
375 for(UINT j=0; j<numFrames; j++){
376 rmsFeatures[n][j] = sqrt( rmsFeatures[n][j] / frameSize );
384 for(UINT n=0; n<numInputDimensions; n++){
385 for(UINT j=0; j<numFrames; j++){
387 featureVector[index++] = meanFeatures[n][j];
390 featureVector[index++] = stdDevFeatures[n][j];
392 if( useEuclideanNorm ){
393 featureVector[index++] = normFeatures[n][j];
396 featureVector[index++] = rmsFeatures[n][j];
401 return featureVector;
bool push_back(const T &value)
std::string getId() const
virtual bool save(std::fstream &file) const
virtual bool load(std::fstream &file)
static std::string getId()
bool getBufferFilled() const
virtual bool resize(const unsigned int size)
virtual bool computeFeatures(const VectorFloat &inputVector)
bool setAllValues(const T &value)
const CircularBuffer< VectorFloat > & getBufferData() const
virtual ~TimeDomainFeatures()
VectorFloat update(const Float x)
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)