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