30 FFTFeatures::FFTFeatures(UINT fftWindowSize,UINT numChannelsInFFTSignal,
bool computeMaxFreqFeature,
bool computeMaxFreqSpectrumRatio,
bool computeCentroidFeature,
bool computeTopNFreqFeatures,UINT N){
32 classType =
"FFTFeatures";
33 featureExtractionType = classType;
35 debugLog.setProceedingText(
"[DEBUG FFTFeatures]");
36 errorLog.setProceedingText(
"[ERROR FFTFeatures]");
37 warningLog.setProceedingText(
"[WARNING FFTFeatures]");
39 featureDataReady =
false;
41 init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
46 classType =
"FFTFeatures";
47 featureExtractionType = classType;
49 debugLog.setProceedingText(
"[DEBUG FFTFeatures]");
50 errorLog.setProceedingText(
"[ERROR FFTFeatures]");
51 warningLog.setProceedingText(
"[WARNING FFTFeatures]");
63 this->fftWindowSize = rhs.fftWindowSize;
64 this->numChannelsInFFTSignal = rhs.numChannelsInFFTSignal;
65 this->computeMaxFreqFeature = rhs.computeMaxFreqFeature;
66 this->computeMaxFreqSpectrumRatio = rhs.computeMaxFreqSpectrumRatio;
67 this->computeCentroidFeature = rhs.computeCentroidFeature;
68 this->computeTopNFreqFeatures = rhs.computeTopNFreqFeatures;
70 this->maxFreqFeature = rhs.maxFreqFeature;
71 this->maxFreqSpectrumRatio = rhs.maxFreqSpectrumRatio;
72 this->centroidFeature = rhs.centroidFeature;
82 if( featureExtraction == NULL )
return false;
92 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
100 file.open(filename.c_str(), std::ios::out);
114 file.open(filename.c_str(), std::ios::in);
128 if( !file.is_open() ){
129 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
134 file <<
"GRT_FFT_FEATURES_FILE_V1.0" << std::endl;
138 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
143 file <<
"FFTWindowSize: " << fftWindowSize << std::endl;
144 file <<
"NumChannelsInFFTSignal: " << numChannelsInFFTSignal << std::endl;
145 file <<
"ComputeMaxFreqFeature: " << computeMaxFreqFeature << std::endl;
146 file <<
"ComputeMaxFreqSpectrumRatio: " << computeMaxFreqSpectrumRatio << std::endl;
147 file <<
"ComputeCentroidFeature: " << computeCentroidFeature << std::endl;
148 file <<
"ComputeTopNFreqFeatures: " << computeTopNFreqFeatures << std::endl;
149 file <<
"N: " << N << std::endl;
156 if( !file.is_open() ){
157 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
166 if( word !=
"GRT_FFT_FEATURES_FILE_V1.0" ){
167 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
172 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
178 if( word !=
"FFTWindowSize:" ){
179 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FFTWindowSize header!" << std::endl;
182 file >> fftWindowSize;
186 if( word !=
"NumChannelsInFFTSignal:" ){
187 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumChannelsInFFTSignal header!" << std::endl;
190 file >> numChannelsInFFTSignal;
193 if( word !=
"ComputeMaxFreqFeature:" ){
194 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeMaxFreqFeature header!" << std::endl;
197 file >> computeMaxFreqFeature;
200 if( word !=
"ComputeMaxFreqSpectrumRatio:" ){
201 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeMaxFreqSpectrumRatio header!" << std::endl;
204 file >> computeMaxFreqSpectrumRatio;
207 if( word !=
"ComputeCentroidFeature:" ){
208 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeCentroidFeature header!" << std::endl;
211 file >> computeCentroidFeature;
214 if( word !=
"ComputeTopNFreqFeatures:" ){
215 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ComputeTopNFreqFeatures header!" << std::endl;
218 file >> computeTopNFreqFeatures;
222 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read N header!" << std::endl;
228 return init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
231 bool FFTFeatures::init(UINT fftWindowSize,UINT numChannelsInFFTSignal,
bool computeMaxFreqFeature,
bool computeMaxFreqSpectrumRatio,
bool computeCentroidFeature,
bool computeTopNFreqFeatures,UINT N){
234 featureDataReady =
false;
237 numInputDimensions = 0;
238 numOutputDimensions = 0;
239 featureVector.clear();
240 topNFreqFeatures.clear();
242 this->fftWindowSize = fftWindowSize;
243 this->numChannelsInFFTSignal = numChannelsInFFTSignal;
244 this->computeMaxFreqFeature = computeMaxFreqFeature;
245 this->computeMaxFreqSpectrumRatio = computeMaxFreqSpectrumRatio;
246 this->computeCentroidFeature = computeCentroidFeature;
247 this->computeTopNFreqFeatures = computeTopNFreqFeatures;
250 numInputDimensions = fftWindowSize*numChannelsInFFTSignal;
251 numOutputDimensions = 0;
252 if( computeMaxFreqFeature ) numOutputDimensions += 1;
253 if( computeMaxFreqSpectrumRatio ) numOutputDimensions += 1;
254 if( computeCentroidFeature ) numOutputDimensions += 1;
255 if( computeTopNFreqFeatures ){
256 numOutputDimensions += N;
257 topNFreqFeatures.
resize(N,0);
259 numOutputDimensions *= numChannelsInFFTSignal;
262 featureVector.
resize(numOutputDimensions,0);
272 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
277 if( inputVector.
getSize() != fftWindowSize*numChannelsInFFTSignal ){
278 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;
282 featureDataReady =
false;
284 UINT featureIndex = 0;
288 for(UINT i=0; i<numChannelsInFFTSignal; i++){
289 Float spectrumSum = 0;
294 for(UINT n=0; n<fftWindowSize; n++){
297 if( inputVector[i*fftWindowSize + n] > maxFreq.value ){
298 maxFreq.value = inputVector[i*fftWindowSize + n];
302 centroidFeature += (n+1) * inputVector[i*fftWindowSize + n];
304 spectrumSum += inputVector[i*fftWindowSize + n];
307 fftMagData[n].value = inputVector[i*fftWindowSize + n];
308 fftMagData[n].index = n;
311 maxFreqFeature = maxFreq.index;
312 maxFreqSpectrumRatio = spectrumSum > 0 ? maxFreq.value/spectrumSum : 0;
313 centroidFeature = spectrumSum > 0 ? centroidFeature/spectrumSum : 0;
315 if( computeMaxFreqFeature ){
316 featureVector[ featureIndex++ ] = maxFreqFeature;
319 if( computeMaxFreqSpectrumRatio ){
320 featureVector[ featureIndex++ ] = maxFreqSpectrumRatio;
323 if( computeCentroidFeature ){
324 featureVector[ featureIndex++ ] = centroidFeature;
327 if( computeTopNFreqFeatures ){
329 sort(fftMagData.begin(),fftMagData.end(),sortIndexDoubleDecendingValue);
330 for(UINT n=0; n<N; n++){
331 topNFreqFeatures[n] = fftMagData[n].index;
334 for(UINT n=0; n<N; n++){
335 featureVector[ featureIndex++ ] = topNFreqFeatures[n];
341 featureDataReady =
true;
347 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)
unsigned int getSize() const
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)