21 #define GRT_DLL_EXPORTS 27 std::string FFTFeatures::id =
"FFTFeatures";
38 featureDataReady =
false;
40 init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
55 this->fftWindowSize = rhs.fftWindowSize;
56 this->numChannelsInFFTSignal = rhs.numChannelsInFFTSignal;
57 this->computeMaxFreqFeature = rhs.computeMaxFreqFeature;
58 this->computeMaxFreqSpectrumRatio = rhs.computeMaxFreqSpectrumRatio;
59 this->computeCentroidFeature = rhs.computeCentroidFeature;
60 this->computeTopNFreqFeatures = rhs.computeTopNFreqFeatures;
62 this->maxFreqFeature = rhs.maxFreqFeature;
63 this->maxFreqSpectrumRatio = rhs.maxFreqSpectrumRatio;
64 this->centroidFeature = rhs.centroidFeature;
74 if( featureExtraction == NULL )
return false;
76 if( this->
getId() == featureExtraction->
getId() ){
84 errorLog <<
"deepCopyFrom(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
91 if( !file.is_open() ){
92 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
97 file <<
"GRT_FFT_FEATURES_FILE_V1.0" << std::endl;
101 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
106 file <<
"FFTWindowSize: " << fftWindowSize << std::endl;
107 file <<
"NumChannelsInFFTSignal: " << numChannelsInFFTSignal << std::endl;
108 file <<
"ComputeMaxFreqFeature: " << computeMaxFreqFeature << std::endl;
109 file <<
"ComputeMaxFreqSpectrumRatio: " << computeMaxFreqSpectrumRatio << std::endl;
110 file <<
"ComputeCentroidFeature: " << computeCentroidFeature << std::endl;
111 file <<
"ComputeTopNFreqFeatures: " << computeTopNFreqFeatures << std::endl;
112 file <<
"N: " << N << std::endl;
119 if( !file.is_open() ){
120 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
129 if( word !=
"GRT_FFT_FEATURES_FILE_V1.0" ){
130 errorLog <<
"load(fstream &file) - Invalid file format!" << std::endl;
135 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
141 if( word !=
"FFTWindowSize:" ){
142 errorLog <<
"load(fstream &file) - Failed to read FFTWindowSize header!" << std::endl;
145 file >> fftWindowSize;
149 if( word !=
"NumChannelsInFFTSignal:" ){
150 errorLog <<
"load(fstream &file) - Failed to read NumChannelsInFFTSignal header!" << std::endl;
153 file >> numChannelsInFFTSignal;
156 if( word !=
"ComputeMaxFreqFeature:" ){
157 errorLog <<
"load(fstream &file) - Failed to read ComputeMaxFreqFeature header!" << std::endl;
160 file >> computeMaxFreqFeature;
163 if( word !=
"ComputeMaxFreqSpectrumRatio:" ){
164 errorLog <<
"load(fstream &file) - Failed to read ComputeMaxFreqSpectrumRatio header!" << std::endl;
167 file >> computeMaxFreqSpectrumRatio;
170 if( word !=
"ComputeCentroidFeature:" ){
171 errorLog <<
"load(fstream &file) - Failed to read ComputeCentroidFeature header!" << std::endl;
174 file >> computeCentroidFeature;
177 if( word !=
"ComputeTopNFreqFeatures:" ){
178 errorLog <<
"load(fstream &file) - Failed to read ComputeTopNFreqFeatures header!" << std::endl;
181 file >> computeTopNFreqFeatures;
185 errorLog <<
"load(fstream &file) - Failed to read N header!" << std::endl;
191 return init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
194 bool FFTFeatures::init(
const UINT fftWindowSize,
const UINT numChannelsInFFTSignal,
const bool computeMaxFreqFeature,
const bool computeMaxFreqSpectrumRatio,
const bool computeCentroidFeature,
const bool computeTopNFreqFeatures,
const UINT N){
197 featureDataReady =
false;
200 numInputDimensions = 0;
201 numOutputDimensions = 0;
202 featureVector.clear();
203 topNFreqFeatures.clear();
205 this->fftWindowSize = fftWindowSize;
206 this->numChannelsInFFTSignal = numChannelsInFFTSignal;
207 this->computeMaxFreqFeature = computeMaxFreqFeature;
208 this->computeMaxFreqSpectrumRatio = computeMaxFreqSpectrumRatio;
209 this->computeCentroidFeature = computeCentroidFeature;
210 this->computeTopNFreqFeatures = computeTopNFreqFeatures;
213 numInputDimensions = fftWindowSize*numChannelsInFFTSignal;
214 numOutputDimensions = 0;
215 if( computeMaxFreqFeature ) numOutputDimensions += 1;
216 if( computeMaxFreqSpectrumRatio ) numOutputDimensions += 1;
217 if( computeCentroidFeature ) numOutputDimensions += 1;
218 if( computeTopNFreqFeatures ){
219 numOutputDimensions += N;
220 topNFreqFeatures.
resize(N,0);
222 numOutputDimensions *= numChannelsInFFTSignal;
225 featureVector.
resize(numOutputDimensions,0);
235 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
240 if( inputVector.
getSize() != fftWindowSize*numChannelsInFFTSignal ){
241 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;
245 featureDataReady =
false;
247 UINT featureIndex = 0;
251 for(UINT i=0; i<numChannelsInFFTSignal; i++){
252 Float spectrumSum = 0;
257 for(UINT n=0; n<fftWindowSize; n++){
260 if( inputVector[i*fftWindowSize + n] > maxFreq.value ){
261 maxFreq.value = inputVector[i*fftWindowSize + n];
265 centroidFeature += (n+1) * inputVector[i*fftWindowSize + n];
267 spectrumSum += inputVector[i*fftWindowSize + n];
270 fftMagData[n].value = inputVector[i*fftWindowSize + n];
271 fftMagData[n].index = n;
274 maxFreqFeature = maxFreq.index;
275 maxFreqSpectrumRatio = spectrumSum > 0 ? maxFreq.value/spectrumSum : 0;
276 centroidFeature = spectrumSum > 0 ? centroidFeature/spectrumSum : 0;
278 if( computeMaxFreqFeature ){
279 featureVector[ featureIndex++ ] = maxFreqFeature;
282 if( computeMaxFreqSpectrumRatio ){
283 featureVector[ featureIndex++ ] = maxFreqSpectrumRatio;
286 if( computeCentroidFeature ){
287 featureVector[ featureIndex++ ] = centroidFeature;
290 if( computeTopNFreqFeatures ){
292 sort(fftMagData.begin(),fftMagData.end(),sortIndexDoubleDecendingValue);
293 for(UINT n=0; n<N; n++){
294 topNFreqFeatures[n] = fftMagData[n].index;
297 for(UINT n=0; n<N; n++){
298 featureVector[ featureIndex++ ] = topNFreqFeatures[n];
304 featureDataReady =
true;
310 if( initialized )
return init(fftWindowSize,numChannelsInFFTSignal,computeMaxFreqFeature,computeMaxFreqSpectrumRatio,computeCentroidFeature,computeTopNFreqFeatures,N);
std::string getId() const
virtual ~FFTFeatures(void)
FFTFeatures(const UINT fftWindowSize=512, const UINT numChannelsInFFTSignal=1, const bool computeMaxFreqFeature=true, const bool computeMaxFreqSpectrumRatio=true, const bool computeCentroidFeature=true, const bool computeTopNFreqFeatures=true, const UINT N=10)
virtual bool resize(const unsigned int size)
virtual bool save(std::fstream &file) const
This class implements the FFTFeatures featue extraction module.
virtual bool load(std::fstream &file)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
virtual bool computeFeatures(const VectorFloat &inputVector)
static std::string getId()
FFTFeatures & operator=(const FFTFeatures &rhs)