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),
FirFilter::~FirFilter() {}
int FirFilter::getOutputSample(int inputSample)
float FirFilter::getOutputSample(float inputSample)
{
_delayLine[_count] = (double)inputSample;
_taps[_count] = inputSample;
double result = 0.0;
int index = _count;
for(int i = 0; i < _length; i++) {
result = result + _impulseResponse[i] * _delayLine[index--];
result = result + _impulseResponse[i] * _taps[index--];
if(index < 0)
index = _length-1;
}
......@@ -54,17 +54,28 @@ int FirFilter::getOutputSample(int inputSample)
if(_count >= _length)
_count = 0;
return (int)result;
return result;
}
DelayDetection::DelayDetection(){}
DelayDetection::DelayDetection(std::vector<double> ir) : _decimationFilter(ir) {}
DelayDetection::~DelayDetection(){}
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; }
......@@ -120,6 +131,7 @@ void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short
++i;
}
}
double DelayDetection::correlate(double *sig1, double *sig2, short size) {
short s = size;
......@@ -130,3 +142,33 @@ double DelayDetection::correlate(double *sig1, double *sig2, short size) {
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 @@
// Segment length in ms for correlation
#define MAX_DELAY 150
// Size of internal buffers in samples
#define DELAY_BUFF_SIZE 150*8000/1000
#define MAXFILTERSIZE 100
class FirFilter {
......@@ -56,6 +59,11 @@ class FirFilter {
*/
~FirFilter();
/**
* Perform filtering on one sample
*/
float getOutputSample(float inputSample);
private:
......@@ -72,17 +80,12 @@ class FirFilter {
/**
* Circular buffer
*/
double _delayLine[MAXFILTERSIZE];
double _taps[MAXFILTERSIZE];
/**
* Counter
*/
int _count;
/**
* Perform filtering on one sample
*/
int getOutputSample(int inputSample);
};
......@@ -91,7 +94,7 @@ class DelayDetection : public Algorithm {
public:
DelayDetection();
DelayDetection(std::vector<double> ir);
~DelayDetection();
......@@ -119,6 +122,10 @@ class DelayDetection : public Algorithm {
*/
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
*/
......@@ -129,6 +136,22 @@ class DelayDetection : public Algorithm {
*/
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:
......
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