annotate tests/TestWindow.cpp @ 335:0a632ac70945

FFT and window tests
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 30 Sep 2013 16:50:38 +0100
parents
children 469d234acb9e
rev   line source
c@335 1
c@335 2 #include "base/Window.h"
c@335 3
c@335 4 #include <iostream>
c@335 5
c@335 6 #define BOOST_TEST_DYN_LINK
c@335 7 #define BOOST_TEST_MAIN
c@335 8
c@335 9 #include <boost/test/unit_test.hpp>
c@335 10
c@335 11 BOOST_AUTO_TEST_SUITE(TestWindow)
c@335 12
c@335 13 using std::cout;
c@335 14 using std::endl;
c@335 15
c@335 16 #define COMPARE_ARRAY(a, b) \
c@335 17 for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \
c@335 18 BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-4); \
c@335 19 }
c@335 20
c@335 21 void
c@335 22 testSymmetric(double *d, int n)
c@335 23 {
c@335 24 for (int i = 0; i <= n/2; ++i) {
c@335 25 BOOST_CHECK_CLOSE(d[i], d[n-i-1], 1e-10);
c@335 26 }
c@335 27 }
c@335 28
c@335 29 BOOST_AUTO_TEST_CASE(periodic)
c@335 30 {
c@335 31 // We can't actually test whether a function is periodic, given
c@335 32 // only one cycle of it! But we can make sure that all but the
c@335 33 // first sample is symmetric, which is what a symmetric window
c@335 34 // becomes when generated in periodic mode
c@335 35 double d[9];
c@335 36 for (int n = 8; n <= 9; ++n) {
c@335 37 for (int wt = (int)FirstWindow; wt <= (int)LastWindow; ++wt) {
c@335 38 for (int i = 0; i < n; ++i) d[i] = 1.0;
c@335 39 Window<double> w((WindowType)wt, n);
c@335 40 w.cut(d);
c@335 41 testSymmetric(d + 1, n - 1);
c@335 42 }
c@335 43 }
c@335 44 }
c@335 45
c@335 46 template <int N>
c@335 47 void testWindow(WindowType type, const double expected[N])
c@335 48 {
c@335 49 double d[N];
c@335 50 for (int i = 0; i < N; ++i) d[i] = 1.0;
c@335 51 Window<double> w(type, N);
c@335 52 w.cut(d);
c@335 53 COMPARE_ARRAY(d, expected);
c@335 54 }
c@335 55
c@335 56 BOOST_AUTO_TEST_CASE(bartlett)
c@335 57 {
c@335 58 double e1[] = { 1 };
c@335 59 testWindow<1>(BartlettWindow, e1);
c@335 60
c@335 61 double e2[] = { 0, 0 };
c@335 62 testWindow<2>(BartlettWindow, e2);
c@335 63
c@335 64 double e3[] = { 0, 2./3., 2./3. };
c@335 65 testWindow<3>(BartlettWindow, e3);
c@335 66
c@335 67 double e4[] = { 0, 1./2., 1., 1./2. };
c@335 68 testWindow<4>(BartlettWindow, e4);
c@335 69
c@335 70 double e5[] = { 0, 1./2., 1., 1., 1./2. };
c@335 71 testWindow<5>(BartlettWindow, e5);
c@335 72
c@335 73 double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. };
c@335 74 testWindow<6>(BartlettWindow, e6);
c@335 75 }
c@335 76
c@335 77 BOOST_AUTO_TEST_CASE(hamming)
c@335 78 {
c@335 79 double e1[] = { 1 };
c@335 80 testWindow<1>(HammingWindow, e1);
c@335 81
c@335 82 double e10[] = {
c@335 83 0.0800, 0.1679, 0.3979, 0.6821, 0.9121,
c@335 84 1.0000, 0.9121, 0.6821, 0.3979, 0.1679
c@335 85 };
c@335 86 testWindow<10>(HammingWindow, e10);
c@335 87 }
c@335 88
c@335 89 BOOST_AUTO_TEST_CASE(hann)
c@335 90 {
c@335 91 double e1[] = { 1 };
c@335 92 testWindow<1>(HanningWindow, e1);
c@335 93
c@335 94 double e10[] = {
c@335 95 0, 0.0955, 0.3455, 0.6545, 0.9045,
c@335 96 1.0000, 0.9045, 0.6545, 0.3455, 0.0955,
c@335 97 };
c@335 98 testWindow<10>(HanningWindow, e10);
c@335 99 }
c@335 100
c@335 101 BOOST_AUTO_TEST_CASE(blackman)
c@335 102 {
c@335 103 double e1[] = { 1 };
c@335 104 testWindow<1>(BlackmanWindow, e1);
c@335 105
c@335 106 double e10[] = {
c@335 107 0, 0.0402, 0.2008, 0.5098, 0.8492,
c@335 108 1.0000, 0.8492, 0.5098, 0.2008, 0.0402,
c@335 109 };
c@335 110 testWindow<10>(BlackmanWindow, e10);
c@335 111 }
c@335 112
c@335 113 BOOST_AUTO_TEST_SUITE_END()
c@335 114