21 #define GRT_DLL_EXPORTS
29 FIRFilter::FIRFilter(
const UINT filterType,
const UINT numTaps,
const Float sampleRate,
const Float cutoffFrequency,
const Float gain,
const UINT numDimensions){
30 classType =
"FIRFilter";
31 preProcessingType = classType;
32 debugLog.setProceedingText(
"[DEBUG FIRFilter]");
33 errorLog.setProceedingText(
"[ERROR FIRFilter]");
34 warningLog.setProceedingText(
"[WARNING FIRFilter]");
37 this->numInputDimensions = numDimensions;
49 this->cutoffFrequencyLower = 0;
50 this->cutoffFrequencyUpper = 0;
55 this->cutoffFrequency = 0;
64 classType =
"FIRFilter";
65 preProcessingType = classType;
66 debugLog.setProceedingText(
"[DEBUG FIRFilter]");
67 errorLog.setProceedingText(
"[ERROR FIRFilter]");
68 warningLog.setProceedingText(
"[WARNING FIRFilter]");
79 this->filterType = rhs.filterType;
80 this->numTaps = rhs.numTaps;
81 this->sampleRate = rhs.sampleRate;
82 this->cutoffFrequency = rhs.cutoffFrequency;
83 this->cutoffFrequencyLower = rhs.cutoffFrequencyLower;
84 this->cutoffFrequencyUpper = rhs.cutoffFrequencyUpper;
85 this->gain = rhs.gain;
96 if( preProcessing == NULL )
return false;
106 errorLog <<
"deepCopyFrom(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
114 errorLog <<
"process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
118 if( inputVector.size() != numInputDimensions ){
119 errorLog <<
"process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
127 if( processedData.size() == numOutputDimensions )
return true;
139 for(UINT n=0; n<numInputDimensions; n++){
140 for(UINT i=0; i<numTaps; i++){
162 if( !file.is_open() ){
163 errorLog <<
"saveSettingsToFile(fstream &file) - The file is not open!" << std::endl;
168 file <<
"GRT_FIR_FILTER_FILE_V1.0" << std::endl;
172 errorLog <<
"saveSettingsToFile(fstream &file) - Failed to save base settings to file!" << std::endl;
177 file <<
"FilterType: " << filterType << std::endl;
178 file <<
"NumTaps: " << numTaps << std::endl;
179 file <<
"SampleRate: " << sampleRate << std::endl;
180 file <<
"CutoffFrequency: " << cutoffFrequency << std::endl;
181 file <<
"CutoffFrequencyLower: " << cutoffFrequencyLower << std::endl;
182 file <<
"CutoffFrequencyUpper: " << cutoffFrequencyUpper << std::endl;
183 file <<
"Gain: " << gain << std::endl;
188 file <<
"FilterCoeff: ";
189 for(UINT i=0; i<numTaps; i++){
203 if( !file.is_open() ){
204 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
213 if( word !=
"GRT_FIR_FILTER_FILE_V1.0" ){
214 errorLog <<
"load(fstream &file) - Invalid file format!" << std::endl;
220 errorLog <<
"load(fstream &file) - Failed to load preprocessing base settings from file!" << std::endl;
227 if( word !=
"FilterType:" ){
228 errorLog <<
"load(fstream &file) - Failed to read FilterType header!" << std::endl;
236 if( word !=
"NumTaps:" ){
237 errorLog <<
"load(fstream &file) - Failed to read NumTaps header!" << std::endl;
245 if( word !=
"SampleRate:" ){
246 errorLog <<
"load(fstream &file) - Failed to read SampleRate header!" << std::endl;
254 if( word !=
"CutoffFrequency:" ){
255 errorLog <<
"load(fstream &file) - Failed to read CutoffFrequency header!" << std::endl;
259 file >> cutoffFrequency;
263 if( word !=
"CutoffFrequencyLower:" ){
264 errorLog <<
"load(fstream &file) - Failed to read CutoffFrequencyLower header!" << std::endl;
268 file >> cutoffFrequencyLower;
272 if( word !=
"CutoffFrequencyUpper:" ){
273 errorLog <<
"load(fstream &file) - Failed to read CutoffFrequencyUpper header!" << std::endl;
277 file >> cutoffFrequencyUpper;
281 if( word !=
"Gain:" ){
282 errorLog <<
"load(fstream &file) - Failed to read Gain header!" << std::endl;
296 if( word !=
"FilterCoeff:" ){
297 errorLog <<
"load(fstream &file) - Failed to read FilterCoeff header!" << std::endl;
302 for(UINT i=0; i<numTaps; i++){
312 if( numInputDimensions == 0 ){
313 errorLog <<
"buildFilter() - Failed to design filter, the number of inputs has not been set!" << std::endl;
321 numOutputDimensions = numInputDimensions;
333 const Float nyquist = sampleRate / 2.0;
335 switch( filterType ){
338 lambda = PI * cutoffFrequency / nyquist;
339 for(UINT i=0; i<numTaps; i++){
340 alpha = i - (numTaps - 1.0) / 2.0;
341 if( alpha == 0.0 ) z[i] = lambda / PI;
342 else z[i] = sin( alpha * lambda ) / (alpha * PI);
347 lambda = PI * cutoffFrequency / nyquist;
348 for(UINT i=0; i<numTaps; i++){
349 alpha = i - (numTaps - 1.0) / 2.0;
350 if( alpha == 0.0 ) z[i] = 1.0 - lambda / PI;
351 else z[i] = -sin( alpha * lambda ) / (alpha * PI);
356 lambda = PI * cutoffFrequencyLower / nyquist;
357 phi = PI * cutoffFrequencyUpper / nyquist;
358 for(UINT i=0; i<numTaps; i++){
359 alpha = i - (numTaps - 1.0) / 2.0;
360 if( alpha == 0.0 ) z[i] = (phi - lambda) / PI;
361 else z[i] = (sin( alpha * phi ) - sin( alpha * lambda )) / (alpha * PI);
365 errorLog <<
"designFilter() - Failed to design filter. Unknown filter type!" << std::endl;
380 errorLog <<
"filter(const Float x) - The filter has not been initialized!" << std::endl;
387 if( result.size() == 0 ){
388 errorLog <<
"filter(const Float x) - Something went wrong, the size of the filtered vector is zero" << std::endl;
399 errorLog <<
"filter(const VectorFloat &x) - Not Initialized!" << std::endl;
403 if( x.size() != numInputDimensions ){
404 errorLog <<
"filter(const VectorFloat &x) - The Number Of Input Dimensions (" << numInputDimensions <<
") does not match the size of the input vector (" << x.size() <<
")!" << std::endl;
411 const UINT K = numTaps-1;
414 for(UINT n=0; n<numInputDimensions; n++){
415 processedData[n] = 0;
416 for(UINT i=0; i<numTaps; i++){
417 processedData[n] += y[K-i][n] * z[i];
419 processedData[n] *= gain;
422 return processedData;
438 return cutoffFrequency;
442 return cutoffFrequencyLower;
446 return cutoffFrequencyUpper;
469 if( filterType == LPF || filterType == HPF || filterType == BPF ){
470 this->filterType = filterType;
475 errorLog <<
"setFilterType(const UINT filterType) - Failed to set filter type, unknown filter type!" << std::endl;
483 this->numTaps = numTaps;
488 errorLog <<
"setNumTaps(const UINT numTaps) - The number of taps must be greater than zero!" << std::endl;
495 if( sampleRate > 0 ){
496 this->sampleRate = sampleRate;
501 errorLog <<
"setSampleRate(const Float sampleRate) - The sample rate should be a positive number greater than zero!" << std::endl;
508 if( filterType == BPF ){
509 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;
512 if( cutoffFrequency > 0 ){
513 this->cutoffFrequency = cutoffFrequency;
518 errorLog <<
"setCutoffFrequency(const Float cutoffFrequency) - The cutoffFrequency should be a positive number greater than zero!" << std::endl;
525 if( filterType == LPF ){
526 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;
529 if( filterType == HPF ){
530 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;
533 if( cutoffFrequencyLower > 0 && cutoffFrequencyUpper > 0 ){
534 this->cutoffFrequencyLower = cutoffFrequencyLower;
535 this->cutoffFrequencyUpper = cutoffFrequencyUpper;
540 errorLog <<
"setCutoffFrequency(const Float cutoffFrequencyLower,const Float cutoffFrequencyUpper) - The cutoffFrequency should be a positive number greater than zero!" << std::endl;
552 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
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 load(std::fstream &file)
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
virtual bool save(std::fstream &file) const
Float getCutoffFrequencyLower() const
Vector< T > getData(const bool rawBuffer=false) const
bool setGain(const Float gain)
bool resize(const unsigned int newBufferSize)