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
History | View | Annotate | Download (5.55 KB)
| 1 | 39:822cf7b8e070 | 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 "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 | 46:c666067fb8da | Chris | // Peak exactly at sample index
|
| 37 | double data[] = { 0.0, 10.0, 0.0 }; |
||
| 38 | 39:822cf7b8e070 | Chris | PeakInterpolator p; |
| 39 | 46:c666067fb8da | Chris | // Asked to find peak at index 1, should return index 1
|
| 40 | 39:822cf7b8e070 | Chris | double result = p.findPeakLocation(data, 3, 1); |
| 41 | BOOST_CHECK_EQUAL(result, 1.0); |
||
| 42 | 46:c666067fb8da | Chris | // Asked to find any peak, should return index 1
|
| 43 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 3);
|
| 44 | BOOST_CHECK_EQUAL(result, 1.0); |
||
| 45 | 39:822cf7b8e070 | Chris | } |
| 46 | |||
| 47 | BOOST_AUTO_TEST_CASE(peakAtSample_N5) |
||
| 48 | {
|
||
| 49 | 46:c666067fb8da | Chris | // Peak exactly at sample index
|
| 50 | double data[] = { 0.0, 10.0, 20.0, 10.0, 0.0 }; |
||
| 51 | 39:822cf7b8e070 | Chris | PeakInterpolator p; |
| 52 | 46:c666067fb8da | Chris | // Asked to find peak at index 2, should return index 2
|
| 53 | 39:822cf7b8e070 | Chris | double result = p.findPeakLocation(data, 5, 2); |
| 54 | BOOST_CHECK_EQUAL(result, 2.0); |
||
| 55 | 46:c666067fb8da | Chris | // Asked to find any peak, should return index 2
|
| 56 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 5);
|
| 57 | BOOST_CHECK_EQUAL(result, 2.0); |
||
| 58 | 39:822cf7b8e070 | Chris | } |
| 59 | |||
| 60 | BOOST_AUTO_TEST_CASE(flat) |
||
| 61 | {
|
||
| 62 | 46:c666067fb8da | Chris | // No peak
|
| 63 | 39:822cf7b8e070 | Chris | double data[] = { 1.0, 1.0, 1.0, 1.0, 1.0 }; |
| 64 | PeakInterpolator p; |
||
| 65 | 46:c666067fb8da | Chris | // Asked to find peak at index N, should return N (no superior neighbours)
|
| 66 | 39:822cf7b8e070 | Chris | double result = p.findPeakLocation(data, 5, 2); |
| 67 | BOOST_CHECK_EQUAL(result, 2.0); |
||
| 68 | 46:c666067fb8da | Chris | // Asked to find any peak, should return 0 (first value as good as any)
|
| 69 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 5);
|
| 70 | BOOST_CHECK_EQUAL(result, 0.0); |
||
| 71 | 39:822cf7b8e070 | Chris | } |
| 72 | |||
| 73 | BOOST_AUTO_TEST_CASE(multiPeak) |
||
| 74 | {
|
||
| 75 | 46:c666067fb8da | Chris | // More than one peak
|
| 76 | 39:822cf7b8e070 | Chris | double data[] = { 1.0, 2.0, 1.0, 2.0, 1.0 }; |
| 77 | PeakInterpolator p; |
||
| 78 | 46:c666067fb8da | Chris | // Asked to find peak at index 3, should return index 3
|
| 79 | 39:822cf7b8e070 | Chris | double result = p.findPeakLocation(data, 5, 3); |
| 80 | BOOST_CHECK_EQUAL(result, 3.0); |
||
| 81 | 46:c666067fb8da | Chris | // But asked to find any peak, should return 1 (first peak)
|
| 82 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 5);
|
| 83 | BOOST_CHECK_EQUAL(result, 1.0); |
||
| 84 | 39:822cf7b8e070 | Chris | } |
| 85 | |||
| 86 | 40:8f56ef28b0b1 | Chris | BOOST_AUTO_TEST_CASE(start) |
| 87 | {
|
||
| 88 | // Can't meaningfully interpolate if we're identifying element 0
|
||
| 89 | 46:c666067fb8da | Chris | // as the peak (nothing to its left)
|
| 90 | 40:8f56ef28b0b1 | Chris | 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 | 46:c666067fb8da | Chris | // 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 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 4);
|
| 106 | BOOST_CHECK(result > 2.0 && result < 3.0); |
||
| 107 | 40:8f56ef28b0b1 | Chris | } |
| 108 | |||
| 109 | BOOST_AUTO_TEST_CASE(longHalfway) |
||
| 110 | {
|
||
| 111 | 46:c666067fb8da | Chris | // Peak is exactly half-way between indices
|
| 112 | 40:8f56ef28b0b1 | Chris | double data[] = { 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0 }; |
| 113 | PeakInterpolator p; |
||
| 114 | 46:c666067fb8da | Chris | // Asked to find peak for either index 3 or 4, should return 3.5
|
| 115 | double result = p.findPeakLocation(data, 8, 3); |
||
| 116 | 40:8f56ef28b0b1 | Chris | BOOST_CHECK_EQUAL(result, 3.5); |
| 117 | 46:c666067fb8da | Chris | result = p.findPeakLocation(data, 8, 4); |
| 118 | BOOST_CHECK_EQUAL(result, 3.5); |
||
| 119 | // Likewise if asked to find any peak
|
||
| 120 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 8);
|
| 121 | BOOST_CHECK_EQUAL(result, 3.5); |
||
| 122 | 40:8f56ef28b0b1 | Chris | } |
| 123 | |||
| 124 | BOOST_AUTO_TEST_CASE(shortHalfway) |
||
| 125 | {
|
||
| 126 | 46:c666067fb8da | Chris | // As longHalfway, but with fewer points
|
| 127 | 40:8f56ef28b0b1 | Chris | 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 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 4);
|
| 132 | BOOST_CHECK_EQUAL(result, 1.5); |
||
| 133 | 40:8f56ef28b0b1 | Chris | } |
| 134 | |||
| 135 | 41:16908c2bd781 | Chris | BOOST_AUTO_TEST_CASE(aboveHalfway) |
| 136 | {
|
||
| 137 | 46:c666067fb8da | Chris | // 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 | 41:16908c2bd781 | Chris | 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 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 4);
|
| 145 | BOOST_CHECK(result > 1.5 && result < 2.0); |
||
| 146 | 41:16908c2bd781 | Chris | } |
| 147 | |||
| 148 | BOOST_AUTO_TEST_CASE(belowHalfway) |
||
| 149 | {
|
||
| 150 | 46:c666067fb8da | Chris | // 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 | 41:16908c2bd781 | Chris | 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 | 42:45b4401136f6 | Chris | result = p.findPeakLocation(data, 4);
|
| 158 | BOOST_CHECK(result > 1.0 && result < 1.5); |
||
| 159 | 41:16908c2bd781 | Chris | } |
| 160 | |||
| 161 | 39:822cf7b8e070 | Chris | BOOST_AUTO_TEST_SUITE_END() |