21 #define GRT_DLL_EXPORTS
31 FFTFeatures::FFTFeatures(UINT fftWindowSize,UINT numChannelsInFFTSignal,
bool computeMaxFreqFeature,
bool computeMaxFreqSpectrumRatio,
bool computeCentroidFeature,
bool computeTopNFreqFeatures,UINT N){
33 classType =
"FFTFeatures";
34 featureExtractionType = classType;
36 debugLog.setProceedingText(
"[DEBUG FFTFeatures]");
37 errorLog.setProceedingText(
"[ERROR FFTFeatures]");
38 warningLog.setProceedingText(
"[WARNING FFTFeatures]");
40 featureDataReady =
false;
42 init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
47 classType =
"FFTFeatures";
48 featureExtractionType = classType;
50 debugLog.setProceedingText(
"[DEBUG FFTFeatures]");
51 errorLog.setProceedingText(
"[ERROR FFTFeatures]");
52 warningLog.setProceedingText(
"[WARNING FFTFeatures]");
64 this->fftWindowSize = rhs.fftWindowSize;
65 this->numChannelsInFFTSignal = rhs.numChannelsInFFTSignal;
66 this->computeMaxFreqFeature = rhs.computeMaxFreqFeature;
67 this->computeMaxFreqSpectrumRatio = rhs.computeMaxFreqSpectrumRatio;
68 this->computeCentroidFeature = rhs.computeCentroidFeature;
69 this->computeTopNFreqFeatures = rhs.computeTopNFreqFeatures;
71 this->maxFreqFeature = rhs.maxFreqFeature;
72 this->maxFreqSpectrumRatio = rhs.maxFreqSpectrumRatio;
73 this->centroidFeature = rhs.centroidFeature;
83 if( featureExtraction == NULL )
return false;
93 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
101 file.open(filename.c_str(), std::ios::out);
115 file.open(filename.c_str(), std::ios::in);
129 if( !file.is_open() ){
130 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
135 file <<
"GRT_FFT_FEATURES_FILE_V1.0" << std::endl;
139 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
144 file <<
"FFTWindowSize: " << fftWindowSize << std::endl;
145 file <<
"NumChannelsInFFTSignal: " << numChannelsInFFTSignal << std::endl;
146 file <<
"ComputeMaxFreqFeature: " << computeMaxFreqFeature << std::endl;
147 file <<
"ComputeMaxFreqSpectrumRatio: " << computeMaxFreqSpectrumRatio << std::endl;
148 file <<
"ComputeCentroidFeature: " << computeCentroidFeature << std::endl;
149 file <<
"ComputeTopNFreqFeatures: " << computeTopNFreqFeatures << std::endl;
150 file <<
"N: " << N << std::endl;
157 if( !file.is_open() ){
158 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
167 if( word !=
"GRT_FFT_FEATURES_FILE_V1.0" ){
168 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
173 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
179 if( word !=
"FFTWindowSize:" ){
180 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FFTWindowSize header!" << std::endl;
183 file >> fftWindowSize;
187 if( word !=
"NumChannelsInFFTSignal:" ){
188 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumChannelsInFFTSignal header!" << std::endl;
191 file >> numChannelsInFFTSignal;
194 if( word !=
"ComputeMaxFreqFeature:" ){
195 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeMaxFreqFeature header!" << std::endl;
198 file >> computeMaxFreqFeature;
201 if( word !=
"ComputeMaxFreqSpectrumRatio:" ){
202 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeMaxFreqSpectrumRatio header!" << std::endl;
205 file >> computeMaxFreqSpectrumRatio;
208 if( word !=
"ComputeCentroidFeature:" ){
209 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeCentroidFeature header!" << std::endl;
212 file >> computeCentroidFeature;
215 if( word !=
"ComputeTopNFreqFeatures:" ){
216 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeTopNFreqFeatures header!" << std::endl;
219 file >> computeTopNFreqFeatures;
223 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read N header!" << std::endl;
229 return init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
232 bool FFTFeatures::init(UINT fftWindowSize,UINT numChannelsInFFTSignal,
bool computeMaxFreqFeature,
bool computeMaxFreqSpectrumRatio,
bool computeCentroidFeature,
bool computeTopNFreqFeatures,UINT N){
235 featureDataReady =
false;
238 numInputDimensions = 0;
239 numOutputDimensions = 0;
240 featureVector.clear();
241 topNFreqFeatures.clear();
243 this->fftWindowSize = fftWindowSize;
244 this->numChannelsInFFTSignal = numChannelsInFFTSignal;
245 this->computeMaxFreqFeature = computeMaxFreqFeature;
246 this->computeMaxFreqSpectrumRatio = computeMaxFreqSpectrumRatio;
247 this->computeCentroidFeature = computeCentroidFeature;
248 this->computeTopNFreqFeatures = computeTopNFreqFeatures;
251 numInputDimensions = fftWindowSize*numChannelsInFFTSignal;
252 numOutputDimensions = 0;
253 if( computeMaxFreqFeature ) numOutputDimensions += 1;
254 if( computeMaxFreqSpectrumRatio ) numOutputDimensions += 1;
255 if( computeCentroidFeature ) numOutputDimensions += 1;
256 if( computeTopNFreqFeatures ){
257 numOutputDimensions += N;
258 topNFreqFeatures.
resize(N,0);
260 numOutputDimensions *= numChannelsInFFTSignal;
263 featureVector.
resize(numOutputDimensions,0);
273 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
278 if( inputVector.
getSize() != fftWindowSize*numChannelsInFFTSignal ){
279 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match the expected size! Verify that the FFT module that generated this inputVector has a window size of " << fftWindowSize <<
" and the number of input channels is: " << numChannelsInFFTSignal <<
". Also verify that only the magnitude values are being computed (and not the phase)." << std::endl;
283 featureDataReady =
false;
285 UINT featureIndex = 0;
289 for(UINT i=0; i<numChannelsInFFTSignal; i++){
290 Float spectrumSum = 0;
295 for(UINT n=0; n<fftWindowSize; n++){
298 if( inputVector[i*fftWindowSize + n] > maxFreq.value ){
299 maxFreq.value = inputVector[i*fftWindowSize + n];
303 centroidFeature += (n+1) * inputVector[i*fftWindowSize + n];
305 spectrumSum += inputVector[i*fftWindowSize + n];
308 fftMagData[n].value = inputVector[i*fftWindowSize + n];
309 fftMagData[n].index = n;
312 maxFreqFeature = maxFreq.index;
313 maxFreqSpectrumRatio = spectrumSum > 0 ? maxFreq.value/spectrumSum : 0;
314 centroidFeature = spectrumSum > 0 ? centroidFeature/spectrumSum : 0;
316 if( computeMaxFreqFeature ){
317 featureVector[ featureIndex++ ] = maxFreqFeature;
320 if( computeMaxFreqSpectrumRatio ){
321 featureVector[ featureIndex++ ] = maxFreqSpectrumRatio;
324 if( computeCentroidFeature ){
325 featureVector[ featureIndex++ ] = centroidFeature;
328 if( computeTopNFreqFeatures ){
330 sort(fftMagData.begin(),fftMagData.end(),sortIndexDoubleDecendingValue);
331 for(UINT n=0; n<N; n++){
332 topNFreqFeatures[n] = fftMagData[n].index;
335 for(UINT n=0; n<N; n++){
336 featureVector[ featureIndex++ ] = topNFreqFeatures[n];
342 featureDataReady =
true;
348 if( initialized )
return init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
virtual bool saveModelToFile(std::string filename) const
virtual ~FFTFeatures(void)
virtual bool resize(const unsigned int size)
This class implements the FFTFeatures featue extraction module.
FFTFeatures(UINT fftWindowSize=512, UINT numChannelsInFFTSignal=1, bool computeMaxFreqFeature=true, bool computeMaxFreqSpectrumRatio=true, bool computeCentroidFeature=true, bool computeTopNFreqFeatures=true, UINT N=10)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
virtual bool computeFeatures(const VectorFloat &inputVector)
virtual bool loadModelFromFile(std::string filename)
FFTFeatures & operator=(const FFTFeatures &rhs)