Commit 20658dad authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3507] Add downsampling decimation filter

parent d7a2b23d
...@@ -40,13 +40,13 @@ FirFilter::FirFilter(std::vector<double> ir) : _impulseResponse(ir), ...@@ -40,13 +40,13 @@ FirFilter::FirFilter(std::vector<double> ir) : _impulseResponse(ir),
FirFilter::~FirFilter() {} FirFilter::~FirFilter() {}
int FirFilter::getOutputSample(int inputSample) float FirFilter::getOutputSample(float inputSample)
{ {
_delayLine[_count] = (double)inputSample; _taps[_count] = inputSample;
double result = 0.0; double result = 0.0;
int index = _count; int index = _count;
for(int i = 0; i < _length; i++) { for(int i = 0; i < _length; i++) {
result = result + _impulseResponse[i] * _delayLine[index--]; result = result + _impulseResponse[i] * _taps[index--];
if(index < 0) if(index < 0)
index = _length-1; index = _length-1;
} }
...@@ -54,17 +54,28 @@ int FirFilter::getOutputSample(int inputSample) ...@@ -54,17 +54,28 @@ int FirFilter::getOutputSample(int inputSample)
if(_count >= _length) if(_count >= _length)
_count = 0; _count = 0;
return (int)result; return result;
} }
DelayDetection::DelayDetection(){} DelayDetection::DelayDetection(std::vector<double> ir) : _decimationFilter(ir) {}
DelayDetection::~DelayDetection(){} DelayDetection::~DelayDetection(){}
void DelayDetection::reset() {} void DelayDetection::reset() {}
void DelayDetection::putData(SFLDataFormat *inputData, int nbBytes) {} void DelayDetection::putData(SFLDataFormat *inputData, int nbBytes)
{
int nbSamples = nbBytes/sizeof(SFLDataFormat);
float tmp[nbSamples];
float down[nbSamples];
convertInt16ToFloat32(inputData, tmp, nbSamples);
downsampleData(tmp, down, nbSamples, 8);
}
int DelayDetection::getData(SFLDataFormat *outputData) { return 0; } int DelayDetection::getData(SFLDataFormat *outputData) { return 0; }
...@@ -120,6 +131,7 @@ void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short ...@@ -120,6 +131,7 @@ void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short
++i; ++i;
} }
} }
double DelayDetection::correlate(double *sig1, double *sig2, short size) { double DelayDetection::correlate(double *sig1, double *sig2, short size) {
short s = size; short s = size;
...@@ -130,3 +142,33 @@ double DelayDetection::correlate(double *sig1, double *sig2, short size) { ...@@ -130,3 +142,33 @@ double DelayDetection::correlate(double *sig1, double *sig2, short size) {
return ac; return ac;
} }
void DelayDetection::convertInt16ToFloat32(SFLDataFormat *input, float *output, int nbSamples) {
// factor is 1/(2^15), used to rescale the short int range to the
// [-1.0 - 1.0] float range.
#define S2F_FACTOR .000030517578125f;
int len = nbSamples;
while(len) {
len--;
output[len] = (float)input[len] * S2F_FACTOR;
}
}
void DelayDetection::downsampleData(float *input, float *output, int nbSamples, int factor) {
float tmp[nbSamples];
for(int i = 0; i < nbSamples; i++) {
tmp[i] = _decimationFilter.getOutputSample(input[i]);
}
for(int i = 0; i < nbSamples; i+=factor) {
output[i] = tmp[i];
}
}
...@@ -40,6 +40,9 @@ ...@@ -40,6 +40,9 @@
// Segment length in ms for correlation // Segment length in ms for correlation
#define MAX_DELAY 150 #define MAX_DELAY 150
// Size of internal buffers in samples
#define DELAY_BUFF_SIZE 150*8000/1000
#define MAXFILTERSIZE 100 #define MAXFILTERSIZE 100
class FirFilter { class FirFilter {
...@@ -56,6 +59,11 @@ class FirFilter { ...@@ -56,6 +59,11 @@ class FirFilter {
*/ */
~FirFilter(); ~FirFilter();
/**
* Perform filtering on one sample
*/
float getOutputSample(float inputSample);
private: private:
...@@ -72,17 +80,12 @@ class FirFilter { ...@@ -72,17 +80,12 @@ class FirFilter {
/** /**
* Circular buffer * Circular buffer
*/ */
double _delayLine[MAXFILTERSIZE]; double _taps[MAXFILTERSIZE];
/** /**
* Counter * Counter
*/ */
int _count; int _count;
/**
* Perform filtering on one sample
*/
int getOutputSample(int inputSample);
}; };
...@@ -91,7 +94,7 @@ class DelayDetection : public Algorithm { ...@@ -91,7 +94,7 @@ class DelayDetection : public Algorithm {
public: public:
DelayDetection(); DelayDetection(std::vector<double> ir);
~DelayDetection(); ~DelayDetection();
...@@ -119,6 +122,10 @@ class DelayDetection : public Algorithm { ...@@ -119,6 +122,10 @@ class DelayDetection : public Algorithm {
*/ */
double correlate(double *sig1, double *sig2, short size); double correlate(double *sig1, double *sig2, short size);
void convertInt16ToFloat32(SFLDataFormat *input, float *ouput, int nbSamples);
void downsampleData(float *input, float *output, int nbSamples, int factor);
/** /**
* Segment size in samples for correlation * Segment size in samples for correlation
*/ */
...@@ -129,6 +136,22 @@ class DelayDetection : public Algorithm { ...@@ -129,6 +136,22 @@ class DelayDetection : public Algorithm {
*/ */
short _correlationSize; short _correlationSize;
float _spkrReference[DELAY_BUFF_SIZE];
float _capturedData[DELAY_BUFF_SIZE];
float _spkrReferenceDown[DELAY_BUFF_SIZE];
float _captureDataDown[DELAY_BUFF_SIZE];
float _spkrReferenceFilter[DELAY_BUFF_SIZE];
float _captureDataFilter[DELAY_BUFF_SIZE];
// int myints[] = {16,2,77,29};
// vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
FirFilter _decimationFilter;
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