annotate test/TestWindow.cpp @ 196:da283326bcd3 tip master

Update plugin versions in RDF
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 28 Feb 2020 09:43:02 +0000
parents 6b13f9c694a8
children
rev   line source
c@131 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@131 2
c@131 3 #include "dsp/Window.h"
c@131 4 #include "dsp/KaiserWindow.h"
c@131 5 #include "dsp/SincWindow.h"
c@131 6
c@131 7 #include <iostream>
c@131 8
c@131 9 #define BOOST_TEST_DYN_LINK
c@131 10 #define BOOST_TEST_MAIN
c@131 11
c@131 12 #include <boost/test/unit_test.hpp>
c@131 13
c@131 14 BOOST_AUTO_TEST_SUITE(TestWindow)
c@131 15
c@131 16 using std::cout;
c@131 17 using std::endl;
c@131 18
c@131 19 #define COMPARE_ARRAY(a, b) \
c@131 20 for (int cmp_i = 0; cmp_i < (int)(sizeof(a)/sizeof(a[0])); ++cmp_i) { \
c@131 21 BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], 1e-4); \
c@131 22 }
c@131 23
c@131 24 void
c@131 25 testSymmetric(double *d, int n)
c@131 26 {
c@131 27 for (int i = 0; i <= n/2; ++i) {
c@131 28 BOOST_CHECK_CLOSE(d[i], d[n-i-1], 1e-10);
c@131 29 }
c@131 30 }
c@131 31
c@131 32 BOOST_AUTO_TEST_CASE(periodic)
c@131 33 {
c@131 34 // We can't actually test whether a function is periodic, given
c@131 35 // only one cycle of it! But we can make sure that all but the
c@131 36 // first sample is symmetric, which is what a symmetric window
c@131 37 // becomes when generated in periodic mode
c@131 38 double d[9];
c@131 39 for (int n = 8; n <= 9; ++n) {
c@131 40 for (int wt = (int)FirstWindow; wt <= (int)LastWindow; ++wt) {
c@131 41 for (int i = 0; i < n; ++i) d[i] = 1.0;
c@131 42 Window<double> w((WindowType)wt, n);
c@131 43 w.cut(d);
c@131 44 testSymmetric(d + 1, n - 1);
c@131 45 }
c@131 46 }
c@131 47 }
c@131 48
c@131 49 template <int N>
c@131 50 void testWindow(WindowType type, const double expected[N])
c@131 51 {
c@131 52 double d[N];
c@131 53 for (int i = 0; i < N; ++i) d[i] = 1.0;
c@131 54 Window<double> w(type, N);
c@131 55 w.cut(d);
c@131 56 COMPARE_ARRAY(d, expected);
c@131 57
c@131 58 double d0[N], d1[N];
c@131 59 for (int i = 0; i < N; ++i) d0[i] = 0.5 + (1.0 / (N * 2)) * (i + 1);
c@131 60 w.cut(d0, d1);
c@131 61 for (int i = 0; i < N; ++i) {
c@131 62 BOOST_CHECK_SMALL(d1[i] - d0[i] * expected[i], 1e-4);
c@131 63 }
c@131 64 }
c@131 65
c@131 66 BOOST_AUTO_TEST_CASE(bartlett)
c@131 67 {
c@131 68 double e1[] = { 1 };
c@131 69 testWindow<1>(BartlettWindow, e1);
c@131 70
c@131 71 double e2[] = { 0, 0 };
c@131 72 testWindow<2>(BartlettWindow, e2);
c@131 73
c@131 74 double e3[] = { 0, 2./3., 2./3. };
c@131 75 testWindow<3>(BartlettWindow, e3);
c@131 76
c@131 77 double e4[] = { 0, 1./2., 1., 1./2. };
c@131 78 testWindow<4>(BartlettWindow, e4);
c@131 79
c@131 80 double e5[] = { 0, 1./2., 1., 1., 1./2. };
c@131 81 testWindow<5>(BartlettWindow, e5);
c@131 82
c@131 83 double e6[] = { 0, 1./3., 2./3., 1., 2./3., 1./3. };
c@131 84 testWindow<6>(BartlettWindow, e6);
c@131 85 }
c@131 86
c@131 87 BOOST_AUTO_TEST_CASE(hamming)
c@131 88 {
c@131 89 double e1[] = { 1 };
c@131 90 testWindow<1>(HammingWindow, e1);
c@131 91
c@131 92 double e10[] = {
c@131 93 0.0800, 0.1679, 0.3979, 0.6821, 0.9121,
c@131 94 1.0000, 0.9121, 0.6821, 0.3979, 0.1679
c@131 95 };
c@131 96 testWindow<10>(HammingWindow, e10);
c@131 97 }
c@131 98
c@131 99 BOOST_AUTO_TEST_CASE(hann)
c@131 100 {
c@131 101 double e1[] = { 1 };
c@131 102 testWindow<1>(HanningWindow, e1);
c@131 103
c@131 104 double e10[] = {
c@131 105 0, 0.0955, 0.3455, 0.6545, 0.9045,
c@131 106 1.0000, 0.9045, 0.6545, 0.3455, 0.0955,
c@131 107 };
c@131 108 testWindow<10>(HanningWindow, e10);
c@131 109 }
c@131 110
c@131 111 BOOST_AUTO_TEST_CASE(blackman)
c@131 112 {
c@131 113 double e1[] = { 1 };
c@131 114 testWindow<1>(BlackmanWindow, e1);
c@131 115
c@131 116 double e10[] = {
c@131 117 0, 0.0402, 0.2008, 0.5098, 0.8492,
c@131 118 1.0000, 0.8492, 0.5098, 0.2008, 0.0402,
c@131 119 };
c@131 120 testWindow<10>(BlackmanWindow, e10);
c@131 121 }
c@131 122
c@131 123 BOOST_AUTO_TEST_CASE(blackmanHarris)
c@131 124 {
c@131 125 double e1[] = { 1 };
c@131 126 testWindow<1>(BlackmanHarrisWindow, e1);
c@131 127
c@131 128 double e10[] = {
c@131 129 0.0001, 0.0110, 0.1030, 0.3859, 0.7938,
c@131 130 1.0000, 0.7938, 0.3859, 0.1030, 0.0110,
c@131 131 };
c@131 132 testWindow<10>(BlackmanHarrisWindow, e10);
c@131 133 }
c@131 134
c@131 135 BOOST_AUTO_TEST_CASE(kaiser_4_10)
c@131 136 {
c@131 137 double d[10];
c@131 138 for (int i = 0; i < 10; ++i) d[i] = 1.0;
c@131 139 double e[] = {
c@131 140 0.0885, 0.2943, 0.5644, 0.8216, 0.9789,
c@131 141 0.9789, 0.8216, 0.5644, 0.2943, 0.0885
c@131 142 };
c@131 143 KaiserWindow::Parameters p;
c@131 144 p.length = 10;
c@131 145 p.beta = 4;
c@131 146 KaiserWindow k(p);
c@131 147 k.cut(d);
c@131 148 COMPARE_ARRAY(d, e);
c@131 149 }
c@131 150
c@131 151 BOOST_AUTO_TEST_CASE(kaiser_2p5_11)
c@131 152 {
c@131 153 double d[11];
c@131 154 for (int i = 0; i < 11; ++i) d[i] = 1.0;
c@131 155 double e[] = {
c@131 156 0.3040, 0.5005, 0.6929, 0.8546, 0.9622,
c@131 157 1.0000, 0.9622, 0.8546, 0.6929, 0.5005, 0.3040
c@131 158 };
c@131 159 KaiserWindow::Parameters p;
c@131 160 p.length = 11;
c@131 161 p.beta = 2.5;
c@131 162 KaiserWindow k(p);
c@131 163 k.cut(d);
c@131 164 COMPARE_ARRAY(d, e);
c@131 165 }
c@131 166
c@131 167 //!!! todo: tests for kaiser with attenuation and bandwidth parameters
c@131 168
c@131 169 template <int N>
c@131 170 void testSinc(double p, const double expected[N])
c@131 171 {
c@131 172 double d[N];
c@131 173 for (int i = 0; i < N; ++i) d[i] = 1.0;
c@131 174 SincWindow w(N, p);
c@131 175 w.cut(d);
c@131 176 COMPARE_ARRAY(d, expected);
c@131 177
c@131 178 double d0[N], d1[N];
c@131 179 for (int i = 0; i < N; ++i) d0[i] = 0.5 + (1.0 / (N * 2)) * (i + 1);
c@131 180 w.cut(d0, d1);
c@131 181 for (int i = 0; i < N; ++i) {
c@131 182 BOOST_CHECK_SMALL(d1[i] - d0[i] * expected[i], 1e-4);
c@131 183 }
c@131 184 }
c@131 185
c@131 186 BOOST_AUTO_TEST_CASE(sinc)
c@131 187 {
c@131 188 double e1[] = { 0, 0, 1, 0, 0 };
c@131 189 testSinc<5>(1, e1);
c@131 190
c@131 191 double e2[] = { 0, 0, 1, 0, 0 };
c@131 192 testSinc<5>(2, e2);
c@131 193
c@131 194 double e3[] = { -0.2122, 0.0, 0.6366, 1.0, 0.6366, 0, -0.2122 };
c@131 195 testSinc<7>(4, e3);
c@131 196
c@131 197 double e4[] = { -0.2122, 0, 0.6366, 1, 0.6366, 0 };
c@131 198 testSinc<6>(4, e4);
c@131 199 }
c@131 200
c@131 201 BOOST_AUTO_TEST_SUITE_END()
c@131 202