annotate tests/TestWindow.cpp @ 515:08bcc06c38ec tip master

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