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

[#3507] Autocorrelation first try

parent ec8a5eac
......@@ -31,6 +31,7 @@
#include "delaydetection.h"
#include "math.h"
DelayDetection::DelayDetection(){}
......@@ -47,3 +48,76 @@ void DelayDetection::process(SFLDataFormat *inputData, int nbBytes) {}
int DelayDetection::process(SFLDataFormat *intputData, SFLDataFormat *outputData, int nbBytes) { return 0; }
void DelayDetection::process(SFLDataFormat *micData, SFLDataFormat *spkrData, SFLDataFormat *outputData, int nbBytes) {}
void DelayDetection::crossCorrelate(double *ref, double *seg, double *res, short refSize, short segSize)
{
_debug("CrossCorrelate");
// Output has same size as the
short rsize = refSize;
short ssize = segSize;
short tmpsize = segSize-refSize+1;
// 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;
}
_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;
}
}
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];
return ac;
}
......@@ -34,6 +34,12 @@
#include "algorithm.h"
// Template size in samples for correlation
#define WINDOW_SIZE 256
// Segment length in ms for correlation
#define MAX_DELAY 150
class DelayDetection : public Algorithm {
public:
......@@ -56,6 +62,30 @@ class DelayDetection : public Algorithm {
private:
};
/**
* Perform a normalized crosscorrelation between template and segment
*/
void crossCorrelate(double *ref, double *seg, double *res, short refSize, short segSize);
/**
* Perform a correlation on specified signals (mac)
*/
double correlate(double *sig1, double *sig2, short size);
/**
* Segment size in samples for correlation
*/
short _segmentSize;
/**
* Resulting correlation size (s + w -1)
*/
short _correlationSize;
public:
friend class DelayDetectionTest;
};
#endif
......@@ -31,6 +31,8 @@
#include "delaydetectiontest.h"
#include <iostream>
#include <math.h>
void DelayDetectionTest::setUp() {}
......@@ -38,6 +40,29 @@ void DelayDetectionTest::tearDown() {}
void DelayDetectionTest::testCrossCorrelation()
{
CPPUNIT_ASSERT (true);
double signal[10] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
double ref[3] = {0.0, 1.0, 2.0};
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);
_delaydetect.crossCorrelate(ref, signal, result, 3, 10);
std::cout << std::endl;
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);
}
}
......@@ -58,6 +58,7 @@
// #include "user_cfg.h"
#include "audio/delaydetection.h"
#ifndef _DELAYDETECTION_TEST_
#define _DELAYDETECTION_TEST_
......@@ -82,6 +83,10 @@ class DelayDetectionTest : public CppUnit::TestCase {
inline void tearDown();
void testCrossCorrelation();
private:
DelayDetection _delaydetect;
};
......
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