Chris@51
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@51
|
2 /*
|
Chris@51
|
3 This file is Copyright (c) 2012 Chris Cannam
|
Chris@51
|
4
|
Chris@51
|
5 Permission is hereby granted, free of charge, to any person
|
Chris@51
|
6 obtaining a copy of this software and associated documentation
|
Chris@51
|
7 files (the "Software"), to deal in the Software without
|
Chris@51
|
8 restriction, including without limitation the rights to use, copy,
|
Chris@51
|
9 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@51
|
10 of the Software, and to permit persons to whom the Software is
|
Chris@51
|
11 furnished to do so, subject to the following conditions:
|
Chris@51
|
12
|
Chris@51
|
13 The above copyright notice and this permission notice shall be
|
Chris@51
|
14 included in all copies or substantial portions of the Software.
|
Chris@51
|
15
|
Chris@51
|
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@51
|
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@51
|
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@51
|
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
Chris@51
|
20 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@51
|
21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@51
|
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@51
|
23 */
|
Chris@51
|
24
|
Chris@51
|
25 #include "Cepstrum.h"
|
Chris@51
|
26
|
Chris@51
|
27 #define BOOST_TEST_DYN_LINK
|
Chris@51
|
28 #define BOOST_TEST_MAIN
|
Chris@51
|
29
|
Chris@51
|
30 #include <boost/test/unit_test.hpp>
|
Chris@51
|
31
|
Chris@51
|
32 BOOST_AUTO_TEST_SUITE(TestCepstrum)
|
Chris@51
|
33
|
Chris@51
|
34 BOOST_AUTO_TEST_CASE(cosine)
|
Chris@51
|
35 {
|
Chris@51
|
36 // input format is re0, im0, re1, im1...
|
Chris@51
|
37 float in[] = { 0,0, 10,0, 0,0 };
|
Chris@51
|
38 double out[4];
|
Chris@51
|
39 double mm = Cepstrum(4).process(in, out);
|
Chris@51
|
40 BOOST_CHECK_SMALL(out[0] - (-4.5), 1e-10);
|
Chris@51
|
41 BOOST_CHECK_EQUAL(out[1], 0);
|
Chris@51
|
42 BOOST_CHECK_SMALL(out[2] - (-5.5), 1e-10);
|
Chris@51
|
43 BOOST_CHECK_EQUAL(out[3], 0);
|
Chris@51
|
44 BOOST_CHECK_EQUAL(mm, 10.0/3.0);
|
Chris@51
|
45 }
|
Chris@51
|
46
|
Chris@51
|
47 BOOST_AUTO_TEST_CASE(symmetry)
|
Chris@51
|
48 {
|
Chris@51
|
49 // Cepstrum output bins 1..n-1 are symmetric about bin n/2
|
Chris@51
|
50 float in[] = { 1,2,3,4,5,6,7,8,9,10 };
|
Chris@51
|
51 double out[8];
|
Chris@51
|
52 double mm = Cepstrum(8).process(in, out);
|
Chris@51
|
53 BOOST_CHECK_SMALL(out[1] - out[7], 1e-14);
|
Chris@51
|
54 BOOST_CHECK_SMALL(out[2] - out[6], 1e-14);
|
Chris@51
|
55 BOOST_CHECK_SMALL(out[3] - out[5], 1e-14);
|
Chris@73
|
56 double mmcheck = 0;
|
Chris@73
|
57 for (int i = 0; i < 5; ++i) {
|
Chris@73
|
58 mmcheck += sqrt(in[i*2] * in[i*2] + in[i*2+1] * in[i*2+1]);
|
Chris@73
|
59 }
|
Chris@73
|
60 mmcheck /= 5;
|
Chris@73
|
61 BOOST_CHECK_EQUAL(mm, mmcheck);
|
Chris@51
|
62 }
|
Chris@51
|
63
|
Chris@51
|
64 BOOST_AUTO_TEST_CASE(oneHarmonic)
|
Chris@51
|
65 {
|
Chris@51
|
66 // input format is re0, im0, re1, im1, re2, im2
|
Chris@51
|
67 // freq for bin i is i * samplerate / n
|
Chris@51
|
68 // freqs: 0 sr/n 2sr/n 3sr/n 4sr/n 5sr/n 6sr/n 7sr/n sr/2
|
Chris@51
|
69 float in[] = { 0,0, 0,0, 10,0, 0,0, 10,0, 0,0, 10,0, 0,0, 0,0 };
|
Chris@51
|
70 double out[16];
|
Chris@51
|
71 double mm = Cepstrum(16).process(in, out);
|
Chris@51
|
72 BOOST_CHECK_EQUAL(mm, 30.0/9.0);
|
Chris@51
|
73 // peak is at 8
|
Chris@51
|
74 BOOST_CHECK(out[8] > 0);
|
Chris@51
|
75 // odd bins are all zero
|
Chris@51
|
76 BOOST_CHECK_EQUAL(out[1], 0);
|
Chris@51
|
77 BOOST_CHECK_EQUAL(out[3], 0);
|
Chris@51
|
78 BOOST_CHECK_EQUAL(out[5], 0);
|
Chris@51
|
79 BOOST_CHECK_EQUAL(out[7], 0);
|
Chris@51
|
80 BOOST_CHECK_EQUAL(out[9], 0);
|
Chris@51
|
81 BOOST_CHECK_EQUAL(out[11], 0);
|
Chris@51
|
82 BOOST_CHECK_EQUAL(out[13], 0);
|
Chris@51
|
83 BOOST_CHECK_EQUAL(out[15], 0);
|
Chris@51
|
84 // the rest are negative
|
Chris@51
|
85 BOOST_CHECK(out[0] < 0);
|
Chris@51
|
86 BOOST_CHECK(out[2] < 0);
|
Chris@51
|
87 BOOST_CHECK(out[4] < 0);
|
Chris@51
|
88 BOOST_CHECK(out[6] < 0);
|
Chris@51
|
89 BOOST_CHECK(out[10] < 0);
|
Chris@51
|
90 BOOST_CHECK(out[12] < 0);
|
Chris@51
|
91 BOOST_CHECK(out[14] < 0);
|
Chris@51
|
92 }
|
Chris@51
|
93
|
Chris@51
|
94 BOOST_AUTO_TEST_SUITE_END()
|
Chris@51
|
95
|