To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / test / TestPeakInterpolator.cpp @ 54:751b43d119cf
History | View | Annotate | Download (5.55 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 "PeakInterpolator.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(TestPeakInterpolator) |
| 33 |
|
| 34 |
BOOST_AUTO_TEST_CASE(peakAtSample_N3) |
| 35 |
{
|
| 36 |
// Peak exactly at sample index
|
| 37 |
double data[] = { 0.0, 10.0, 0.0 }; |
| 38 |
PeakInterpolator p; |
| 39 |
// Asked to find peak at index 1, should return index 1
|
| 40 |
double result = p.findPeakLocation(data, 3, 1); |
| 41 |
BOOST_CHECK_EQUAL(result, 1.0); |
| 42 |
// Asked to find any peak, should return index 1
|
| 43 |
result = p.findPeakLocation(data, 3);
|
| 44 |
BOOST_CHECK_EQUAL(result, 1.0); |
| 45 |
} |
| 46 |
|
| 47 |
BOOST_AUTO_TEST_CASE(peakAtSample_N5) |
| 48 |
{
|
| 49 |
// Peak exactly at sample index
|
| 50 |
double data[] = { 0.0, 10.0, 20.0, 10.0, 0.0 }; |
| 51 |
PeakInterpolator p; |
| 52 |
// Asked to find peak at index 2, should return index 2
|
| 53 |
double result = p.findPeakLocation(data, 5, 2); |
| 54 |
BOOST_CHECK_EQUAL(result, 2.0); |
| 55 |
// Asked to find any peak, should return index 2
|
| 56 |
result = p.findPeakLocation(data, 5);
|
| 57 |
BOOST_CHECK_EQUAL(result, 2.0); |
| 58 |
} |
| 59 |
|
| 60 |
BOOST_AUTO_TEST_CASE(flat) |
| 61 |
{
|
| 62 |
// No peak
|
| 63 |
double data[] = { 1.0, 1.0, 1.0, 1.0, 1.0 }; |
| 64 |
PeakInterpolator p; |
| 65 |
// Asked to find peak at index N, should return N (no superior neighbours)
|
| 66 |
double result = p.findPeakLocation(data, 5, 2); |
| 67 |
BOOST_CHECK_EQUAL(result, 2.0); |
| 68 |
// Asked to find any peak, should return 0 (first value as good as any)
|
| 69 |
result = p.findPeakLocation(data, 5);
|
| 70 |
BOOST_CHECK_EQUAL(result, 0.0); |
| 71 |
} |
| 72 |
|
| 73 |
BOOST_AUTO_TEST_CASE(multiPeak) |
| 74 |
{
|
| 75 |
// More than one peak
|
| 76 |
double data[] = { 1.0, 2.0, 1.0, 2.0, 1.0 }; |
| 77 |
PeakInterpolator p; |
| 78 |
// Asked to find peak at index 3, should return index 3
|
| 79 |
double result = p.findPeakLocation(data, 5, 3); |
| 80 |
BOOST_CHECK_EQUAL(result, 3.0); |
| 81 |
// But asked to find any peak, should return 1 (first peak)
|
| 82 |
result = p.findPeakLocation(data, 5);
|
| 83 |
BOOST_CHECK_EQUAL(result, 1.0); |
| 84 |
} |
| 85 |
|
| 86 |
BOOST_AUTO_TEST_CASE(start) |
| 87 |
{
|
| 88 |
// Can't meaningfully interpolate if we're identifying element 0
|
| 89 |
// as the peak (nothing to its left)
|
| 90 |
double data[] = { 1.0, 1.0, 0.0, 0.0 }; |
| 91 |
PeakInterpolator p; |
| 92 |
double result = p.findPeakLocation(data, 4, 0); |
| 93 |
BOOST_CHECK_EQUAL(result, 0.0); |
| 94 |
} |
| 95 |
|
| 96 |
BOOST_AUTO_TEST_CASE(end) |
| 97 |
{
|
| 98 |
// Likewise for the final element
|
| 99 |
double data[] = { 0.0, 0.0, 1.0, 1.0 }; |
| 100 |
PeakInterpolator p; |
| 101 |
double result = p.findPeakLocation(data, 4, 3); |
| 102 |
BOOST_CHECK_EQUAL(result, 3.0); |
| 103 |
// But when asked to find any peak, we expect idx 2 to be picked,
|
| 104 |
// not idx 3, so that will result in interpolation
|
| 105 |
result = p.findPeakLocation(data, 4);
|
| 106 |
BOOST_CHECK(result > 2.0 && result < 3.0); |
| 107 |
} |
| 108 |
|
| 109 |
BOOST_AUTO_TEST_CASE(longHalfway) |
| 110 |
{
|
| 111 |
// Peak is exactly half-way between indices
|
| 112 |
double data[] = { 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0 }; |
| 113 |
PeakInterpolator p; |
| 114 |
// Asked to find peak for either index 3 or 4, should return 3.5
|
| 115 |
double result = p.findPeakLocation(data, 8, 3); |
| 116 |
BOOST_CHECK_EQUAL(result, 3.5); |
| 117 |
result = p.findPeakLocation(data, 8, 4); |
| 118 |
BOOST_CHECK_EQUAL(result, 3.5); |
| 119 |
// Likewise if asked to find any peak
|
| 120 |
result = p.findPeakLocation(data, 8);
|
| 121 |
BOOST_CHECK_EQUAL(result, 3.5); |
| 122 |
} |
| 123 |
|
| 124 |
BOOST_AUTO_TEST_CASE(shortHalfway) |
| 125 |
{
|
| 126 |
// As longHalfway, but with fewer points
|
| 127 |
double data[] = { 1.0, 2.0, 2.0, 1.0 }; |
| 128 |
PeakInterpolator p; |
| 129 |
double result = p.findPeakLocation(data, 4, 1); |
| 130 |
BOOST_CHECK_EQUAL(result, 1.5); |
| 131 |
result = p.findPeakLocation(data, 4);
|
| 132 |
BOOST_CHECK_EQUAL(result, 1.5); |
| 133 |
} |
| 134 |
|
| 135 |
BOOST_AUTO_TEST_CASE(aboveHalfway) |
| 136 |
{
|
| 137 |
// Peak is nearer to one index than its neighbour. (Exact position
|
| 138 |
// depends on the peak interpolation method in use; we only know
|
| 139 |
// that it must be beyond the half way point)
|
| 140 |
double data[] = { 1.0, 1.5, 2.0, 1.0 }; |
| 141 |
PeakInterpolator p; |
| 142 |
double result = p.findPeakLocation(data, 4, 2); |
| 143 |
BOOST_CHECK(result > 1.5 && result < 2.0); |
| 144 |
result = p.findPeakLocation(data, 4);
|
| 145 |
BOOST_CHECK(result > 1.5 && result < 2.0); |
| 146 |
} |
| 147 |
|
| 148 |
BOOST_AUTO_TEST_CASE(belowHalfway) |
| 149 |
{
|
| 150 |
// Peak is nearer to one index than its neighbour. (Exact position
|
| 151 |
// depends on the peak interpolation method in use; we only know
|
| 152 |
// that it must be before the half way point)
|
| 153 |
double data[] = { 1.0, 2.0, 1.5, 1.0 }; |
| 154 |
PeakInterpolator p; |
| 155 |
double result = p.findPeakLocation(data, 4, 1); |
| 156 |
BOOST_CHECK(result > 1.0 && result < 1.5); |
| 157 |
result = p.findPeakLocation(data, 4);
|
| 158 |
BOOST_CHECK(result > 1.0 && result < 1.5); |
| 159 |
} |
| 160 |
|
| 161 |
BOOST_AUTO_TEST_SUITE_END() |
| 162 |
|