annotate bqvec/test/TestVectorOpsComplex.cpp @ 372:af71cbdab621 tip

Update bqvec code
author Chris Cannam
date Tue, 19 Nov 2019 10:13:32 +0000
parents
children
rev   line source
Chris@372 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@372 2
Chris@372 3 #include "bqvec/VectorOpsComplex.h"
Chris@372 4 #include "bqvec/VectorOps.h"
Chris@372 5
Chris@372 6 #define BOOST_TEST_DYN_LINK
Chris@372 7 #define BOOST_TEST_MAIN
Chris@372 8
Chris@372 9 #include <boost/test/unit_test.hpp>
Chris@372 10
Chris@372 11 #include <stdexcept>
Chris@372 12 #include <vector>
Chris@372 13 #include <iostream>
Chris@372 14
Chris@372 15 using namespace breakfastquay;
Chris@372 16
Chris@372 17 using namespace std;
Chris@372 18
Chris@372 19 BOOST_AUTO_TEST_SUITE(TestVectorOpsComplex)
Chris@372 20
Chris@372 21 #ifdef USE_APPROXIMATE_ATAN2
Chris@372 22 static const double eps = 5.0e-3;
Chris@372 23 #else
Chris@372 24 #ifdef USE_SINGLE_PRECISION_COMPLEX
Chris@372 25 static const double eps = 1.0e-7;
Chris@372 26 #else
Chris@372 27 static const double eps = 1.0e-14;
Chris@372 28 #endif
Chris@372 29 #endif
Chris@372 30
Chris@372 31 #define COMPARE_N(a, b, n) \
Chris@372 32 for (int cmp_i = 0; cmp_i < n; ++cmp_i) { \
Chris@372 33 BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], eps); \
Chris@372 34 }
Chris@372 35
Chris@372 36 #define COMPARE_NC(a, b, n) \
Chris@372 37 for (int cmp_i = 0; cmp_i < n; ++cmp_i) { \
Chris@372 38 BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], (bq_complex_element_t) eps); \
Chris@372 39 }
Chris@372 40
Chris@372 41 #define COMPARE_CPLX_N(a, b, n) \
Chris@372 42 for (int cmp_i = 0; cmp_i < n; ++cmp_i) { \
Chris@372 43 BOOST_CHECK_SMALL(a[cmp_i].re - b[cmp_i].re, (bq_complex_element_t) eps); \
Chris@372 44 BOOST_CHECK_SMALL(a[cmp_i].im - b[cmp_i].im, (bq_complex_element_t) eps); \
Chris@372 45 }
Chris@372 46
Chris@372 47 BOOST_AUTO_TEST_CASE(add)
Chris@372 48 {
Chris@372 49 bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } };
Chris@372 50 bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } };
Chris@372 51 bq_complex_t expected[] = { { 0.0, 5.0 }, { -1.5, -4.0 } };
Chris@372 52 v_add(a, b, 2);
Chris@372 53 COMPARE_CPLX_N(a, expected, 2);
Chris@372 54 }
Chris@372 55
Chris@372 56 BOOST_AUTO_TEST_CASE(add_with_gain)
Chris@372 57 {
Chris@372 58 bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } };
Chris@372 59 bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } };
Chris@372 60 bq_complex_t expected[] = { { -0.5, 6.5 }, { -3.75, -4.0 } };
Chris@372 61 v_add_with_gain(a, b, (bq_complex_element_t) 1.5, 2);
Chris@372 62 COMPARE_CPLX_N(a, expected, 2);
Chris@372 63 }
Chris@372 64
Chris@372 65 BOOST_AUTO_TEST_CASE(multiply)
Chris@372 66 {
Chris@372 67 bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } };
Chris@372 68 bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } };
Chris@372 69 bq_complex_t expected[] = { { -7.0, 1.0 }, { -13.5, 18.0 } };
Chris@372 70 v_multiply(a, b, 2);
Chris@372 71 COMPARE_CPLX_N(a, expected, 2);
Chris@372 72 }
Chris@372 73
Chris@372 74 BOOST_AUTO_TEST_CASE(multiply_to)
Chris@372 75 {
Chris@372 76 bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } };
Chris@372 77 bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } };
Chris@372 78 bq_complex_t o[2];
Chris@372 79 bq_complex_t expected[] = { { -7.0, 1.0 }, { -13.5, 18.0 } };
Chris@372 80 v_multiply_to(o, a, b, 2);
Chris@372 81 COMPARE_CPLX_N(o, expected, 2);
Chris@372 82 }
Chris@372 83
Chris@372 84 BOOST_AUTO_TEST_CASE(cartesian_to_magnitudes_bq)
Chris@372 85 {
Chris@372 86 bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } };
Chris@372 87 bq_complex_element_t o[2];
Chris@372 88 bq_complex_element_t expected[] = { sqrt(5.0), 5.0 };
Chris@372 89 v_cartesian_to_magnitudes(o, a, 2);
Chris@372 90 COMPARE_NC(o, expected, 2);
Chris@372 91 }
Chris@372 92
Chris@372 93 BOOST_AUTO_TEST_CASE(cartesian_to_magnitudes)
Chris@372 94 {
Chris@372 95 double re[] = { 1.0, 3.0 };
Chris@372 96 double im[] = { 2.0, -4.0 };
Chris@372 97 double o[2];
Chris@372 98 double expected[] = { sqrt(5.0), 5.0 };
Chris@372 99 v_cartesian_to_magnitudes(o, re, im, 2);
Chris@372 100 COMPARE_N(o, expected, 2);
Chris@372 101 }
Chris@372 102
Chris@372 103 BOOST_AUTO_TEST_CASE(cartesian_interleaved_to_magnitudes)
Chris@372 104 {
Chris@372 105 double a[] = { 1.0, 2.0, 3.0, -4.0 };
Chris@372 106 double o[2];
Chris@372 107 double expected[] = { sqrt(5.0), 5.0 };
Chris@372 108 v_cartesian_interleaved_to_magnitudes(o, a, 2);
Chris@372 109 COMPARE_N(o, expected, 2);
Chris@372 110 }
Chris@372 111
Chris@372 112 BOOST_AUTO_TEST_CASE(cartesian_to_polar_bq)
Chris@372 113 {
Chris@372 114 bq_complex_t a[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } };
Chris@372 115 bq_complex_element_t mo[3], po[3];
Chris@372 116 bq_complex_element_t me[] = { 0.0, sqrt(2.0), 1.0 };
Chris@372 117 bq_complex_element_t pe[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 };
Chris@372 118 v_cartesian_to_polar(mo, po, a, 3);
Chris@372 119 COMPARE_NC(mo, me, 3);
Chris@372 120 COMPARE_NC(po, pe, 3);
Chris@372 121 }
Chris@372 122
Chris@372 123 BOOST_AUTO_TEST_CASE(cartesian_to_polar_interleaved_bq)
Chris@372 124 {
Chris@372 125 bq_complex_t a[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } };
Chris@372 126 bq_complex_element_t o[6];
Chris@372 127 bq_complex_element_t e[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 };
Chris@372 128 v_cartesian_to_polar_interleaved(o, a, 3);
Chris@372 129 COMPARE_NC(o, e, 6);
Chris@372 130 }
Chris@372 131
Chris@372 132 BOOST_AUTO_TEST_CASE(cartesian_to_polar)
Chris@372 133 {
Chris@372 134 double re[] = { 0.0, 1.0, 0.0 };
Chris@372 135 double im[] = { 0.0, 1.0, -1.0 };
Chris@372 136 double mo[3], po[3];
Chris@372 137 double me[] = { 0.0, sqrt(2.0), 1.0 };
Chris@372 138 double pe[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 };
Chris@372 139 v_cartesian_to_polar(mo, po, re, im, 3);
Chris@372 140 COMPARE_N(mo, me, 3);
Chris@372 141 COMPARE_N(po, pe, 3);
Chris@372 142 }
Chris@372 143
Chris@372 144 BOOST_AUTO_TEST_CASE(cartesian_to_polar_interleaved_inplace)
Chris@372 145 {
Chris@372 146 double a[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 };
Chris@372 147 double e[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 };
Chris@372 148 v_cartesian_to_polar_interleaved_inplace(a, 3);
Chris@372 149 COMPARE_N(a, e, 6);
Chris@372 150 }
Chris@372 151
Chris@372 152 BOOST_AUTO_TEST_CASE(cartesian_interleaved_to_polar)
Chris@372 153 {
Chris@372 154 double a[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 };
Chris@372 155 double mo[3], po[3];
Chris@372 156 double me[] = { 0.0, sqrt(2.0), 1.0 };
Chris@372 157 double pe[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 };
Chris@372 158 v_cartesian_interleaved_to_polar(mo, po, a, 3);
Chris@372 159 COMPARE_N(mo, me, 3);
Chris@372 160 COMPARE_N(po, pe, 3);
Chris@372 161 }
Chris@372 162
Chris@372 163 BOOST_AUTO_TEST_CASE(polar_to_cartesian_bq)
Chris@372 164 {
Chris@372 165 bq_complex_element_t m[] = { 0.0, sqrt(2.0), 1.0 };
Chris@372 166 bq_complex_element_t p[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 };
Chris@372 167 bq_complex_t o[3];
Chris@372 168 bq_complex_t e[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } };
Chris@372 169 v_polar_to_cartesian(o, m, p, 3);
Chris@372 170 COMPARE_CPLX_N(o, e, 3);
Chris@372 171 }
Chris@372 172
Chris@372 173 BOOST_AUTO_TEST_CASE(polar_to_cartesian_interleaved_bq)
Chris@372 174 {
Chris@372 175 bq_complex_t a[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } };
Chris@372 176 bq_complex_element_t o[6];
Chris@372 177 bq_complex_element_t e[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 };
Chris@372 178 v_cartesian_to_polar_interleaved(o, a, 3);
Chris@372 179 COMPARE_NC(o, e, 6);
Chris@372 180 }
Chris@372 181
Chris@372 182 BOOST_AUTO_TEST_CASE(polar_to_cartesian)
Chris@372 183 {
Chris@372 184 double m[] = { 0.0, sqrt(2.0), 1.0 };
Chris@372 185 double p[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 };
Chris@372 186 double ro[3], io[3];
Chris@372 187 double re[] = { 0.0, 1.0, 0.0 };
Chris@372 188 double ie[] = { 0.0, 1.0, -1.0 };
Chris@372 189 v_polar_to_cartesian(ro, io, m, p, 3);
Chris@372 190 COMPARE_N(ro, re, 3);
Chris@372 191 COMPARE_N(io, ie, 3);
Chris@372 192 }
Chris@372 193
Chris@372 194 BOOST_AUTO_TEST_CASE(polar_to_cartesian_interleaved_inplace)
Chris@372 195 {
Chris@372 196 double a[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 };
Chris@372 197 double e[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 };
Chris@372 198 v_polar_interleaved_to_cartesian_inplace(a, 3);
Chris@372 199 COMPARE_N(a, e, 6);
Chris@372 200 }
Chris@372 201
Chris@372 202 BOOST_AUTO_TEST_CASE(polar_to_cartesian_interleaved)
Chris@372 203 {
Chris@372 204 double m[] = { 0.0, sqrt(2.0), 1.0 };
Chris@372 205 double p[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 };
Chris@372 206 double o[6];
Chris@372 207 double e[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 };
Chris@372 208 v_polar_to_cartesian_interleaved(o, m, p, 3);
Chris@372 209 COMPARE_N(o, e, 6);
Chris@372 210 }
Chris@372 211
Chris@372 212 BOOST_AUTO_TEST_SUITE_END()
Chris@372 213