annotate test/TestCQFrequency.cpp @ 133:16822c41b9af

Crop some excessively optimistic tests. We now fail the same two cases as the yeti implementation does.
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 19 May 2014 12:47:45 +0100
parents c188cade44f8
children cb0f0e317a33
rev   line source
c@131 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@131 2
c@131 3 #include "cq/CQSpectrogram.h"
c@131 4
c@131 5 #include "dsp/Window.h"
c@131 6
c@131 7 #include <cmath>
c@131 8 #include <vector>
c@132 9 #include <iostream>
c@131 10
c@131 11 using std::vector;
c@132 12 using std::cerr;
c@132 13 using std::endl;
c@131 14
c@131 15 #define BOOST_TEST_DYN_LINK
c@131 16 #define BOOST_TEST_MAIN
c@131 17
c@131 18 #include <boost/test/unit_test.hpp>
c@131 19
c@131 20 BOOST_AUTO_TEST_SUITE(TestCQFrequency)
c@131 21
c@131 22 // The principle here is to feed a single windowed sinusoid into a
c@131 23 // small CQ transform and check that the output has its peak bin at
c@133 24 // the correct frequency.
c@131 25
c@131 26 // Set up fs/2 = 50, frequency range 10 -> 40 i.e. 2 octaves, fixed
c@131 27 // duration of 2 seconds
c@131 28 static const double sampleRate = 100;
c@131 29 static const double cqmin = 10;
c@131 30 static const double cqmax = 40;
c@131 31 static const double bpo = 4;
c@131 32 static const int duration = sampleRate * 2;
c@131 33
c@132 34 // Threshold below which to ignore a column completely
c@131 35 static const double threshold = 0.08;
c@131 36
c@132 37 int
c@132 38 binForFrequency(double freq)
c@132 39 {
c@132 40 int bin = (2 * bpo) - round(bpo * log2(freq / cqmin));
c@132 41 return bin;
c@132 42 }
c@132 43
c@131 44 void
c@132 45 checkCQFreqColumn(int i, vector<double> column, double freq)
c@131 46 {
c@132 47 double maxval = 0.0;
c@132 48 int maxidx = -1;
c@132 49 int height = column.size();
c@132 50 for (int j = 0; j < height; ++j) {
c@132 51 if (j == 0 || column[j] > maxval) {
c@132 52 maxval = column[j];
c@132 53 maxidx = j;
c@132 54 }
c@132 55 }
c@132 56 int expected = binForFrequency(freq);
c@132 57 if (maxval < threshold) {
c@132 58 return; // ignore these columns at start and end
c@132 59 } else {
c@133 60 if (maxidx != expected) {
c@133 61 cerr << "ERROR: In column " << i << ", maximum value for frequency "
c@133 62 << freq << " found at index " << maxidx << endl
c@133 63 << "(expected index " << expected << ")" << endl;
c@133 64 cerr << "column contains: ";
c@133 65 for (int j = 0; j < height; ++j) {
c@133 66 cerr << column[j] << " ";
c@133 67 }
c@133 68 cerr << endl;
c@133 69 }
c@132 70 BOOST_CHECK_EQUAL(maxidx, expected);
c@132 71 }
c@131 72 }
c@131 73
c@131 74 void
c@131 75 testCQFrequency(double freq)
c@131 76 {
c@131 77 CQParameters params(sampleRate, cqmin, cqmax, bpo);
c@131 78 CQSpectrogram cq(params, CQSpectrogram::InterpolateLinear);
c@131 79
c@132 80 BOOST_CHECK_EQUAL(cq.getBinsPerOctave(), bpo);
c@132 81 BOOST_CHECK_EQUAL(cq.getOctaves(), 2);
c@132 82
c@131 83 vector<double> input;
c@131 84 for (int i = 0; i < duration; ++i) {
c@131 85 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate));
c@131 86 }
c@131 87 Window<double>(HanningWindow, duration).cut(input.data());
c@131 88
c@131 89 CQSpectrogram::RealBlock output = cq.process(input);
c@131 90 CQSpectrogram::RealBlock rest = cq.getRemainingOutput();
c@131 91 output.insert(output.end(), rest.begin(), rest.end());
c@131 92
c@132 93 BOOST_CHECK_EQUAL(output[0].size(), cq.getBinsPerOctave() * cq.getOctaves());
c@132 94
c@132 95 for (int i = 0; i < int(output.size()); ++i) {
c@132 96 checkCQFreqColumn(i, output[i], freq);
c@132 97 }
c@131 98 }
c@131 99
c@133 100 BOOST_AUTO_TEST_CASE(freq_11) { testCQFrequency(11); }
c@131 101 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); }
c@131 102 BOOST_AUTO_TEST_CASE(freq_20) { testCQFrequency(20); }
c@131 103 BOOST_AUTO_TEST_CASE(freq_25) { testCQFrequency(25); }
c@131 104 BOOST_AUTO_TEST_CASE(freq_30) { testCQFrequency(30); }
c@131 105 BOOST_AUTO_TEST_CASE(freq_35) { testCQFrequency(35); }
c@131 106 BOOST_AUTO_TEST_CASE(freq_40) { testCQFrequency(40); }
c@131 107
c@131 108 BOOST_AUTO_TEST_SUITE_END()
c@131 109