annotate tests/TestWindow.cpp @ 336:f665f9ce2fd1

Test two-arg cut as well
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 01 Oct 2013 15:15:59 +0100
parents 0a632ac70945
children 4920d100b290
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@336 54
c@336 55 double d0[N], d1[N];
c@336 56 for (int i = 0; i < N; ++i) d0[i] = 0.5 + (1.0 / (N * 2)) * (i + 1);
c@336 57 w.cut(d0, d1);
c@336 58 for (int i = 0; i < N; ++i) {
c@336 59 BOOST_CHECK_SMALL(d1[i] - d0[i] * expected[i], 1e-4);
c@336 60 }
c@335 61 }
c@335 62
c@335 63 BOOST_AUTO_TEST_CASE(bartlett)
c@335 64 {
c@335 65 double e1[] = { 1 };
c@335 66 testWindow<1>(BartlettWindow, e1);
c@335 67
c@335 68 double e2[] = { 0, 0 };
c@335 69 testWindow<2>(BartlettWindow, e2);
c@335 70
c@335 71 double e3[] = { 0, 2./3., 2./3. };
c@335 72 testWindow<3>(BartlettWindow, e3);
c@335 73
c@335 74 double e4[] = { 0, 1./2., 1., 1./2. };
c@335 75 testWindow<4>(BartlettWindow, e4);
c@335 76
c@335 77 double e5[] = { 0, 1./2., 1., 1., 1./2. };
c@335 78 testWindow<5>(BartlettWindow, e5);
c@335 79
c@335 80 double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. };
c@335 81 testWindow<6>(BartlettWindow, e6);
c@335 82 }
c@335 83
c@335 84 BOOST_AUTO_TEST_CASE(hamming)
c@335 85 {
c@335 86 double e1[] = { 1 };
c@335 87 testWindow<1>(HammingWindow, e1);
c@335 88
c@335 89 double e10[] = {
c@335 90 0.0800, 0.1679, 0.3979, 0.6821, 0.9121,
c@335 91 1.0000, 0.9121, 0.6821, 0.3979, 0.1679
c@335 92 };
c@335 93 testWindow<10>(HammingWindow, e10);
c@335 94 }
c@335 95
c@335 96 BOOST_AUTO_TEST_CASE(hann)
c@335 97 {
c@335 98 double e1[] = { 1 };
c@335 99 testWindow<1>(HanningWindow, e1);
c@335 100
c@335 101 double e10[] = {
c@335 102 0, 0.0955, 0.3455, 0.6545, 0.9045,
c@335 103 1.0000, 0.9045, 0.6545, 0.3455, 0.0955,
c@335 104 };
c@335 105 testWindow<10>(HanningWindow, e10);
c@335 106 }
c@335 107
c@335 108 BOOST_AUTO_TEST_CASE(blackman)
c@335 109 {
c@335 110 double e1[] = { 1 };
c@335 111 testWindow<1>(BlackmanWindow, e1);
c@335 112
c@335 113 double e10[] = {
c@335 114 0, 0.0402, 0.2008, 0.5098, 0.8492,
c@335 115 1.0000, 0.8492, 0.5098, 0.2008, 0.0402,
c@335 116 };
c@335 117 testWindow<10>(BlackmanWindow, e10);
c@335 118 }
c@335 119
c@335 120 BOOST_AUTO_TEST_SUITE_END()
c@335 121