~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/test/delaydetectiontest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
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.
 
29
 */
 
30
 
 
31
 
 
32
#include "delaydetectiontest.h"
 
33
 
 
34
#include <iostream>
 
35
#include <math.h>
 
36
#include <string.h>
 
37
 
 
38
void DelayDetectionTest::setUp() {}
 
39
 
 
40
void DelayDetectionTest::tearDown() {}
 
41
 
 
42
void DelayDetectionTest::testCrossCorrelation()
 
43
{
 
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};
 
46
 
 
47
    float result[10];
 
48
    float expected[10] = {0.0, 0.89442719, 1.0, 0.95618289, 0.91350028, 0.88543774, 0.86640023, 0.85280287, 0.8426548, 0.83480969};
 
49
 
 
50
    CPPUNIT_ASSERT (_delaydetect.correlate (ref, ref, 3) == 5.0);
 
51
    CPPUNIT_ASSERT (_delaydetect.correlate (signal, signal, 10) == 285.0);
 
52
 
 
53
    _delaydetect.crossCorrelate (ref, signal, result, 3, 10);
 
54
 
 
55
    float tmp;
 
56
 
 
57
    for (int i = 0; i < 10; i++) {
 
58
        tmp = result[i]-expected[i];
 
59
 
 
60
        if (tmp < 0.0)
 
61
            CPPUNIT_ASSERT (tmp > -0.001);
 
62
        else
 
63
            CPPUNIT_ASSERT (tmp < 0.001);
 
64
    }
 
65
}
 
66
 
 
67
void DelayDetectionTest::testCrossCorrelationDelay()
 
68
{
 
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};
 
71
 
 
72
    float result[10];
 
73
 
 
74
    _delaydetect.crossCorrelate (ref, signal, result, 3, 10);
 
75
 
 
76
    float expected[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0};
 
77
 
 
78
}
 
79
 
 
80
void DelayDetectionTest::testFirFilter()
 
81
{
 
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
 
91
                              };
 
92
    std::vector<double> ird (decimationCoefs, decimationCoefs + sizeof (decimationCoefs) /sizeof (float));
 
93
 
 
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
 
100
                            };
 
101
    std::vector<double> irb (bandpassCoefs, bandpassCoefs + sizeof (bandpassCoefs) /sizeof (float));
 
102
 
 
103
    float impulse[100];
 
104
    memset (impulse, 0, sizeof (float) *100);
 
105
    impulse[0] = 1.0;
 
106
 
 
107
    FirFilter _decimationFilter (ird);
 
108
    FirFilter _bandpassFilter (irb);
 
109
 
 
110
    float impulseresponse[100];
 
111
    memset (impulseresponse, 0, sizeof (float) *100);
 
112
 
 
113
    // compute impulse response
 
114
    for (int i = 0; i < 100; i++) {
 
115
        impulseresponse[i] = _decimationFilter.getOutputSample (impulse[i]);
 
116
    }
 
117
 
 
118
    float tmp;
 
119
    int size = sizeof (decimationCoefs) /sizeof (float);
 
120
 
 
121
    for (int i = 0; i < size; i++) {
 
122
        tmp = decimationCoefs[i] - impulseresponse[i];
 
123
 
 
124
        if (tmp < 0.0)
 
125
            CPPUNIT_ASSERT (tmp > -0.000001);
 
126
        else
 
127
            CPPUNIT_ASSERT (tmp < 0.000001);
 
128
    }
 
129
 
 
130
 
 
131
    for (int i = 0; i < 100; i++) {
 
132
        impulseresponse[i] = _bandpassFilter.getOutputSample (impulse[i]);
 
133
    }
 
134
 
 
135
    size = sizeof (bandpassCoefs) /sizeof (float);
 
136
 
 
137
    for (int i = 0; i < size; i++) {
 
138
        tmp = bandpassCoefs[i] - impulseresponse[i];
 
139
 
 
140
        if (tmp < 0.0)
 
141
            CPPUNIT_ASSERT (tmp > -0.000001);
 
142
        else
 
143
            CPPUNIT_ASSERT (tmp < 0.000001);
 
144
    }
 
145
 
 
146
}
 
147
 
 
148
void DelayDetectionTest::testIntToFloatConversion()
 
149
{
 
150
 
 
151
    SFLDataFormat data[32768*2];
 
152
    float converted[32768*2];
 
153
 
 
154
    for (int i = -32768; i < 32768; i++)
 
155
        data[i+32768] = i;
 
156
 
 
157
    _delaydetect.convertInt16ToFloat32 (data, converted, 32768*2);
 
158
 
 
159
    for (int i = -32768; i < 0; i++) {
 
160
        CPPUNIT_ASSERT (converted[i+32768] >= -1.0);
 
161
        CPPUNIT_ASSERT (converted[i+32768] <= 0.0);
 
162
    }
 
163
 
 
164
    for (int i = 0; i < 32768; i++) {
 
165
        CPPUNIT_ASSERT (converted[i+32768] >= 0.0);
 
166
        CPPUNIT_ASSERT (converted[i+32768] <= 1.0);
 
167
    }
 
168
}
 
169
 
 
170
void DelayDetectionTest::testDownSamplingData()
 
171
{
 
172
 
 
173
    SFLDataFormat data[32768*2];
 
174
    float converted[32768*2];
 
175
    float resampled[32768*2];
 
176
 
 
177
    for (int i = -32768; i < 32768; i++)
 
178
        data[i+32768] = i;
 
179
 
 
180
    _delaydetect.convertInt16ToFloat32 (data, converted, 32768*2);
 
181
 
 
182
    _delaydetect.downsampleData (converted, resampled, 32768*2, 8);
 
183
 
 
184
    for (int i = 0; i < 32768/8; i++) {
 
185
        CPPUNIT_ASSERT (resampled[i] >= -1.0);
 
186
        CPPUNIT_ASSERT (resampled[i] <= 0.0);
 
187
    }
 
188
 
 
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);
 
192
    }
 
193
 
 
194
 
 
195
}
 
196
 
 
197
 
 
198
void DelayDetectionTest::testDelayDetection()
 
199
{
 
200
 
 
201
    int delay = 100;
 
202
 
 
203
    SFLDataFormat spkr[WINDOW_SIZE];
 
204
    memset (spkr, 0, sizeof (SFLDataFormat) *WINDOW_SIZE);
 
205
    spkr[0] = 32000;
 
206
    spkr[1] = 32000;
 
207
    spkr[2] = 32000;
 
208
    spkr[3] = 32000;
 
209
    spkr[4] = 32000;
 
210
 
 
211
    SFLDataFormat mic[DELAY_BUFF_SIZE];
 
212
    memset (mic, 0, sizeof (SFLDataFormat) *DELAY_BUFF_SIZE);
 
213
    mic[delay] = 32000;
 
214
    mic[delay+1] = 32000;
 
215
    mic[delay+2] = 32000;
 
216
    mic[delay+3] = 32000;
 
217
    mic[delay+4] = 32000;
 
218
 
 
219
    _delaydetect.putData (spkr, WINDOW_SIZE*sizeof (SFLDataFormat));
 
220
    _delaydetect.process (mic, DELAY_BUFF_SIZE*sizeof (SFLDataFormat));
 
221
 
 
222
}