comparison test/TestCQFrequency.cpp @ 132:c188cade44f8

Tests (not quite correct yet)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 19 May 2014 12:03:04 +0100
parents 6b13f9c694a8
children 16822c41b9af
comparison
equal deleted inserted replaced
131:6b13f9c694a8 132:c188cade44f8
4 4
5 #include "dsp/Window.h" 5 #include "dsp/Window.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <vector> 8 #include <vector>
9 #include <iostream>
9 10
10 using std::vector; 11 using std::vector;
12 using std::cerr;
13 using std::endl;
11 14
12 #define BOOST_TEST_DYN_LINK 15 #define BOOST_TEST_DYN_LINK
13 #define BOOST_TEST_MAIN 16 #define BOOST_TEST_MAIN
14 17
15 #include <boost/test/unit_test.hpp> 18 #include <boost/test/unit_test.hpp>
28 static const double cqmin = 10; 31 static const double cqmin = 10;
29 static const double cqmax = 40; 32 static const double cqmax = 40;
30 static const double bpo = 4; 33 static const double bpo = 4;
31 static const int duration = sampleRate * 2; 34 static const int duration = sampleRate * 2;
32 35
33 // Fairly arbitrary max value for CQ bins other than the "correct" one 36 // Threshold below which to ignore a column completely
34 static const double threshold = 0.08; 37 static const double threshold = 0.08;
35 38
39 int
40 binForFrequency(double freq)
41 {
42 int bin = (2 * bpo) - round(bpo * log2(freq / cqmin));
43 cerr << "binForFrequency: " << freq << " -> " << bin << endl;
44 return bin;
45 }
46
36 void 47 void
37 checkCQFreqOutput(const CQSpectrogram::RealBlock &output, double freq) 48 checkCQFreqColumn(int i, vector<double> column, double freq)
38 { 49 {
39 50 double maxval = 0.0;
51 int maxidx = -1;
52 int height = column.size();
53 for (int j = 0; j < height; ++j) {
54 if (j == 0 || column[j] > maxval) {
55 maxval = column[j];
56 maxidx = j;
57 }
58 }
59 cerr << "maxval = " << maxval << " at " << maxidx << endl;
60 int expected = binForFrequency(freq);
61 if (maxval < threshold) {
62 return; // ignore these columns at start and end
63 } else if (expected < 0 || expected >= height) {
64 cerr << "maxval = " << maxval << endl;
65 BOOST_CHECK(maxval < threshold);
66 } else {
67 BOOST_CHECK_EQUAL(maxidx, expected);
68 }
40 } 69 }
41 70
42 void 71 void
43 testCQFrequency(double freq) 72 testCQFrequency(double freq)
44 { 73 {
45 CQParameters params(sampleRate, cqmin, cqmax, bpo); 74 CQParameters params(sampleRate, cqmin, cqmax, bpo);
46 CQSpectrogram cq(params, CQSpectrogram::InterpolateLinear); 75 CQSpectrogram cq(params, CQSpectrogram::InterpolateLinear);
76
77 BOOST_CHECK_EQUAL(cq.getBinsPerOctave(), bpo);
78 BOOST_CHECK_EQUAL(cq.getOctaves(), 2);
47 79
48 vector<double> input; 80 vector<double> input;
49 for (int i = 0; i < duration; ++i) { 81 for (int i = 0; i < duration; ++i) {
50 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate)); 82 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate));
51 } 83 }
53 85
54 CQSpectrogram::RealBlock output = cq.process(input); 86 CQSpectrogram::RealBlock output = cq.process(input);
55 CQSpectrogram::RealBlock rest = cq.getRemainingOutput(); 87 CQSpectrogram::RealBlock rest = cq.getRemainingOutput();
56 output.insert(output.end(), rest.begin(), rest.end()); 88 output.insert(output.end(), rest.begin(), rest.end());
57 89
58 checkCQFreqOutput(output, freq); 90 BOOST_CHECK_EQUAL(output[0].size(), cq.getBinsPerOctave() * cq.getOctaves());
91
92 for (int i = 0; i < int(output.size()); ++i) {
93 checkCQFreqColumn(i, output[i], freq);
94 }
59 } 95 }
60 96
61 BOOST_AUTO_TEST_CASE(freq_0) { testCQFrequency(0); }
62 BOOST_AUTO_TEST_CASE(freq_5) { testCQFrequency(5); } 97 BOOST_AUTO_TEST_CASE(freq_5) { testCQFrequency(5); }
63 BOOST_AUTO_TEST_CASE(freq_10) { testCQFrequency(10); } 98 BOOST_AUTO_TEST_CASE(freq_10) { testCQFrequency(10); }
64 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); } 99 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); }
65 BOOST_AUTO_TEST_CASE(freq_20) { testCQFrequency(20); } 100 BOOST_AUTO_TEST_CASE(freq_20) { testCQFrequency(20); }
66 BOOST_AUTO_TEST_CASE(freq_25) { testCQFrequency(25); } 101 BOOST_AUTO_TEST_CASE(freq_25) { testCQFrequency(25); }