21 #define GRT_DLL_EXPORTS
31 classType =
"ZeroCrossingCounter";
32 featureExtractionType = classType;
33 debugLog.setProceedingText(
"[DEBUG ZeroCrossingCounter]");
34 errorLog.setProceedingText(
"[ERROR ZeroCrossingCounter]");
35 warningLog.setProceedingText(
"[WARNING ZeroCrossingCounter]");
37 init(searchWindowSize,deadZoneThreshold,numDimensions,featureMode);
42 classType =
"ZeroCrossingCounter";
43 featureExtractionType = classType;
44 debugLog.setProceedingText(
"[DEBUG ZeroCrossingCounter]");
45 errorLog.setProceedingText(
"[ERROR ZeroCrossingCounter]");
46 warningLog.setProceedingText(
"[WARNING ZeroCrossingCounter]");
72 if( featureExtraction == NULL )
return false;
80 errorLog <<
"clone(FeatureExtraction *featureExtraction) - FeatureExtraction Types Do Not Match!" << std::endl;
88 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - Not initialized!" << std::endl;
92 if( inputVector.size() != numInputDimensions ){
93 errorLog <<
"computeFeatures(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
112 file.open(filename.c_str(), std::ios::out);
126 file.open(filename.c_str(), std::ios::in);
140 if( !file.is_open() ){
141 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
146 file <<
"GRT_ZERO_CROSSING_COUNTER_FILE_V1.0" << std::endl;
150 errorLog <<
"saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
156 file <<
"FeatureMode: " <<
featureMode << std::endl;
164 if( !file.is_open() ){
165 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
174 if( word !=
"GRT_ZERO_CROSSING_COUNTER_FILE_V1.0" ){
175 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
180 errorLog <<
"loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
185 if( word !=
"SearchWindowSize:" ){
186 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read SearchWindowSize header!" << std::endl;
192 if( word !=
"FeatureMode:" ){
193 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FeatureMode header!" << std::endl;
199 if( word !=
"DeadZoneThreshold:" ){
200 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read DeadZoneThreshold header!" << std::endl;
206 return init(searchWindowSize,deadZoneThreshold,numInputDimensions,featureMode);
212 featureDataReady =
false;
214 if( searchWindowSize == 0 ){
215 errorLog <<
"init(UINT searchWindowSize,Float deadZoneThreshold,UINT numDimensions,UINT featureMode) - The searchWindowSize must be greater than zero!" << std::endl;
219 if( deadZoneThreshold < 0 ){
220 errorLog <<
"init(UINT searchWindowSize,Float deadZoneThreshold,UINT numDimensions,UINT featureMode) - The deadZoneThreshold must be greater than zero!" << std::endl;
224 if( numDimensions == 0 ){
225 errorLog <<
"init(UINT searchWindowSize,Float deadZoneThreshold,UINT numDimensions,UINT featureMode) - The numDimensions must be greater than zero!" << std::endl;
229 if( featureMode != INDEPENDANT_FEATURE_MODE && featureMode != COMBINED_FEATURE_MODE ){
230 errorLog <<
"init(UINT searchWindowSize,Float deadZoneThreshold,UINT numDimensions,UINT featureMode) - Unkown feature mode!" << std::endl;
238 numInputDimensions = numDimensions;
239 numOutputDimensions = (featureMode == INDEPENDANT_FEATURE_MODE ? TOTAL_NUM_ZERO_CROSSING_FEATURES * numInputDimensions : TOTAL_NUM_ZERO_CROSSING_FEATURES);
240 derivative.
init(Derivative::FIRST_DERIVATIVE, 1.0, numInputDimensions,
true, 5);
241 deadZone.
init(-deadZoneThreshold,deadZoneThreshold,numInputDimensions);
243 featureVector.
resize(numOutputDimensions,0);
259 errorLog <<
"update(const VectorFloat &x) - Not Initialized!" << std::endl;
263 if( x.
getSize() != numInputDimensions ){
264 errorLog <<
"update(const VectorFloat &x)- The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.
getSize() <<
")!" << std::endl;
269 std::fill(featureVector.begin(),featureVector.end(),0);
281 for(UINT j=0; j<numInputDimensions; j++){
282 UINT colIndex = (
featureMode == INDEPENDANT_FEATURE_MODE ? (TOTAL_NUM_ZERO_CROSSING_FEATURES*j) : 0);
287 featureVector[ NUM_ZERO_CROSSINGS_COUNTED + colIndex ]++;
291 UINT searchSize = i > 5 ? 5 : i;
292 for(UINT n=0; n<searchSize; n++){
293 Float value = fabs( dataBuffer[ i-n ][j] );
294 if( value > maxValue ) maxValue = value;
296 featureVector[ ZERO_CROSSING_MAGNITUDE + colIndex ] += maxValue;
302 featureDataReady =
true;
304 return featureVector;
308 if( searchWindowSize > 0 ){
310 if( initialized )
return reset();
313 errorLog <<
"setSearchWindowSize(UINT searchWindowSize) - The searchWindowSize must be larger than zero!" << std::endl;
318 if( featureMode == INDEPENDANT_FEATURE_MODE || featureMode == COMBINED_FEATURE_MODE ){
320 if( initialized )
return reset();
323 errorLog <<
"setFeatureMode(UINT featureMode) - Unkown feature mode!" << std::endl;
328 if( deadZoneThreshold > 0 ){
330 if( initialized )
return reset();
333 errorLog <<
"setDeadZoneThreshold(Float deadZoneThreshold) - The deadZoneThreshold must be larger than zero!" << std::endl;
bool push_back(const T &value)
bool setDeadZoneThreshold(Float deadZoneThreshold)
DeadZone deadZone
Used to remove small amounts of noise from the data.
VectorFloat update(Float x)
virtual bool resize(const unsigned int size)
virtual bool deepCopyFrom(const FeatureExtraction *featureExtraction)
Float filter(const Float x)
bool init(UINT derivativeOrder, Float delta, UINT numDimensions, bool filterData, UINT filterSize)
Float computeDerivative(const Float x)
CircularBuffer< VectorFloat > dataBuffer
A buffer used to store the previous derivative data.
virtual ~ZeroCrossingCounter()
virtual bool saveModelToFile(std::string filename) const
UINT searchWindowSize
The size of the search window, i.e. the amount of previous data stored and searched.
bool init(Float lowerLimit, Float upperLimit, UINT numDimensions)
ZeroCrossingCounter & operator=(const ZeroCrossingCounter &rhs)
virtual bool computeFeatures(const VectorFloat &inputVector)
UINT featureMode
The featureMode controls how the features are added to the feature vector.
bool setSearchWindowSize(UINT searchWindowSize)
VectorFloat getProcessedData() const
virtual bool loadModelFromFile(std::string filename)
Float deadZoneThreshold
The threshold value used for the dead zone filter.
Derivative derivative
Used to compute the derivative of the input signal.
bool setFeatureMode(UINT featureMode)
unsigned int getSize() const
ZeroCrossingCounter(UINT searchWindowSize=20, Float deadZoneThreshold=0.01, UINT numDimensions=1, UINT featureMode=INDEPENDANT_FEATURE_MODE)
bool resize(const unsigned int newBufferSize)