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 @ 51:0997774f5fdc
History | View | Annotate | Download (3.02 KB)
| 1 |
/* -*- 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 |
} |
| 57 |
|
| 58 |
BOOST_AUTO_TEST_CASE(oneHarmonic) |
| 59 |
{
|
| 60 |
// input format is re0, im0, re1, im1, re2, im2
|
| 61 |
// freq for bin i is i * samplerate / n
|
| 62 |
// freqs: 0 sr/n 2sr/n 3sr/n 4sr/n 5sr/n 6sr/n 7sr/n sr/2
|
| 63 |
float in[] = { 0,0, 0,0, 10,0, 0,0, 10,0, 0,0, 10,0, 0,0, 0,0 }; |
| 64 |
double out[16]; |
| 65 |
double mm = Cepstrum(16).process(in, out); |
| 66 |
BOOST_CHECK_EQUAL(mm, 30.0/9.0); |
| 67 |
// peak is at 8
|
| 68 |
BOOST_CHECK(out[8] > 0); |
| 69 |
// odd bins are all zero
|
| 70 |
BOOST_CHECK_EQUAL(out[1], 0); |
| 71 |
BOOST_CHECK_EQUAL(out[3], 0); |
| 72 |
BOOST_CHECK_EQUAL(out[5], 0); |
| 73 |
BOOST_CHECK_EQUAL(out[7], 0); |
| 74 |
BOOST_CHECK_EQUAL(out[9], 0); |
| 75 |
BOOST_CHECK_EQUAL(out[11], 0); |
| 76 |
BOOST_CHECK_EQUAL(out[13], 0); |
| 77 |
BOOST_CHECK_EQUAL(out[15], 0); |
| 78 |
// the rest are negative
|
| 79 |
BOOST_CHECK(out[0] < 0); |
| 80 |
BOOST_CHECK(out[2] < 0); |
| 81 |
BOOST_CHECK(out[4] < 0); |
| 82 |
BOOST_CHECK(out[6] < 0); |
| 83 |
BOOST_CHECK(out[10] < 0); |
| 84 |
BOOST_CHECK(out[12] < 0); |
| 85 |
BOOST_CHECK(out[14] < 0); |
| 86 |
} |
| 87 |
|
| 88 |
BOOST_AUTO_TEST_SUITE_END() |
| 89 |
|