c@335: c@335: #include "base/Window.h" c@335: c@335: #include c@335: c@335: #define BOOST_TEST_DYN_LINK c@335: #define BOOST_TEST_MAIN c@335: c@335: #include c@335: c@335: BOOST_AUTO_TEST_SUITE(TestWindow) c@335: c@335: using std::cout; c@335: using std::endl; c@335: c@335: #define COMPARE_ARRAY(a, b) \ c@335: for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ c@335: BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-4); \ c@335: } c@335: c@335: void c@335: testSymmetric(double *d, int n) c@335: { c@335: for (int i = 0; i <= n/2; ++i) { c@335: BOOST_CHECK_CLOSE(d[i], d[n-i-1], 1e-10); c@335: } c@335: } c@335: c@335: BOOST_AUTO_TEST_CASE(periodic) c@335: { c@335: // We can't actually test whether a function is periodic, given c@335: // only one cycle of it! But we can make sure that all but the c@335: // first sample is symmetric, which is what a symmetric window c@335: // becomes when generated in periodic mode c@335: double d[9]; c@335: for (int n = 8; n <= 9; ++n) { c@335: for (int wt = (int)FirstWindow; wt <= (int)LastWindow; ++wt) { c@335: for (int i = 0; i < n; ++i) d[i] = 1.0; c@335: Window w((WindowType)wt, n); c@335: w.cut(d); c@335: testSymmetric(d + 1, n - 1); c@335: } c@335: } c@335: } c@335: c@335: template c@335: void testWindow(WindowType type, const double expected[N]) c@335: { c@335: double d[N]; c@335: for (int i = 0; i < N; ++i) d[i] = 1.0; c@335: Window w(type, N); c@335: w.cut(d); c@335: COMPARE_ARRAY(d, expected); c@335: } c@335: c@335: BOOST_AUTO_TEST_CASE(bartlett) c@335: { c@335: double e1[] = { 1 }; c@335: testWindow<1>(BartlettWindow, e1); c@335: c@335: double e2[] = { 0, 0 }; c@335: testWindow<2>(BartlettWindow, e2); c@335: c@335: double e3[] = { 0, 2./3., 2./3. }; c@335: testWindow<3>(BartlettWindow, e3); c@335: c@335: double e4[] = { 0, 1./2., 1., 1./2. }; c@335: testWindow<4>(BartlettWindow, e4); c@335: c@335: double e5[] = { 0, 1./2., 1., 1., 1./2. }; c@335: testWindow<5>(BartlettWindow, e5); c@335: c@335: double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. }; c@335: testWindow<6>(BartlettWindow, e6); c@335: } c@335: c@335: BOOST_AUTO_TEST_CASE(hamming) c@335: { c@335: double e1[] = { 1 }; c@335: testWindow<1>(HammingWindow, e1); c@335: c@335: double e10[] = { c@335: 0.0800, 0.1679, 0.3979, 0.6821, 0.9121, c@335: 1.0000, 0.9121, 0.6821, 0.3979, 0.1679 c@335: }; c@335: testWindow<10>(HammingWindow, e10); c@335: } c@335: c@335: BOOST_AUTO_TEST_CASE(hann) c@335: { c@335: double e1[] = { 1 }; c@335: testWindow<1>(HanningWindow, e1); c@335: c@335: double e10[] = { c@335: 0, 0.0955, 0.3455, 0.6545, 0.9045, c@335: 1.0000, 0.9045, 0.6545, 0.3455, 0.0955, c@335: }; c@335: testWindow<10>(HanningWindow, e10); c@335: } c@335: c@335: BOOST_AUTO_TEST_CASE(blackman) c@335: { c@335: double e1[] = { 1 }; c@335: testWindow<1>(BlackmanWindow, e1); c@335: c@335: double e10[] = { c@335: 0, 0.0402, 0.2008, 0.5098, 0.8492, c@335: 1.0000, 0.8492, 0.5098, 0.2008, 0.0402, c@335: }; c@335: testWindow<10>(BlackmanWindow, e10); c@335: } c@335: c@335: BOOST_AUTO_TEST_SUITE_END() c@335: