Mercurial > hg > constant-q-cpp
comparison test/TestCQFrequency.cpp @ 135:cb0f0e317a33
Different interpolation types; start on timing tests
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Mon, 19 May 2014 13:02:08 +0100 |
parents | 16822c41b9af |
children | 1aef2b746c64 |
comparison
equal
deleted
inserted
replaced
134:7b48d7ae41e4 | 135:cb0f0e317a33 |
---|---|
40 int bin = (2 * bpo) - round(bpo * log2(freq / cqmin)); | 40 int bin = (2 * bpo) - round(bpo * log2(freq / cqmin)); |
41 return bin; | 41 return bin; |
42 } | 42 } |
43 | 43 |
44 void | 44 void |
45 checkCQFreqColumn(int i, vector<double> column, double freq) | 45 checkCQFreqColumn(int i, vector<double> column, |
46 double freq, CQSpectrogram::Interpolation interp) | |
46 { | 47 { |
47 double maxval = 0.0; | 48 double maxval = 0.0; |
48 int maxidx = -1; | 49 int maxidx = -1; |
49 int height = column.size(); | 50 int height = column.size(); |
50 for (int j = 0; j < height; ++j) { | 51 for (int j = 0; j < height; ++j) { |
51 if (j == 0 || column[j] > maxval) { | 52 if (j == 0 || column[j] > maxval) { |
52 maxval = column[j]; | 53 maxval = column[j]; |
53 maxidx = j; | 54 maxidx = j; |
54 } | 55 } |
55 } | 56 } |
57 | |
58 int nonZeroHeight = height; | |
59 if (interp == CQSpectrogram::InterpolateZeros && i % 2 == 1) { | |
60 nonZeroHeight = height / 2; | |
61 } | |
62 | |
56 int expected = binForFrequency(freq); | 63 int expected = binForFrequency(freq); |
57 if (maxval < threshold) { | 64 if (maxval < threshold) { |
58 return; // ignore these columns at start and end | 65 return; // ignore these columns at start and end |
59 } else { | 66 } else if (expected < nonZeroHeight && maxidx != expected) { |
60 if (maxidx != expected) { | 67 cerr << "ERROR: In column " << i << ", maximum value for frequency " |
61 cerr << "ERROR: In column " << i << ", maximum value for frequency " | 68 << freq << " found at index " << maxidx << endl |
62 << freq << " found at index " << maxidx << endl | 69 << "(expected index " << expected << ")" << endl; |
63 << "(expected index " << expected << ")" << endl; | 70 cerr << "column contains: "; |
64 cerr << "column contains: "; | 71 for (int j = 0; j < height; ++j) { |
65 for (int j = 0; j < height; ++j) { | 72 cerr << column[j] << " "; |
66 cerr << column[j] << " "; | |
67 } | |
68 cerr << endl; | |
69 } | 73 } |
74 cerr << endl; | |
70 BOOST_CHECK_EQUAL(maxidx, expected); | 75 BOOST_CHECK_EQUAL(maxidx, expected); |
71 } | 76 } |
72 } | 77 } |
73 | 78 |
74 void | 79 void |
75 testCQFrequency(double freq) | 80 testCQFrequency(double freq) |
76 { | 81 { |
77 CQParameters params(sampleRate, cqmin, cqmax, bpo); | 82 vector<CQSpectrogram::Interpolation> interpolationTypes; |
78 CQSpectrogram cq(params, CQSpectrogram::InterpolateLinear); | 83 interpolationTypes.push_back(CQSpectrogram::InterpolateZeros); |
84 interpolationTypes.push_back(CQSpectrogram::InterpolateHold); | |
85 interpolationTypes.push_back(CQSpectrogram::InterpolateLinear); | |
79 | 86 |
80 BOOST_CHECK_EQUAL(cq.getBinsPerOctave(), bpo); | 87 for (int k = 0; k < int(interpolationTypes.size()); ++k) { |
81 BOOST_CHECK_EQUAL(cq.getOctaves(), 2); | |
82 | 88 |
83 vector<double> input; | 89 CQSpectrogram::Interpolation interp = interpolationTypes[k]; |
84 for (int i = 0; i < duration; ++i) { | |
85 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate)); | |
86 } | |
87 Window<double>(HanningWindow, duration).cut(input.data()); | |
88 | 90 |
89 CQSpectrogram::RealBlock output = cq.process(input); | 91 CQParameters params(sampleRate, cqmin, cqmax, bpo); |
90 CQSpectrogram::RealBlock rest = cq.getRemainingOutput(); | 92 CQSpectrogram cq(params, interp); |
91 output.insert(output.end(), rest.begin(), rest.end()); | |
92 | 93 |
93 BOOST_CHECK_EQUAL(output[0].size(), cq.getBinsPerOctave() * cq.getOctaves()); | 94 BOOST_CHECK_EQUAL(cq.getBinsPerOctave(), bpo); |
95 BOOST_CHECK_EQUAL(cq.getOctaves(), 2); | |
94 | 96 |
95 for (int i = 0; i < int(output.size()); ++i) { | 97 vector<double> input; |
96 checkCQFreqColumn(i, output[i], freq); | 98 for (int i = 0; i < duration; ++i) { |
99 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate)); | |
100 } | |
101 Window<double>(HanningWindow, duration).cut(input.data()); | |
102 | |
103 CQSpectrogram::RealBlock output = cq.process(input); | |
104 CQSpectrogram::RealBlock rest = cq.getRemainingOutput(); | |
105 output.insert(output.end(), rest.begin(), rest.end()); | |
106 | |
107 BOOST_CHECK_EQUAL(output[0].size(), | |
108 cq.getBinsPerOctave() * cq.getOctaves()); | |
109 | |
110 for (int i = 0; i < int(output.size()); ++i) { | |
111 checkCQFreqColumn(i, output[i], freq, interp); | |
112 } | |
97 } | 113 } |
98 } | 114 } |
99 | 115 |
100 BOOST_AUTO_TEST_CASE(freq_11) { testCQFrequency(11); } | 116 BOOST_AUTO_TEST_CASE(freq_11) { testCQFrequency(11); } |
101 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); } | 117 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); } |