Commit d548c620 authored by Alexandre Savard's avatar Alexandre Savard

[#3507] Update delay detection unit tests

parent b3c2bf0a
......@@ -34,6 +34,7 @@
#include "math.h"
// #include <stdio.h>
#include <string.h>
#include <samplerate.h>
// decimation filter coefficient
float decimationCoefs[] = {-0.09870257, 0.07473655, 0.05616626, 0.04448337, 0.03630817, 0.02944626,
......@@ -182,8 +183,21 @@ void DelayDetection::process(SFLDataFormat *inputData, int nbBytes) {
memcpy(_capturedData+_nbMicSampleStored, tmp, nbSamples);
downsampleData(tmp, down, nbSamples, _downsamplingFactor);
/*
for(int i = 0; i < 10; i++)
_debug("up: %.10f", tmp[i]);
for(int i = 0; i < 10; i++)
_debug("down: %.10f", down[i]);
bandpassFilter(down, nbSamples/_downsamplingFactor);
memcpy(_capturedData+(_nbMicSampleStored/_downsamplingFactor), tmp, nbSamples);
for(int i = 0; i < 10; i++)
_debug("band: %.10f", down[i]);
*/
memcpy(_captureDataDown+(_nbMicSampleStored/_downsamplingFactor), down, (nbSamples/_downsamplingFactor)*sizeof(float));
_nbMicSampleStored += nbSamples;
......@@ -194,6 +208,14 @@ void DelayDetection::process(SFLDataFormat *inputData, int nbBytes) {
else
return;
/*
for(int i = 0; i < 10; i++)
_debug("spkrRef: %.10f", _spkrReferenceDown[i]);
for(int i = 0; i < 10; i++)
_debug("micSeg: %.10f", _captureDataDown[i]);
*/
_debug("_spkrDownSize: %d, _micDownSize: %d", _spkrDownSize, _micDownSize);
crossCorrelate(_spkrReferenceDown, _captureDataDown, _correlationResult, _micDownSize, _spkrDownSize);
......@@ -210,6 +232,8 @@ void DelayDetection::process(SFLDataFormat *micData, SFLDataFormat *spkrData, SF
void DelayDetection::crossCorrelate(float *ref, float *seg, float *res, int refSize, int segSize) {
_debug("CrossCorrelate");
int counter = 0;
// Output has same size as the
......@@ -217,9 +241,17 @@ void DelayDetection::crossCorrelate(float *ref, float *seg, float *res, int refS
int ssize = segSize;
int tmpsize = segSize-refSize+1;
/*
for(int i = 0; i < 32; i++)
_debug("ref: %.10f", ref[i]);
for(int i = 0; i < 150; i++)
_debug("seg: %.10f", seg[i]);
*/
// perform autocorrelation on reference signal
float acref = correlate(ref, ref, rsize);
_debug("acref: %f", acref);
// _debug("acref: %f", acref);
// perform crossrelation on signal
float acseg = 0.0;
......@@ -227,7 +259,7 @@ void DelayDetection::crossCorrelate(float *ref, float *seg, float *res, int refS
while(--tmpsize) {
--ssize;
acseg = correlate(seg+tmpsize, seg+tmpsize, rsize);
_debug("acseg: %f", acseg);
// _debug("acseg: %f", acseg);
res[ssize] = correlate(ref, seg+tmpsize, rsize);
r = sqrt(acref*acseg);
......@@ -241,6 +273,7 @@ void DelayDetection::crossCorrelate(float *ref, float *seg, float *res, int refS
int i = 0;
while(rsize) {
acseg = correlate(seg, seg, rsize);
// _debug("acseg: %f", acseg);
res[ssize-1] = correlate(ref+i, seg, rsize);
r = sqrt(acref*acseg);
......@@ -282,19 +315,50 @@ void DelayDetection::convertInt16ToFloat32(SFLDataFormat *input, float *output,
void DelayDetection::downsampleData(float *input, float *output, int nbSamples, int factor) {
/*
float tmp[nbSamples];
float tmp[nbSamples];
for(int i = 0; i < nbSamples; i++) {
tmp[i] = _decimationFilter.getOutputSample(input[i]);
}
for(int i = 0; i < nbSamples; i++) {
tmp[i] = _decimationFilter.getOutputSample(input[i]);
}
int j;
for(j=_remainingIndex; j<nbSamples; j+=factor) {
output[j] = tmp[j];
}
_remainingIndex = j - nbSamples;
*/
/*
double downsampleFactor = (double) samplerate1 / samplerate2;
int nbSamplesMax = (int) (samplerate1 * getFramesize() / 1000);
*/
int j;
for(j=_remainingIndex; j<nbSamples; j+=factor) {
output[j] = tmp[j];
}
_remainingIndex = j - nbSamples;
int _src_err;
SRC_STATE *_src_state = src_new (SRC_LINEAR, 1, &_src_err);
double downfactor = 1.0 / (double)factor;
if (downfactor != 1.0) {
SRC_DATA src_data;
src_data.data_in = input;
src_data.data_out = output;
src_data.input_frames = nbSamples;
src_data.output_frames = nbSamples / factor;
src_data.src_ratio = downfactor;
src_data.end_of_input = 0; // More data will come
//src_short_to_float_array (dataIn, _floatBufferUpMic, nbSamples);
//_debug("downsample %d %f %d" , src_data.output_frames, src_data.src_ratio , nbSamples);
src_process (_src_state, &src_data);
//_debug("downsample %d %f %d" , src_data.output_frames, src_data.src_ratio , nbSamples);
// nbSamples = (src_data.output_frames_gen > nbSamplesMax) ? nbSamplesMax : src_data.output_frames_gen;
//_debug("downsample %d %f %d" , src_data.output_frames, src_data.src_ratio , nbSamples);
// src_float_to_short_array (_floatBufferDownMic , dataOut , nbSamples);
}
}
......
......@@ -137,20 +137,75 @@ void DelayDetectionTest::testFirFilter()
}
void DelayDetectionTest::testIntToFloatConversion() {
SFLDataFormat data[32768*2];
float converted[32768*2];
for(int i = -32768; i < 32768; i++)
data[i+32768] = i;
_delaydetect.convertInt16ToFloat32(data, converted, 32768*2);
for(int i = -32768; i < 0; i++) {
CPPUNIT_ASSERT(converted[i+32768] >= -1.0);
CPPUNIT_ASSERT(converted[i+32768] <= 0.0);
}
for(int i = 0; i < 32768; i++) {
CPPUNIT_ASSERT(converted[i+32768] >= 0.0);
CPPUNIT_ASSERT(converted[i+32768] <= 1.0);
}
}
void DelayDetectionTest::testDownSamplingData() {
SFLDataFormat data[32768*2];
float converted[32768*2];
float resampled[32768*2];
for(int i = -32768; i < 32768; i++)
data[i+32768] = i;
_delaydetect.convertInt16ToFloat32(data, converted, 32768*2);
_delaydetect.downsampleData(converted, resampled, 32768*2, 8);
for(int i = 0; i < 32768/8; i++) {
CPPUNIT_ASSERT(resampled[i] >= -1.0);
CPPUNIT_ASSERT(resampled[i] <= 0.0);
}
for(int i = 32768/8+1; i < 32768/4; i++) {
CPPUNIT_ASSERT(resampled[i] >= 0.0);
CPPUNIT_ASSERT(resampled[i] <= 1.0);
}
}
void DelayDetectionTest::testDelayDetection() {
int delay = 3;
int delay = 100;
SFLDataFormat spkr[WINDOW_SIZE];
memset(spkr, 0, sizeof(SFLDataFormat)*WINDOW_SIZE);
spkr[0] = 32000;
spkr[1] = 32000;
spkr[2] = 32000;
spkr[3] = 32000;
spkr[4] = 32000;
SFLDataFormat mic[DELAY_BUFF_SIZE];
memset(mic, 0, sizeof(SFLDataFormat)*DELAY_BUFF_SIZE);
mic[delay] = 32000;
mic[delay+1] = 32000;
mic[delay+2] = 32000;
mic[delay+3] = 32000;
mic[delay+4] = 32000;
_delaydetect.putData(spkr, WINDOW_SIZE*sizeof(SFLDataFormat));
_delaydetect.process(mic, DELAY_BUFF_SIZE*sizeof(SFLDataFormat));
}
......@@ -74,6 +74,8 @@ class DelayDetectionTest : public CppUnit::TestCase {
CPPUNIT_TEST( testCrossCorrelation );
CPPUNIT_TEST( testCrossCorrelationDelay );
CPPUNIT_TEST( testFirFilter );
CPPUNIT_TEST( testIntToFloatConversion );
CPPUNIT_TEST( testDownSamplingData );
CPPUNIT_TEST( testDelayDetection );
CPPUNIT_TEST_SUITE_END();
......@@ -91,6 +93,10 @@ class DelayDetectionTest : public CppUnit::TestCase {
void testFirFilter();
void testIntToFloatConversion();
void testDownSamplingData();
void testDelayDetection();
private:
......
Markdown is supported
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