comparison test/TestFeatureExtractor.cpp @ 126:0ed1adb2d522 refactors

Chroma feature test case
author Chris Cannam
date Thu, 11 Dec 2014 11:41:25 +0000
parents
children 2ed42b7616c5
comparison
equal deleted inserted replaced
125:4ca5e4219684 126:0ed1adb2d522
1
2 #include "FeatureExtractor.h"
3
4 #include <vector>
5 #include <iostream>
6 #include <cmath>
7
8 using namespace std;
9
10 #define BOOST_TEST_DYN_LINK
11 #define BOOST_TEST_MAIN
12
13 #include <boost/test/unit_test.hpp>
14
15 static int freq2mid(double freq)
16 {
17 return round(57.0 + 12.0 * log(freq / 220.) / log(2.));
18 }
19
20 static int freq2chroma(double freq)
21 {
22 return freq2mid(freq) % 12;
23 }
24
25 BOOST_AUTO_TEST_SUITE(TestFeatureExtractor)
26
27 BOOST_AUTO_TEST_CASE(chroma)
28 {
29 int rate = 44100;
30 int sz = 2048;
31 int hs = sz / 2 + 1;
32 int fsz = 13;
33
34 FeatureExtractor::Parameters params(rate, sz);
35 params.useChromaFrequencyMap = true;
36 FeatureExtractor fe(params);
37 BOOST_CHECK_EQUAL(fe.getFeatureSize(), fsz);
38
39 for (int bin = 0; bin < hs; ++bin) {
40
41 vector<double> real, imag;
42 real.resize(hs, 0.0);
43 imag.resize(hs, 0.0);
44
45 real[bin] += 10.0;
46 imag[bin] += 10.0;
47
48 // use two input sweeps, so we can test that they are properly
49 // summed into the output bin
50 real[hs-bin-1] += 5.0;
51 imag[hs-bin-1] += 5.0;
52
53 vector<double> out = fe.process(real, imag);
54
55 // We expect to find all bins are 0 except for:
56 //
57 // * two bins of 200 and 50 respectively, if the two input
58 // bins are distinct and their output chroma are also distinct
59 //
60 // * one bin of value 250 (= 10^2 + 5^2), if the two input
61 // bins are distinct but their output chroma are not
62 //
63 // * one bin of value 450 (= 15^2 + 15^2), if the input bins
64 // are not distinct
65 //
66 // The bin corresponding to each input frequency is that of
67 // its semitone value (with C=0), except that input bin
68 // frequencies less than 362Hz are shepherded into the
69 // separate bin 0 (see docs in FeatureExtractor.h)
70
71 vector<double> expected(fsz);
72
73 double infreq1 = (double(bin) * rate) / sz;
74
75 if (bin == hs-bin-1) {
76 expected[freq2chroma(infreq1) + 1] += 450;
77 } else {
78 if (infreq1 < 362) {
79 expected[0] += 200;
80 } else {
81 expected[freq2chroma(infreq1) + 1] += 200;
82 }
83 double infreq2 = (double(hs-bin-1) * rate) / sz;
84 if (infreq2 < 362) {
85 expected[0] += 50;
86 } else {
87 expected[freq2chroma(infreq2) + 1] += 50;
88 }
89 }
90
91 BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(),
92 expected.begin(), expected.end());
93 }
94 }
95
96 BOOST_AUTO_TEST_SUITE_END()