comparison tests/TestWindow.cpp @ 335:0a632ac70945

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