28 FIRFilter::FIRFilter(
const UINT filterType,
const UINT numTaps,
const Float sampleRate,
const Float cutoffFrequency,
const Float gain,
const UINT numDimensions){
29 classType =
"FIRFilter";
30 preProcessingType = classType;
31 debugLog.setProceedingText(
"[DEBUG FIRFilter]");
32 errorLog.setProceedingText(
"[ERROR FIRFilter]");
33 warningLog.setProceedingText(
"[WARNING FIRFilter]");
36 this->numInputDimensions = numDimensions;
48 this->cutoffFrequencyLower = 0;
49 this->cutoffFrequencyUpper = 0;
54 this->cutoffFrequency = 0;
63 classType =
"FIRFilter";
64 preProcessingType = classType;
65 debugLog.setProceedingText(
"[DEBUG FIRFilter]");
66 errorLog.setProceedingText(
"[ERROR FIRFilter]");
67 warningLog.setProceedingText(
"[WARNING FIRFilter]");
78 this->filterType = rhs.filterType;
79 this->numTaps = rhs.numTaps;
80 this->sampleRate = rhs.sampleRate;
81 this->cutoffFrequency = rhs.cutoffFrequency;
82 this->cutoffFrequencyLower = rhs.cutoffFrequencyLower;
83 this->cutoffFrequencyUpper = rhs.cutoffFrequencyUpper;
84 this->gain = rhs.gain;
95 if( preProcessing == NULL )
return false;
105 errorLog <<
"deepCopyFrom(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
113 errorLog <<
"process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
117 if( inputVector.size() != numInputDimensions ){
118 errorLog <<
"process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
126 if( processedData.size() == numOutputDimensions )
return true;
138 for(UINT n=0; n<numInputDimensions; n++){
139 for(UINT i=0; i<numTaps; i++){
162 file.open(filename.c_str(), std::ios::out);
176 if( !file.is_open() ){
177 errorLog <<
"saveSettingsToFile(fstream &file) - The file is not open!" << std::endl;
182 file <<
"GRT_FIR_FILTER_FILE_V1.0" << std::endl;
186 errorLog <<
"saveSettingsToFile(fstream &file) - Failed to save base settings to file!" << std::endl;
191 file <<
"FilterType: " << filterType << std::endl;
192 file <<
"NumTaps: " << numTaps << std::endl;
193 file <<
"SampleRate: " << sampleRate << std::endl;
194 file <<
"CutoffFrequency: " << cutoffFrequency << std::endl;
195 file <<
"CutoffFrequencyLower: " << cutoffFrequencyLower << std::endl;
196 file <<
"CutoffFrequencyUpper: " << cutoffFrequencyUpper << std::endl;
197 file <<
"Gain: " << gain << std::endl;
202 file <<
"FilterCoeff: ";
203 for(UINT i=0; i<numTaps; i++){
215 file.open(filename.c_str(), std::ios::in);
233 if( !file.is_open() ){
234 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
243 if( word !=
"GRT_FIR_FILTER_FILE_V1.0" ){
244 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
250 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load preprocessing base settings from file!" << std::endl;
257 if( word !=
"FilterType:" ){
258 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FilterType header!" << std::endl;
266 if( word !=
"NumTaps:" ){
267 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumTaps header!" << std::endl;
275 if( word !=
"SampleRate:" ){
276 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read SampleRate header!" << std::endl;
284 if( word !=
"CutoffFrequency:" ){
285 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read CutoffFrequency header!" << std::endl;
289 file >> cutoffFrequency;
293 if( word !=
"CutoffFrequencyLower:" ){
294 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read CutoffFrequencyLower header!" << std::endl;
298 file >> cutoffFrequencyLower;
302 if( word !=
"CutoffFrequencyUpper:" ){
303 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read CutoffFrequencyUpper header!" << std::endl;
307 file >> cutoffFrequencyUpper;
311 if( word !=
"Gain:" ){
312 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read Gain header!" << std::endl;
326 if( word !=
"FilterCoeff:" ){
327 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FilterCoeff header!" << std::endl;
332 for(UINT i=0; i<numTaps; i++){
342 if( numInputDimensions == 0 ){
343 errorLog <<
"buildFilter() - Failed to design filter, the number of inputs has not been set!" << std::endl;
351 numOutputDimensions = numInputDimensions;
363 const Float nyquist = sampleRate / 2.0;
365 switch( filterType ){
368 lambda = PI * cutoffFrequency / nyquist;
369 for(UINT i=0; i<numTaps; i++){
370 alpha = i - (numTaps - 1.0) / 2.0;
371 if( alpha == 0.0 ) z[i] = lambda / PI;
372 else z[i] = sin( alpha * lambda ) / (alpha * PI);
377 lambda = PI * cutoffFrequency / nyquist;
378 for(UINT i=0; i<numTaps; i++){
379 alpha = i - (numTaps - 1.0) / 2.0;
380 if( alpha == 0.0 ) z[i] = 1.0 - lambda / PI;
381 else z[i] = -sin( alpha * lambda ) / (alpha * PI);
386 lambda = PI * cutoffFrequencyLower / nyquist;
387 phi = PI * cutoffFrequencyUpper / nyquist;
388 for(UINT i=0; i<numTaps; i++){
389 alpha = i - (numTaps - 1.0) / 2.0;
390 if( alpha == 0.0 ) z[i] = (phi - lambda) / PI;
391 else z[i] = (sin( alpha * phi ) - sin( alpha * lambda )) / (alpha * PI);
395 errorLog <<
"designFilter() - Failed to design filter. Unknown filter type!" << std::endl;
410 errorLog <<
"filter(const Float x) - The filter has not been initialized!" << std::endl;
417 if( result.size() == 0 ){
418 errorLog <<
"filter(const Float x) - Something went wrong, the size of the filtered vector is zero" << std::endl;
429 errorLog <<
"filter(const VectorFloat &x) - Not Initialized!" << std::endl;
433 if( x.size() != numInputDimensions ){
434 errorLog <<
"filter(const VectorFloat &x) - The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.size() <<
")!" << std::endl;
441 const UINT K = numTaps-1;
444 for(UINT n=0; n<numInputDimensions; n++){
445 processedData[n] = 0;
446 for(UINT i=0; i<numTaps; i++){
447 processedData[n] += y[K-i][n] * z[i];
449 processedData[n] *= gain;
452 return processedData;
468 return cutoffFrequency;
472 return cutoffFrequencyLower;
476 return cutoffFrequencyUpper;
499 if( filterType == LPF || filterType == HPF || filterType == BPF ){
500 this->filterType = filterType;
505 errorLog <<
"setFilterType(const UINT filterType) - Failed to set filter type, unknown filter type!" << std::endl;
513 this->numTaps = numTaps;
518 errorLog <<
"setNumTaps(const UINT numTaps) - The number of taps must be greater than zero!" << std::endl;
525 if( sampleRate > 0 ){
526 this->sampleRate = sampleRate;
531 errorLog <<
"setSampleRate(const Float sampleRate) - The sample rate should be a positive number greater than zero!" << std::endl;
538 if( filterType == BPF ){
539 warningLog <<
"setCutoffFrequency(const Float cutoffFrequency) - Setting the cutoff frequency has no effect if you are using a BPF. You should set the lower and upper cutoff frequencies instead!" << std::endl;
542 if( cutoffFrequency > 0 ){
543 this->cutoffFrequency = cutoffFrequency;
548 errorLog <<
"setCutoffFrequency(const Float cutoffFrequency) - The cutoffFrequency should be a positive number greater than zero!" << std::endl;
555 if( filterType == LPF ){
556 warningLog <<
"setCutoffFrequency(const Float cutoffFrequencyLower,const Float cutoffFrequencyUpper) - Setting the lower and upper cutoff frequency has no effect if you are using a LPF. You should set the cutoff frequency instead!" << std::endl;
559 if( filterType == HPF ){
560 warningLog <<
"setCutoffFrequency(const Float cutoffFrequencyLower,const Float cutoffFrequencyUpper) - Setting the lower and upper cutoff frequency has no effect if you are using a HPF. You should set the cutoff frequency instead!" << std::endl;
563 if( cutoffFrequencyLower > 0 && cutoffFrequencyUpper > 0 ){
564 this->cutoffFrequencyLower = cutoffFrequencyLower;
565 this->cutoffFrequencyUpper = cutoffFrequencyUpper;
570 errorLog <<
"setCutoffFrequency(const Float cutoffFrequencyLower,const Float cutoffFrequencyUpper) - The cutoffFrequency should be a positive number greater than zero!" << std::endl;
582 errorLog <<
"setGain(const Float gain) - The gain should be a positive number greater than zero!" << std::endl;
bool push_back(const T &value)
Vector< VectorFloat > getInputBuffer() const
virtual bool loadModelFromFile(std::string filename)
bool setNumTaps(const UINT numTaps)
bool savePreProcessingSettingsToFile(std::fstream &file) const
bool loadPreProcessingSettingsFromFile(std::fstream &file)
bool setFilterType(const UINT filterType)
Float getCutoffFrequency() const
virtual bool resize(const unsigned int size)
FIRFilter(const UINT filterType=LPF, const UINT numTaps=50, const Float sampleRate=100, const Float cutoffFrequency=10, const Float gain=1, const UINT numDimensions=1)
Float filter(const Float x)
Float getCutoffFrequencyUpper() const
Float getSampleRate() const
bool setCutoffFrequency(const Float cutoffFrequency)
virtual bool process(const VectorFloat &inputVector)
std::string getPreProcessingType() const
bool setSampleRate(const Float sampleRate)
VectorFloat getFilterCoefficents() const
virtual bool saveModelToFile(std::string filename) const
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
This class implements a Finite Impulse Response (FIR) Filter.
FIRFilter & operator=(const FIRFilter &rhs)
bool copyBaseVariables(const PreProcessing *preProcessingModule)
UINT getFilterType() const
Float getCutoffFrequencyLower() const
Vector< T > getData(const bool rawBuffer=false) const
bool setGain(const Float gain)
bool resize(const unsigned int newBufferSize)