Commit 404ce691 authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3543] Implemented normalized crosscorrelation with unit tests

parent dc32d62f
......@@ -49,73 +49,55 @@ int DelayDetection::process(SFLDataFormat *intputData, SFLDataFormat *outputData
void DelayDetection::process(SFLDataFormat *micData, SFLDataFormat *spkrData, SFLDataFormat *outputData, int nbBytes) {}
void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short refSize, short segSize)
{
void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short refSize, short segSize) {
_debug("CrossCorrelate");
int counter = 0;
// Output has same size as the
short rsize = refSize;
short ssize = segSize;
// Output has same size as the
short rsize = refSize;
short ssize = segSize;
short tmpsize = segSize-refSize+1;
// perform autocorrelation on reference signal
// perform autocorrelation on reference signal
double acref = correlate(ref, ref, rsize);
double acseg = 0.0;
double r;
while(--tmpsize) {
acseg = correlate(seg+tmpsize, seg+tmpsize, rsize);
res[ssize] = correlate(ref, seg+tmpsize, rsize);
r = sqrt(acref*acseg);
if(r < 0.0001)
res[ssize] = 0.0;
else
res[ssize] = res[ssize] / r;
--ssize;
// perform crossrelation on signal
double acseg = 0.0;
double r;
while(--tmpsize) {
--ssize;
acseg = correlate(seg+tmpsize, seg+tmpsize, rsize);
res[ssize] = correlate(ref, seg+tmpsize, rsize);
r = sqrt(acref*acseg);
if(r < 0.0001)
res[ssize] = 0.0;
else
res[ssize] = res[ssize] / r;
}
_debug("----------------start------------------");
for (int j = 0; j < 10; j++)
_debug("segment: %f", seg[j]);
seg[0] = 0.0;
// zerro padded region
int i = 0;
while(rsize) {
_debug("rsize: %i, ssize: %i", rsize, ssize);
acref = correlate(ref, ref, refSize);
_debug("acref: %f", acref);
acseg = correlate(seg, seg, rsize);
_debug("acseg: %f", acseg);
res[ssize] = correlate(ref+rsize, seg, rsize);
_debug("res[ssize]: %f", res[ssize]);
r = sqrt(acref*acseg);
_debug("r: %f", r);
if(r < 0.0001)
res[ssize] = 0.0;
else
res[ssize] = res[ssize] / r;
--ssize;
--rsize;
++i;
// perform crosscorrelation on zerro padded region
int i = 0;
while(rsize) {
acseg = correlate(seg, seg, rsize);
res[ssize-1] = correlate(ref+i, seg, rsize);
r = sqrt(acref*acseg);
if(r < 0.0001)
res[ssize-1] = 0.0;
else
res[ssize-1] = res[ssize-1] / r;
--rsize;
--ssize;
++i;
}
}
double DelayDetection::correlate(double *sig1, double *sig2, short size) {
_debug("Correlate");
short s = size;
double ac = 0.0;
for(int i = size-1; i >= 0; i--) {
_debug(" %i: s1, %f, s2 %f", i, sig1[i], sig2[i]);
}
while(s--)
ac += sig1[s]*sig2[s];
......
......@@ -46,8 +46,8 @@ void DelayDetectionTest::testCrossCorrelation()
double result[10];
double expected[10] = {0.0, 0.89442719, 1.0, 0.95618289, 0.91350028, 0.88543774, 0.86640023, 0.85280287, 0.8426548, 0.83480969};
// CPPUNIT_ASSERT(_delaydetect.correlate(ref, ref, 3) == 5.0);
// CPPUNIT_ASSERT(_delaydetect.correlate(signal, signal, 10) == 285.0);
CPPUNIT_ASSERT(_delaydetect.correlate(ref, ref, 3) == 5.0);
CPPUNIT_ASSERT(_delaydetect.correlate(signal, signal, 10) == 285.0);
_delaydetect.crossCorrelate(ref, signal, result, 3, 10);
......@@ -56,11 +56,10 @@ void DelayDetectionTest::testCrossCorrelation()
double tmp;
for (int i = 0; i < 10; i++) {
tmp = result[i]-expected[i];
std::cout << "Result: " << result[i] << ", Expected: " << expected[i] << ", Delta: " << tmp << std::endl;
//delayif(tmp < 0.0)
// CPPUNIT_ASSERT (tmp > -0.1);
//else
// CPPUNIT_ASSERT(tmp < 0.1);
if(tmp < 0.0)
CPPUNIT_ASSERT (tmp > -0.001);
else
CPPUNIT_ASSERT(tmp < 0.001);
}
......
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