Commit d7a2b23d authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3507] Implement FIR general filter with variable number of coefficient

parent 404ce691
...@@ -30,31 +30,6 @@ ...@@ -30,31 +30,6 @@
#include "dcblocker.h" #include "dcblocker.h"
FirFilter::FirFilter(std::vector<double> ir) : _impulseResponse(ir),
_length(ir.size()),
_count(0)
{}
FirFilter::~FirFilter() {}
int FirFilter::getOutputSample(int inputSample)
{
_delayLine[_count] = (double)inputSample;
double result = 0.0;
int index = _count;
for(int i = 0; i < _length; i++) {
result = result + _impulseResponse[i] * _delayLine[index--];
if(index < 0)
index = _length-1;
}
_count++;
if(_count >= _length)
_count = 0;
return (int)result;
}
DcBlocker::DcBlocker() : _y(0), _x(0), _xm1(0), _ym1(0) {} DcBlocker::DcBlocker() : _y(0), _x(0), _xm1(0), _ym1(0) {}
DcBlocker::~DcBlocker() {} DcBlocker::~DcBlocker() {}
......
...@@ -36,52 +36,6 @@ ...@@ -36,52 +36,6 @@
#include <vector> #include <vector>
#define MAXFILTERSIZE 100
class FirFilter {
public:
/**
* Constructor for this class
*/
FirFilter(std::vector<double> ir);
/**
* SDestructor for this class
*/
~FirFilter();
private:
/**
* Length of the filter
*/
int _length;
/**
* Coefficient of the filter
*/
std::vector<double> _impulseResponse;
/**
* Circular buffer
*/
double _delayLine[MAXFILTERSIZE];
/**
* Counter
*/
int _count;
/**
* Perform filtering on one sample
*/
int getOutputSample(int inputSample);
};
class DcBlocker : public Algorithm { class DcBlocker : public Algorithm {
public: public:
......
...@@ -33,6 +33,31 @@ ...@@ -33,6 +33,31 @@
#include "delaydetection.h" #include "delaydetection.h"
#include "math.h" #include "math.h"
FirFilter::FirFilter(std::vector<double> ir) : _impulseResponse(ir),
_length(ir.size()),
_count(0)
{}
FirFilter::~FirFilter() {}
int FirFilter::getOutputSample(int inputSample)
{
_delayLine[_count] = (double)inputSample;
double result = 0.0;
int index = _count;
for(int i = 0; i < _length; i++) {
result = result + _impulseResponse[i] * _delayLine[index--];
if(index < 0)
index = _length-1;
}
_count++;
if(_count >= _length)
_count = 0;
return (int)result;
}
DelayDetection::DelayDetection(){} DelayDetection::DelayDetection(){}
DelayDetection::~DelayDetection(){} DelayDetection::~DelayDetection(){}
...@@ -43,7 +68,9 @@ void DelayDetection::putData(SFLDataFormat *inputData, int nbBytes) {} ...@@ -43,7 +68,9 @@ void DelayDetection::putData(SFLDataFormat *inputData, int nbBytes) {}
int DelayDetection::getData(SFLDataFormat *outputData) { return 0; } int DelayDetection::getData(SFLDataFormat *outputData) { return 0; }
void DelayDetection::process(SFLDataFormat *inputData, int nbBytes) {} void DelayDetection::process(SFLDataFormat *inputData, int nbBytes) {
}
int DelayDetection::process(SFLDataFormat *intputData, SFLDataFormat *outputData, int nbBytes) { return 0; } int DelayDetection::process(SFLDataFormat *intputData, SFLDataFormat *outputData, int nbBytes) { return 0; }
...@@ -53,44 +80,44 @@ void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short ...@@ -53,44 +80,44 @@ void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short
int counter = 0; int counter = 0;
// Output has same size as the // Output has same size as the
short rsize = refSize; short rsize = refSize;
short ssize = segSize; short ssize = segSize;
short tmpsize = segSize-refSize+1; short tmpsize = segSize-refSize+1;
// perform autocorrelation on reference signal // perform autocorrelation on reference signal
double acref = correlate(ref, ref, rsize); double acref = correlate(ref, ref, rsize);
// perform crossrelation on signal // perform crossrelation on signal
double acseg = 0.0; double acseg = 0.0;
double r; double r;
while(--tmpsize) { while(--tmpsize) {
--ssize; --ssize;
acseg = correlate(seg+tmpsize, seg+tmpsize, rsize); acseg = correlate(seg+tmpsize, seg+tmpsize, rsize);
res[ssize] = correlate(ref, seg+tmpsize, rsize); res[ssize] = correlate(ref, seg+tmpsize, rsize);
r = sqrt(acref*acseg); r = sqrt(acref*acseg);
if(r < 0.0001) if(r < 0.0001)
res[ssize] = 0.0; res[ssize] = 0.0;
else else
res[ssize] = res[ssize] / r; res[ssize] = res[ssize] / r;
} }
// perform crosscorrelation on zerro padded region // perform crosscorrelation on zerro padded region
int i = 0; int i = 0;
while(rsize) { while(rsize) {
acseg = correlate(seg, seg, rsize); acseg = correlate(seg, seg, rsize);
res[ssize-1] = correlate(ref+i, seg, rsize); res[ssize-1] = correlate(ref+i, seg, rsize);
r = sqrt(acref*acseg); r = sqrt(acref*acseg);
if(r < 0.0001) if(r < 0.0001)
res[ssize-1] = 0.0; res[ssize-1] = 0.0;
else else
res[ssize-1] = res[ssize-1] / r; res[ssize-1] = res[ssize-1] / r;
--rsize; --rsize;
--ssize; --ssize;
++i; ++i;
} }
} }
double DelayDetection::correlate(double *sig1, double *sig2, short size) { double DelayDetection::correlate(double *sig1, double *sig2, short size) {
......
...@@ -40,6 +40,53 @@ ...@@ -40,6 +40,53 @@
// Segment length in ms for correlation // Segment length in ms for correlation
#define MAX_DELAY 150 #define MAX_DELAY 150
#define MAXFILTERSIZE 100
class FirFilter {
public:
/**
* Constructor for this class
*/
FirFilter(std::vector<double> ir);
/**
* SDestructor for this class
*/
~FirFilter();
private:
/**
* Length of the filter
*/
int _length;
/**
* Coefficient of the filter
*/
std::vector<double> _impulseResponse;
/**
* Circular buffer
*/
double _delayLine[MAXFILTERSIZE];
/**
* Counter
*/
int _count;
/**
* Perform filtering on one sample
*/
int getOutputSample(int inputSample);
};
class DelayDetection : public Algorithm { class DelayDetection : public Algorithm {
public: public:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment