comparison test/TestCQFrequency.cpp @ 139:1aef2b746c64

More (failing) tests
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 19 May 2014 17:46:40 +0100
parents cb0f0e317a33
children 9bf76fc43844
comparison
equal deleted inserted replaced
138:7563025cc1b1 139:1aef2b746c64
24 // the correct frequency. 24 // the correct frequency.
25 25
26 // Set up fs/2 = 50, frequency range 10 -> 40 i.e. 2 octaves, fixed 26 // Set up fs/2 = 50, frequency range 10 -> 40 i.e. 2 octaves, fixed
27 // duration of 2 seconds 27 // duration of 2 seconds
28 static const double sampleRate = 100; 28 static const double sampleRate = 100;
29 static const double cqmin = 10; 29 static const double cqmin = 11.8921;
30 static const double cqmax = 40; 30 static const double cqmax = 40;
31 static const double bpo = 4; 31 static const double bpo = 4;
32 static const int duration = sampleRate * 2; 32 static const int duration = sampleRate * 2;
33 33
34 // Threshold below which to ignore a column completely 34 // Threshold below which to ignore a column completely
35 static const double threshold = 0.08; 35 static const double threshold = 0.08;
36 36
37 int 37 int
38 binForFrequency(double freq) 38 binForFrequency(double freq)
39 { 39 {
40 int bin = (2 * bpo) - round(bpo * log2(freq / cqmin)); 40 int bin = (bpo * 2) - round(bpo * log2(freq / cqmin)) - 1;
41 return bin; 41 return bin;
42 } 42 }
43 43
44 void 44 void
45 checkCQFreqColumn(int i, vector<double> column, 45 checkCQFreqColumn(int i, vector<double> column,
46 double freq, CQSpectrogram::Interpolation interp) 46 double freq, CQSpectrogram::Interpolation interp)
47 { 47 {
48 double maxval = 0.0; 48 double maxval = 0.0;
49 int maxidx = -1; 49 int maxidx = -1;
50 int height = column.size(); 50 int height = column.size();
51 for (int j = 0; j < height; ++j) { 51
52 int nonZeroHeight = ((i % 2 == 1) ? height/2 : height);
53
54 for (int j = 0; j < nonZeroHeight; ++j) {
52 if (j == 0 || column[j] > maxval) { 55 if (j == 0 || column[j] > maxval) {
53 maxval = column[j]; 56 maxval = column[j];
54 maxidx = j; 57 maxidx = j;
55 } 58 }
56 } 59 }
57 60
58 int nonZeroHeight = height;
59 if (interp == CQSpectrogram::InterpolateZeros && i % 2 == 1) {
60 nonZeroHeight = height / 2;
61 }
62
63 int expected = binForFrequency(freq); 61 int expected = binForFrequency(freq);
64 if (maxval < threshold) { 62 if (maxval < threshold) {
65 return; // ignore these columns at start and end 63 return; // ignore these columns at start and end
66 } else if (expected < nonZeroHeight && maxidx != expected) { 64 } else if (expected < nonZeroHeight && maxidx != expected) {
67 cerr << "ERROR: In column " << i << ", maximum value for frequency " 65 cerr << "ERROR: In column " << i << " with interpolation " << interp
68 << freq << " found at index " << maxidx << endl 66 << ", maximum value for frequency " << freq
69 << "(expected index " << expected << ")" << endl; 67 << "\n found at index " << maxidx
68 << " (expected index " << expected << ")" << endl;
70 cerr << "column contains: "; 69 cerr << "column contains: ";
71 for (int j = 0; j < height; ++j) { 70 for (int j = 0; j < height; ++j) {
72 cerr << column[j] << " "; 71 cerr << column[j] << " ";
73 } 72 }
74 cerr << endl; 73 cerr << endl;
89 CQSpectrogram::Interpolation interp = interpolationTypes[k]; 88 CQSpectrogram::Interpolation interp = interpolationTypes[k];
90 89
91 CQParameters params(sampleRate, cqmin, cqmax, bpo); 90 CQParameters params(sampleRate, cqmin, cqmax, bpo);
92 CQSpectrogram cq(params, interp); 91 CQSpectrogram cq(params, interp);
93 92
93 cerr << "cq freq " << freq << ", binForFrequency " << binForFrequency(freq) << endl;
94
95 cerr << "bin freqs: ";
96 for (int i = 0; i < cq.getBinsPerOctave() * cq.getOctaves(); ++i) {
97 cerr << i << ": " << cq.getBinFrequency(i) << ", ";
98 }
99 cerr << endl;
100
94 BOOST_CHECK_EQUAL(cq.getBinsPerOctave(), bpo); 101 BOOST_CHECK_EQUAL(cq.getBinsPerOctave(), bpo);
95 BOOST_CHECK_EQUAL(cq.getOctaves(), 2); 102 BOOST_CHECK_EQUAL(cq.getOctaves(), 2);
103 BOOST_CHECK_CLOSE(cq.getBinFrequency(0), 40, 1e-10);
104 BOOST_CHECK_CLOSE(cq.getBinFrequency(4), 20, 1e-10);
105 BOOST_CHECK_CLOSE(cq.getBinFrequency(7), cqmin, 1e-3);
96 106
97 vector<double> input; 107 vector<double> input;
98 for (int i = 0; i < duration; ++i) { 108 for (int i = 0; i < duration; ++i) {
99 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate)); 109 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate));
100 } 110 }
112 } 122 }
113 } 123 }
114 } 124 }
115 125
116 BOOST_AUTO_TEST_CASE(freq_11) { testCQFrequency(11); } 126 BOOST_AUTO_TEST_CASE(freq_11) { testCQFrequency(11); }
117 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); } 127 BOOST_AUTO_TEST_CASE(freq_16) { testCQFrequency(16); }
118 BOOST_AUTO_TEST_CASE(freq_20) { testCQFrequency(20); } 128 BOOST_AUTO_TEST_CASE(freq_23) { testCQFrequency(23); }
119 BOOST_AUTO_TEST_CASE(freq_25) { testCQFrequency(25); } 129 BOOST_AUTO_TEST_CASE(freq_27) { testCQFrequency(27); }
120 BOOST_AUTO_TEST_CASE(freq_30) { testCQFrequency(30); } 130 BOOST_AUTO_TEST_CASE(freq_33) { testCQFrequency(33); }
121 BOOST_AUTO_TEST_CASE(freq_35) { testCQFrequency(35); }
122 BOOST_AUTO_TEST_CASE(freq_40) { testCQFrequency(40); } 131 BOOST_AUTO_TEST_CASE(freq_40) { testCQFrequency(40); }
123 132
124 BOOST_AUTO_TEST_SUITE_END() 133 BOOST_AUTO_TEST_SUITE_END()
125 134