To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / TestPeakInterpolator.cpp @ 42:45b4401136f6

History | View | Annotate | Download (4.29 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
    double data[] = { 0.0, 1.0, 0.0 };
37
    PeakInterpolator p;
38
    double result = p.findPeakLocation(data, 3, 1);
39
    BOOST_CHECK_EQUAL(result, 1.0);
40 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 3);
41
    BOOST_CHECK_EQUAL(result, 1.0);
42 39:822cf7b8e070 Chris
}
43
44
BOOST_AUTO_TEST_CASE(peakAtSample_N5)
45
{
46
    double data[] = { 0.0, 1.0, 2.0, 1.0, 0.0 };
47
    PeakInterpolator p;
48
    double result = p.findPeakLocation(data, 5, 2);
49
    BOOST_CHECK_EQUAL(result, 2.0);
50 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 5);
51
    BOOST_CHECK_EQUAL(result, 2.0);
52 39:822cf7b8e070 Chris
}
53
54
BOOST_AUTO_TEST_CASE(flat)
55
{
56
    double data[] = { 1.0, 1.0, 1.0, 1.0, 1.0 };
57
    PeakInterpolator p;
58
    double result = p.findPeakLocation(data, 5, 2);
59
    BOOST_CHECK_EQUAL(result, 2.0);
60 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 5);
61
    BOOST_CHECK_EQUAL(result, 0.0);
62 39:822cf7b8e070 Chris
}
63
64
BOOST_AUTO_TEST_CASE(multiPeak)
65
{
66
    double data[] = { 1.0, 2.0, 1.0, 2.0, 1.0 };
67
    PeakInterpolator p;
68
    double result = p.findPeakLocation(data, 5, 3);
69
    BOOST_CHECK_EQUAL(result, 3.0);
70 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 5);
71
    BOOST_CHECK_EQUAL(result, 1.0);
72 39:822cf7b8e070 Chris
}
73
74 40:8f56ef28b0b1 Chris
BOOST_AUTO_TEST_CASE(start)
75
{
76
    // Can't meaningfully interpolate if we're identifying element 0
77
    // as the peak
78
    double data[] = { 1.0, 1.0, 0.0, 0.0 };
79
    PeakInterpolator p;
80
    double result = p.findPeakLocation(data, 4, 0);
81
    BOOST_CHECK_EQUAL(result, 0.0);
82
}
83
84
BOOST_AUTO_TEST_CASE(end)
85
{
86
    // Likewise for the final element
87
    double data[] = { 0.0, 0.0, 1.0, 1.0 };
88
    PeakInterpolator p;
89
    double result = p.findPeakLocation(data, 4, 3);
90
    BOOST_CHECK_EQUAL(result, 3.0);
91 42:45b4401136f6 Chris
    // But when running without a peak location, we expect idx 2 to be
92
    // picked as peak, not idx 3, so that will result in interpolation
93
    result = p.findPeakLocation(data, 4);
94
    BOOST_CHECK(result > 2.0 && result < 3.0);
95 40:8f56ef28b0b1 Chris
}
96
97
BOOST_AUTO_TEST_CASE(longHalfway)
98
{
99
    double data[] = { 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0 };
100
    PeakInterpolator p;
101
    double result = p.findPeakLocation(data, 8, 4);
102
    BOOST_CHECK_EQUAL(result, 3.5);
103 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 8);
104
    BOOST_CHECK_EQUAL(result, 3.5);
105 40:8f56ef28b0b1 Chris
}
106
107
BOOST_AUTO_TEST_CASE(shortHalfway)
108
{
109
    double data[] = { 1.0, 2.0, 2.0, 1.0 };
110
    PeakInterpolator p;
111
    double result = p.findPeakLocation(data, 4, 1);
112
    BOOST_CHECK_EQUAL(result, 1.5);
113 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 4);
114
    BOOST_CHECK_EQUAL(result, 1.5);
115 40:8f56ef28b0b1 Chris
}
116
117 41:16908c2bd781 Chris
BOOST_AUTO_TEST_CASE(aboveHalfway)
118
{
119
    double data[] = { 1.0, 1.5, 2.0, 1.0 };
120
    PeakInterpolator p;
121
    double result = p.findPeakLocation(data, 4, 2);
122
    BOOST_CHECK(result > 1.5 && result < 2.0);
123 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 4);
124
    BOOST_CHECK(result > 1.5 && result < 2.0);
125 41:16908c2bd781 Chris
}
126
127
BOOST_AUTO_TEST_CASE(belowHalfway)
128
{
129
    double data[] = { 1.0, 2.0, 1.5, 1.0 };
130
    PeakInterpolator p;
131
    double result = p.findPeakLocation(data, 4, 1);
132
    BOOST_CHECK(result > 1.0 && result < 1.5);
133 42:45b4401136f6 Chris
    result = p.findPeakLocation(data, 4);
134
    BOOST_CHECK(result > 1.0 && result < 1.5);
135 41:16908c2bd781 Chris
}
136
137 39:822cf7b8e070 Chris
BOOST_AUTO_TEST_SUITE_END()