Mercurial > hg > match-vamp
comparison test/TestFeatureExtractor.cpp @ 127:2ed42b7616c5 refactors
Non-chroma test
author | Chris Cannam |
---|---|
date | Thu, 11 Dec 2014 11:58:46 +0000 |
parents | 0ed1adb2d522 |
children | 3f32a88ee15a |
comparison
equal
deleted
inserted
replaced
126:0ed1adb2d522 | 127:2ed42b7616c5 |
---|---|
18 } | 18 } |
19 | 19 |
20 static int freq2chroma(double freq) | 20 static int freq2chroma(double freq) |
21 { | 21 { |
22 return freq2mid(freq) % 12; | 22 return freq2mid(freq) % 12; |
23 } | |
24 | |
25 static int bin2warped(int bin, int rate, int sz) | |
26 { | |
27 // see comments in nonchroma below | |
28 if (bin <= 33) return bin; | |
29 double freq = (double(bin) * rate) / sz; | |
30 int mid = freq2mid(freq); | |
31 if (mid > 127) mid = 127; | |
32 int outbin = mid - 77 + 33; | |
33 return outbin; | |
23 } | 34 } |
24 | 35 |
25 BOOST_AUTO_TEST_SUITE(TestFeatureExtractor) | 36 BOOST_AUTO_TEST_SUITE(TestFeatureExtractor) |
26 | 37 |
27 BOOST_AUTO_TEST_CASE(chroma) | 38 BOOST_AUTO_TEST_CASE(chroma) |
91 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), | 102 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), |
92 expected.begin(), expected.end()); | 103 expected.begin(), expected.end()); |
93 } | 104 } |
94 } | 105 } |
95 | 106 |
107 BOOST_AUTO_TEST_CASE(nonchroma) | |
108 { | |
109 int rate = 44100; | |
110 int sz = 2048; | |
111 int hs = sz / 2 + 1; | |
112 int fsz = 84; | |
113 | |
114 FeatureExtractor::Parameters params(rate, sz); | |
115 FeatureExtractor fe(params); | |
116 BOOST_CHECK_EQUAL(fe.getFeatureSize(), fsz); | |
117 | |
118 for (int bin = 0; bin < hs; ++bin) { | |
119 | |
120 vector<double> real, imag; | |
121 real.resize(hs, 0.0); | |
122 imag.resize(hs, 0.0); | |
123 | |
124 real[bin] += 10.0; | |
125 imag[bin] += 10.0; | |
126 | |
127 // use two input sweeps, so we can test that they are properly | |
128 // summed into the output bin | |
129 real[hs-bin-1] += 5.0; | |
130 imag[hs-bin-1] += 5.0; | |
131 | |
132 vector<double> out = fe.process(real, imag); | |
133 | |
134 // We expect to find all bins are 0 except for: | |
135 // | |
136 // * two bins of 200 and 50 respectively, if the two input | |
137 // bins are distinct and their output bins are also distinct | |
138 // | |
139 // * one bin of value 250 (= 10^2 + 5^2), if the two input | |
140 // bins are distinct but their output bins are not | |
141 // | |
142 // * one bin of value 450 (= 15^2 + 15^2), if the input bins | |
143 // are not distinct | |
144 // | |
145 // The first 34 input bins (i.e. up to and including bin 33, | |
146 // 733Hz, MIDI pitch 77.something) are mapped linearly, those | |
147 // above that and up to MIDI pitch 127 (12544Hz) are mapped | |
148 // logarithmically, remaining bins are all mapped into the | |
149 // final output bin. | |
150 // | |
151 // So MIDI pitches up to and including 77 are mapped linearly | |
152 // by frequency into 34 bins; those from 78-126 inclusive are | |
153 // mapped linearly by MIDI pitch into the next 49 bins; | |
154 // everything above goes into the last bin, for 84 bins total. | |
155 | |
156 vector<double> expected(fsz); | |
157 | |
158 if (bin == hs-bin-1) { | |
159 expected[bin2warped(bin, rate, sz)] += 450; | |
160 } else { | |
161 expected[bin2warped(bin, rate, sz)] += 200; | |
162 expected[bin2warped(hs-bin-1, rate, sz)] += 50; | |
163 } | |
164 | |
165 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), | |
166 expected.begin(), expected.end()); | |
167 } | |
168 } | |
169 | |
96 BOOST_AUTO_TEST_SUITE_END() | 170 BOOST_AUTO_TEST_SUITE_END() |