annotate tests/TestWindow.cpp @ 343:24d8ea972643

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