2
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
32
#include "delaydetectiontest.h"
38
void DelayDetectionTest::setUp() {}
40
void DelayDetectionTest::tearDown() {}
42
void DelayDetectionTest::testCrossCorrelation()
44
float signal[10] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
45
float ref[3] = {0.0, 1.0, 2.0};
48
float expected[10] = {0.0, 0.89442719, 1.0, 0.95618289, 0.91350028, 0.88543774, 0.86640023, 0.85280287, 0.8426548, 0.83480969};
50
CPPUNIT_ASSERT (_delaydetect.correlate (ref, ref, 3) == 5.0);
51
CPPUNIT_ASSERT (_delaydetect.correlate (signal, signal, 10) == 285.0);
53
_delaydetect.crossCorrelate (ref, signal, result, 3, 10);
57
for (int i = 0; i < 10; i++) {
58
tmp = result[i]-expected[i];
61
CPPUNIT_ASSERT (tmp > -0.001);
63
CPPUNIT_ASSERT (tmp < 0.001);
67
void DelayDetectionTest::testCrossCorrelationDelay()
69
float signal[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0};
70
float ref[3] = {0.0, 1.0, 0.0};
74
_delaydetect.crossCorrelate (ref, signal, result, 3, 10);
76
float expected[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0};
80
void DelayDetectionTest::testFirFilter()
82
float decimationCoefs[] = {-0.09870257, 0.07473655, 0.05616626, 0.04448337, 0.03630817, 0.02944626,
83
0.02244098, 0.01463477, 0.00610982, -0.00266367, -0.01120109, -0.01873722,
84
-0.02373243, -0.02602213, -0.02437806, -0.01869834, -0.00875287, 0.00500204,
85
0.02183252, 0.04065763, 0.06015944, 0.0788299, 0.09518543, 0.10799179,
86
0.1160644, 0.12889288, 0.1160644, 0.10799179, 0.09518543, 0.0788299,
87
0.06015944, 0.04065763, 0.02183252, 0.00500204, -0.00875287, -0.01869834,
88
-0.02437806, -0.02602213, -0.02373243, -0.01873722, -0.01120109, -0.00266367,
89
0.00610982, 0.01463477, 0.02244098, 0.02944626, 0.03630817, 0.04448337,
90
0.05616626, 0.07473655, -0.09870257
92
std::vector<double> ird (decimationCoefs, decimationCoefs + sizeof (decimationCoefs) /sizeof (float));
94
float bandpassCoefs[] = {0.06278034, -0.0758545, -0.02274943, -0.0084497, 0.0702427, 0.05986113,
95
0.06436469, -0.02412049, -0.03433526, -0.07568665, -0.03214543, -0.07236507,
96
-0.06979052, -0.12446371, -0.05530828, 0.00947243, 0.15294699, 0.17735563,
97
0.15294699, 0.00947243, -0.05530828, -0.12446371, -0.06979052, -0.07236507,
98
-0.03214543, -0.07568665, -0.03433526, -0.02412049, 0.06436469, 0.05986113,
99
0.0702427, -0.0084497, -0.02274943, -0.0758545, 0.06278034
101
std::vector<double> irb (bandpassCoefs, bandpassCoefs + sizeof (bandpassCoefs) /sizeof (float));
104
memset (impulse, 0, sizeof (float) *100);
107
FirFilter _decimationFilter (ird);
108
FirFilter _bandpassFilter (irb);
110
float impulseresponse[100];
111
memset (impulseresponse, 0, sizeof (float) *100);
113
// compute impulse response
114
for (int i = 0; i < 100; i++) {
115
impulseresponse[i] = _decimationFilter.getOutputSample (impulse[i]);
119
int size = sizeof (decimationCoefs) /sizeof (float);
121
for (int i = 0; i < size; i++) {
122
tmp = decimationCoefs[i] - impulseresponse[i];
125
CPPUNIT_ASSERT (tmp > -0.000001);
127
CPPUNIT_ASSERT (tmp < 0.000001);
131
for (int i = 0; i < 100; i++) {
132
impulseresponse[i] = _bandpassFilter.getOutputSample (impulse[i]);
135
size = sizeof (bandpassCoefs) /sizeof (float);
137
for (int i = 0; i < size; i++) {
138
tmp = bandpassCoefs[i] - impulseresponse[i];
141
CPPUNIT_ASSERT (tmp > -0.000001);
143
CPPUNIT_ASSERT (tmp < 0.000001);
148
void DelayDetectionTest::testIntToFloatConversion()
151
SFLDataFormat data[32768*2];
152
float converted[32768*2];
154
for (int i = -32768; i < 32768; i++)
157
_delaydetect.convertInt16ToFloat32 (data, converted, 32768*2);
159
for (int i = -32768; i < 0; i++) {
160
CPPUNIT_ASSERT (converted[i+32768] >= -1.0);
161
CPPUNIT_ASSERT (converted[i+32768] <= 0.0);
164
for (int i = 0; i < 32768; i++) {
165
CPPUNIT_ASSERT (converted[i+32768] >= 0.0);
166
CPPUNIT_ASSERT (converted[i+32768] <= 1.0);
170
void DelayDetectionTest::testDownSamplingData()
173
SFLDataFormat data[32768*2];
174
float converted[32768*2];
175
float resampled[32768*2];
177
for (int i = -32768; i < 32768; i++)
180
_delaydetect.convertInt16ToFloat32 (data, converted, 32768*2);
182
_delaydetect.downsampleData (converted, resampled, 32768*2, 8);
184
for (int i = 0; i < 32768/8; i++) {
185
CPPUNIT_ASSERT (resampled[i] >= -1.0);
186
CPPUNIT_ASSERT (resampled[i] <= 0.0);
189
for (int i = 32768/8+1; i < 32768/4; i++) {
190
CPPUNIT_ASSERT (resampled[i] >= 0.0);
191
CPPUNIT_ASSERT (resampled[i] <= 1.0);
198
void DelayDetectionTest::testDelayDetection()
203
SFLDataFormat spkr[WINDOW_SIZE];
204
memset (spkr, 0, sizeof (SFLDataFormat) *WINDOW_SIZE);
211
SFLDataFormat mic[DELAY_BUFF_SIZE];
212
memset (mic, 0, sizeof (SFLDataFormat) *DELAY_BUFF_SIZE);
214
mic[delay+1] = 32000;
215
mic[delay+2] = 32000;
216
mic[delay+3] = 32000;
217
mic[delay+4] = 32000;
219
_delaydetect.putData (spkr, WINDOW_SIZE*sizeof (SFLDataFormat));
220
_delaydetect.process (mic, DELAY_BUFF_SIZE*sizeof (SFLDataFormat));