annotate test/TestCQFrequency.cpp @ 131:6b13f9c694a8

Start introducing the tests
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 19 May 2014 10:21:51 +0100
parents
children c188cade44f8
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@131 9
c@131 10 using std::vector;
c@131 11
c@131 12 #define BOOST_TEST_DYN_LINK
c@131 13 #define BOOST_TEST_MAIN
c@131 14
c@131 15 #include <boost/test/unit_test.hpp>
c@131 16
c@131 17 BOOST_AUTO_TEST_SUITE(TestCQFrequency)
c@131 18
c@131 19 // The principle here is to feed a single windowed sinusoid into a
c@131 20 // small CQ transform and check that the output has its peak bin at
c@131 21 // the correct frequency. We can repeat for different frequencies both
c@131 22 // inside and outside the frequency range supported by the CQ. We
c@131 23 // should also repeat for CQSpectrogram outputs as well as the raw CQ.
c@131 24
c@131 25 // Set up fs/2 = 50, frequency range 10 -> 40 i.e. 2 octaves, fixed
c@131 26 // duration of 2 seconds
c@131 27 static const double sampleRate = 100;
c@131 28 static const double cqmin = 10;
c@131 29 static const double cqmax = 40;
c@131 30 static const double bpo = 4;
c@131 31 static const int duration = sampleRate * 2;
c@131 32
c@131 33 // Fairly arbitrary max value for CQ bins other than the "correct" one
c@131 34 static const double threshold = 0.08;
c@131 35
c@131 36 void
c@131 37 checkCQFreqOutput(const CQSpectrogram::RealBlock &output, double freq)
c@131 38 {
c@131 39
c@131 40 }
c@131 41
c@131 42 void
c@131 43 testCQFrequency(double freq)
c@131 44 {
c@131 45 CQParameters params(sampleRate, cqmin, cqmax, bpo);
c@131 46 CQSpectrogram cq(params, CQSpectrogram::InterpolateLinear);
c@131 47
c@131 48 vector<double> input;
c@131 49 for (int i = 0; i < duration; ++i) {
c@131 50 input.push_back(sin((i * 2 * M_PI * freq) / sampleRate));
c@131 51 }
c@131 52 Window<double>(HanningWindow, duration).cut(input.data());
c@131 53
c@131 54 CQSpectrogram::RealBlock output = cq.process(input);
c@131 55 CQSpectrogram::RealBlock rest = cq.getRemainingOutput();
c@131 56 output.insert(output.end(), rest.begin(), rest.end());
c@131 57
c@131 58 checkCQFreqOutput(output, freq);
c@131 59 }
c@131 60
c@131 61 BOOST_AUTO_TEST_CASE(freq_0) { testCQFrequency(0); }
c@131 62 BOOST_AUTO_TEST_CASE(freq_5) { testCQFrequency(5); }
c@131 63 BOOST_AUTO_TEST_CASE(freq_10) { testCQFrequency(10); }
c@131 64 BOOST_AUTO_TEST_CASE(freq_15) { testCQFrequency(15); }
c@131 65 BOOST_AUTO_TEST_CASE(freq_20) { testCQFrequency(20); }
c@131 66 BOOST_AUTO_TEST_CASE(freq_25) { testCQFrequency(25); }
c@131 67 BOOST_AUTO_TEST_CASE(freq_30) { testCQFrequency(30); }
c@131 68 BOOST_AUTO_TEST_CASE(freq_35) { testCQFrequency(35); }
c@131 69 BOOST_AUTO_TEST_CASE(freq_40) { testCQFrequency(40); }
c@131 70 BOOST_AUTO_TEST_CASE(freq_45) { testCQFrequency(45); }
c@131 71 BOOST_AUTO_TEST_CASE(freq_50) { testCQFrequency(50); }
c@131 72
c@131 73 BOOST_AUTO_TEST_SUITE_END()
c@131 74