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