annotate tests/TestWindow.cpp @ 349:b247af4c23d2

Add Kaiser window (and some tests for it)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 04 Oct 2013 18:46:32 +0100
parents 24d8ea972643
children 7669b3aa3bc9
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@349 4 #include "base/KaiserWindow.h"
c@335 5
c@335 6 #include <iostream>
c@335 7
c@335 8 #define BOOST_TEST_DYN_LINK
c@335 9 #define BOOST_TEST_MAIN
c@335 10
c@335 11 #include <boost/test/unit_test.hpp>
c@335 12
c@335 13 BOOST_AUTO_TEST_SUITE(TestWindow)
c@335 14
c@335 15 using std::cout;
c@335 16 using std::endl;
c@335 17
c@335 18 #define COMPARE_ARRAY(a, b) \
c@335 19 for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \
c@335 20 BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-4); \
c@335 21 }
c@335 22
c@335 23 void
c@335 24 testSymmetric(double *d, int n)
c@335 25 {
c@335 26 for (int i = 0; i <= n/2; ++i) {
c@335 27 BOOST_CHECK_CLOSE(d[i], d[n-i-1], 1e-10);
c@335 28 }
c@335 29 }
c@335 30
c@335 31 BOOST_AUTO_TEST_CASE(periodic)
c@335 32 {
c@335 33 // We can't actually test whether a function is periodic, given
c@335 34 // only one cycle of it! But we can make sure that all but the
c@335 35 // first sample is symmetric, which is what a symmetric window
c@335 36 // becomes when generated in periodic mode
c@335 37 double d[9];
c@335 38 for (int n = 8; n <= 9; ++n) {
c@335 39 for (int wt = (int)FirstWindow; wt <= (int)LastWindow; ++wt) {
c@335 40 for (int i = 0; i < n; ++i) d[i] = 1.0;
c@335 41 Window<double> w((WindowType)wt, n);
c@335 42 w.cut(d);
c@335 43 testSymmetric(d + 1, n - 1);
c@335 44 }
c@335 45 }
c@335 46 }
c@335 47
c@335 48 template <int N>
c@335 49 void testWindow(WindowType type, const double expected[N])
c@335 50 {
c@335 51 double d[N];
c@335 52 for (int i = 0; i < N; ++i) d[i] = 1.0;
c@335 53 Window<double> w(type, N);
c@335 54 w.cut(d);
c@335 55 COMPARE_ARRAY(d, expected);
c@336 56
c@336 57 double d0[N], d1[N];
c@336 58 for (int i = 0; i < N; ++i) d0[i] = 0.5 + (1.0 / (N * 2)) * (i + 1);
c@336 59 w.cut(d0, d1);
c@336 60 for (int i = 0; i < N; ++i) {
c@336 61 BOOST_CHECK_SMALL(d1[i] - d0[i] * expected[i], 1e-4);
c@336 62 }
c@335 63 }
c@335 64
c@335 65 BOOST_AUTO_TEST_CASE(bartlett)
c@335 66 {
c@335 67 double e1[] = { 1 };
c@335 68 testWindow<1>(BartlettWindow, e1);
c@335 69
c@335 70 double e2[] = { 0, 0 };
c@335 71 testWindow<2>(BartlettWindow, e2);
c@335 72
c@335 73 double e3[] = { 0, 2./3., 2./3. };
c@335 74 testWindow<3>(BartlettWindow, e3);
c@335 75
c@335 76 double e4[] = { 0, 1./2., 1., 1./2. };
c@335 77 testWindow<4>(BartlettWindow, e4);
c@335 78
c@335 79 double e5[] = { 0, 1./2., 1., 1., 1./2. };
c@335 80 testWindow<5>(BartlettWindow, e5);
c@335 81
c@335 82 double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. };
c@335 83 testWindow<6>(BartlettWindow, e6);
c@335 84 }
c@335 85
c@335 86 BOOST_AUTO_TEST_CASE(hamming)
c@335 87 {
c@335 88 double e1[] = { 1 };
c@335 89 testWindow<1>(HammingWindow, e1);
c@335 90
c@335 91 double e10[] = {
c@335 92 0.0800, 0.1679, 0.3979, 0.6821, 0.9121,
c@335 93 1.0000, 0.9121, 0.6821, 0.3979, 0.1679
c@335 94 };
c@335 95 testWindow<10>(HammingWindow, e10);
c@335 96 }
c@335 97
c@335 98 BOOST_AUTO_TEST_CASE(hann)
c@335 99 {
c@335 100 double e1[] = { 1 };
c@335 101 testWindow<1>(HanningWindow, e1);
c@335 102
c@335 103 double e10[] = {
c@335 104 0, 0.0955, 0.3455, 0.6545, 0.9045,
c@335 105 1.0000, 0.9045, 0.6545, 0.3455, 0.0955,
c@335 106 };
c@335 107 testWindow<10>(HanningWindow, e10);
c@335 108 }
c@335 109
c@335 110 BOOST_AUTO_TEST_CASE(blackman)
c@335 111 {
c@335 112 double e1[] = { 1 };
c@335 113 testWindow<1>(BlackmanWindow, e1);
c@335 114
c@335 115 double e10[] = {
c@335 116 0, 0.0402, 0.2008, 0.5098, 0.8492,
c@335 117 1.0000, 0.8492, 0.5098, 0.2008, 0.0402,
c@335 118 };
c@335 119 testWindow<10>(BlackmanWindow, e10);
c@335 120 }
c@349 121
c@349 122 BOOST_AUTO_TEST_CASE(kaiser_4_10)
c@349 123 {
c@349 124 double d[10];
c@349 125 for (int i = 0; i < 10; ++i) d[i] = 1.0;
c@349 126 double e[] = {
c@349 127 0.0885, 0.2943, 0.5644, 0.8216, 0.9789,
c@349 128 0.9789, 0.8216, 0.5644, 0.2943, 0.0885
c@349 129 };
c@349 130 KaiserWindow::Parameters p;
c@349 131 p.length = 10;
c@349 132 p.beta = 4;
c@349 133 KaiserWindow k(p);
c@349 134 k.cut(d);
c@349 135 COMPARE_ARRAY(d, e);
c@349 136 }
c@335 137
c@349 138 BOOST_AUTO_TEST_CASE(kaiser_2p5_11)
c@349 139 {
c@349 140 double d[11];
c@349 141 for (int i = 0; i < 11; ++i) d[i] = 1.0;
c@349 142 double e[] = {
c@349 143 0.3040, 0.5005, 0.6929, 0.8546, 0.9622,
c@349 144 1.0000, 0.9622, 0.8546, 0.6929, 0.5005, 0.3040
c@349 145 };
c@349 146 KaiserWindow::Parameters p;
c@349 147 p.length = 11;
c@349 148 p.beta = 2.5;
c@349 149 KaiserWindow k(p);
c@349 150 k.cut(d);
c@349 151 COMPARE_ARRAY(d, e);
c@349 152 }
c@349 153
c@349 154 //!!! todo: tests for kaiser with attenuation and bandwidth parameters
c@349 155
c@335 156 BOOST_AUTO_TEST_SUITE_END()
c@335 157