c@131: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@131: c@131: #include "dsp/Window.h" c@131: #include "dsp/KaiserWindow.h" c@131: #include "dsp/SincWindow.h" c@131: c@131: #include c@131: c@131: #define BOOST_TEST_DYN_LINK c@131: #define BOOST_TEST_MAIN c@131: c@131: #include c@131: c@131: BOOST_AUTO_TEST_SUITE(TestWindow) c@131: c@131: using std::cout; c@131: using std::endl; c@131: c@131: #define COMPARE_ARRAY(a, b) \ c@131: for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \ c@131: BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-4); \ c@131: } c@131: c@131: void c@131: testSymmetric(double *d, int n) c@131: { c@131: for (int i = 0; i <= n/2; ++i) { c@131: BOOST_CHECK_CLOSE(d[i], d[n-i-1], 1e-10); c@131: } c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(periodic) c@131: { c@131: // We can't actually test whether a function is periodic, given c@131: // only one cycle of it! But we can make sure that all but the c@131: // first sample is symmetric, which is what a symmetric window c@131: // becomes when generated in periodic mode c@131: double d[9]; c@131: for (int n = 8; n <= 9; ++n) { c@131: for (int wt = (int)FirstWindow; wt <= (int)LastWindow; ++wt) { c@131: for (int i = 0; i < n; ++i) d[i] = 1.0; c@131: Window w((WindowType)wt, n); c@131: w.cut(d); c@131: testSymmetric(d + 1, n - 1); c@131: } c@131: } c@131: } c@131: c@131: template c@131: void testWindow(WindowType type, const double expected[N]) c@131: { c@131: double d[N]; c@131: for (int i = 0; i < N; ++i) d[i] = 1.0; c@131: Window w(type, N); c@131: w.cut(d); c@131: COMPARE_ARRAY(d, expected); c@131: c@131: double d0[N], d1[N]; c@131: for (int i = 0; i < N; ++i) d0[i] = 0.5 + (1.0 / (N * 2)) * (i + 1); c@131: w.cut(d0, d1); c@131: for (int i = 0; i < N; ++i) { c@131: BOOST_CHECK_SMALL(d1[i] - d0[i] * expected[i], 1e-4); c@131: } c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(bartlett) c@131: { c@131: double e1[] = { 1 }; c@131: testWindow<1>(BartlettWindow, e1); c@131: c@131: double e2[] = { 0, 0 }; c@131: testWindow<2>(BartlettWindow, e2); c@131: c@131: double e3[] = { 0, 2./3., 2./3. }; c@131: testWindow<3>(BartlettWindow, e3); c@131: c@131: double e4[] = { 0, 1./2., 1., 1./2. }; c@131: testWindow<4>(BartlettWindow, e4); c@131: c@131: double e5[] = { 0, 1./2., 1., 1., 1./2. }; c@131: testWindow<5>(BartlettWindow, e5); c@131: c@131: double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. }; c@131: testWindow<6>(BartlettWindow, e6); c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(hamming) c@131: { c@131: double e1[] = { 1 }; c@131: testWindow<1>(HammingWindow, e1); c@131: c@131: double e10[] = { c@131: 0.0800, 0.1679, 0.3979, 0.6821, 0.9121, c@131: 1.0000, 0.9121, 0.6821, 0.3979, 0.1679 c@131: }; c@131: testWindow<10>(HammingWindow, e10); c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(hann) c@131: { c@131: double e1[] = { 1 }; c@131: testWindow<1>(HanningWindow, e1); c@131: c@131: double e10[] = { c@131: 0, 0.0955, 0.3455, 0.6545, 0.9045, c@131: 1.0000, 0.9045, 0.6545, 0.3455, 0.0955, c@131: }; c@131: testWindow<10>(HanningWindow, e10); c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(blackman) c@131: { c@131: double e1[] = { 1 }; c@131: testWindow<1>(BlackmanWindow, e1); c@131: c@131: double e10[] = { c@131: 0, 0.0402, 0.2008, 0.5098, 0.8492, c@131: 1.0000, 0.8492, 0.5098, 0.2008, 0.0402, c@131: }; c@131: testWindow<10>(BlackmanWindow, e10); c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(blackmanHarris) c@131: { c@131: double e1[] = { 1 }; c@131: testWindow<1>(BlackmanHarrisWindow, e1); c@131: c@131: double e10[] = { c@131: 0.0001, 0.0110, 0.1030, 0.3859, 0.7938, c@131: 1.0000, 0.7938, 0.3859, 0.1030, 0.0110, c@131: }; c@131: testWindow<10>(BlackmanHarrisWindow, e10); c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(kaiser_4_10) c@131: { c@131: double d[10]; c@131: for (int i = 0; i < 10; ++i) d[i] = 1.0; c@131: double e[] = { c@131: 0.0885, 0.2943, 0.5644, 0.8216, 0.9789, c@131: 0.9789, 0.8216, 0.5644, 0.2943, 0.0885 c@131: }; c@131: KaiserWindow::Parameters p; c@131: p.length = 10; c@131: p.beta = 4; c@131: KaiserWindow k(p); c@131: k.cut(d); c@131: COMPARE_ARRAY(d, e); c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(kaiser_2p5_11) c@131: { c@131: double d[11]; c@131: for (int i = 0; i < 11; ++i) d[i] = 1.0; c@131: double e[] = { c@131: 0.3040, 0.5005, 0.6929, 0.8546, 0.9622, c@131: 1.0000, 0.9622, 0.8546, 0.6929, 0.5005, 0.3040 c@131: }; c@131: KaiserWindow::Parameters p; c@131: p.length = 11; c@131: p.beta = 2.5; c@131: KaiserWindow k(p); c@131: k.cut(d); c@131: COMPARE_ARRAY(d, e); c@131: } c@131: c@131: //!!! todo: tests for kaiser with attenuation and bandwidth parameters c@131: c@131: template c@131: void testSinc(double p, const double expected[N]) c@131: { c@131: double d[N]; c@131: for (int i = 0; i < N; ++i) d[i] = 1.0; c@131: SincWindow w(N, p); c@131: w.cut(d); c@131: COMPARE_ARRAY(d, expected); c@131: c@131: double d0[N], d1[N]; c@131: for (int i = 0; i < N; ++i) d0[i] = 0.5 + (1.0 / (N * 2)) * (i + 1); c@131: w.cut(d0, d1); c@131: for (int i = 0; i < N; ++i) { c@131: BOOST_CHECK_SMALL(d1[i] - d0[i] * expected[i], 1e-4); c@131: } c@131: } c@131: c@131: BOOST_AUTO_TEST_CASE(sinc) c@131: { c@131: double e1[] = { 0, 0, 1, 0, 0 }; c@131: testSinc<5>(1, e1); c@131: c@131: double e2[] = { 0, 0, 1, 0, 0 }; c@131: testSinc<5>(2, e2); c@131: c@131: double e3[] = { -0.2122, 0.0, 0.6366, 1.0, 0.6366, 0, -0.2122 }; c@131: testSinc<7>(4, e3); c@131: c@131: double e4[] = { -0.2122, 0, 0.6366, 1, 0.6366, 0 }; c@131: testSinc<6>(4, e4); c@131: } c@131: c@131: BOOST_AUTO_TEST_SUITE_END() c@131: