Chris@372: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@372: Chris@372: #include "bqvec/VectorOpsComplex.h" Chris@372: #include "bqvec/VectorOps.h" Chris@372: Chris@372: #define BOOST_TEST_DYN_LINK Chris@372: #define BOOST_TEST_MAIN Chris@372: Chris@372: #include Chris@372: Chris@372: #include Chris@372: #include Chris@372: #include Chris@372: Chris@372: using namespace breakfastquay; Chris@372: Chris@372: using namespace std; Chris@372: Chris@372: BOOST_AUTO_TEST_SUITE(TestVectorOpsComplex) Chris@372: Chris@372: #ifdef USE_APPROXIMATE_ATAN2 Chris@372: static const double eps = 5.0e-3; Chris@372: #else Chris@372: #ifdef USE_SINGLE_PRECISION_COMPLEX Chris@372: static const double eps = 1.0e-7; Chris@372: #else Chris@372: static const double eps = 1.0e-14; Chris@372: #endif Chris@372: #endif Chris@372: Chris@372: #define COMPARE_N(a, b, n) \ Chris@372: for (int cmp_i = 0; cmp_i < n; ++cmp_i) { \ Chris@372: BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], eps); \ Chris@372: } Chris@372: Chris@372: #define COMPARE_NC(a, b, n) \ Chris@372: for (int cmp_i = 0; cmp_i < n; ++cmp_i) { \ Chris@372: BOOST_CHECK_SMALL(a[cmp_i] - b[cmp_i], (bq_complex_element_t) eps); \ Chris@372: } Chris@372: Chris@372: #define COMPARE_CPLX_N(a, b, n) \ Chris@372: for (int cmp_i = 0; cmp_i < n; ++cmp_i) { \ Chris@372: BOOST_CHECK_SMALL(a[cmp_i].re - b[cmp_i].re, (bq_complex_element_t) eps); \ Chris@372: BOOST_CHECK_SMALL(a[cmp_i].im - b[cmp_i].im, (bq_complex_element_t) eps); \ Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(add) Chris@372: { Chris@372: bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } }; Chris@372: bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } }; Chris@372: bq_complex_t expected[] = { { 0.0, 5.0 }, { -1.5, -4.0 } }; Chris@372: v_add(a, b, 2); Chris@372: COMPARE_CPLX_N(a, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(add_with_gain) Chris@372: { Chris@372: bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } }; Chris@372: bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } }; Chris@372: bq_complex_t expected[] = { { -0.5, 6.5 }, { -3.75, -4.0 } }; Chris@372: v_add_with_gain(a, b, (bq_complex_element_t) 1.5, 2); Chris@372: COMPARE_CPLX_N(a, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(multiply) Chris@372: { Chris@372: bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } }; Chris@372: bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } }; Chris@372: bq_complex_t expected[] = { { -7.0, 1.0 }, { -13.5, 18.0 } }; Chris@372: v_multiply(a, b, 2); Chris@372: COMPARE_CPLX_N(a, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(multiply_to) Chris@372: { Chris@372: bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } }; Chris@372: bq_complex_t b[] = { { -1.0, 3.0 }, { -4.5, 0.0 } }; Chris@372: bq_complex_t o[2]; Chris@372: bq_complex_t expected[] = { { -7.0, 1.0 }, { -13.5, 18.0 } }; Chris@372: v_multiply_to(o, a, b, 2); Chris@372: COMPARE_CPLX_N(o, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_to_magnitudes_bq) Chris@372: { Chris@372: bq_complex_t a[] = { { 1.0, 2.0 }, { 3.0, -4.0 } }; Chris@372: bq_complex_element_t o[2]; Chris@372: bq_complex_element_t expected[] = { sqrt(5.0), 5.0 }; Chris@372: v_cartesian_to_magnitudes(o, a, 2); Chris@372: COMPARE_NC(o, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_to_magnitudes) Chris@372: { Chris@372: double re[] = { 1.0, 3.0 }; Chris@372: double im[] = { 2.0, -4.0 }; Chris@372: double o[2]; Chris@372: double expected[] = { sqrt(5.0), 5.0 }; Chris@372: v_cartesian_to_magnitudes(o, re, im, 2); Chris@372: COMPARE_N(o, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_interleaved_to_magnitudes) Chris@372: { Chris@372: double a[] = { 1.0, 2.0, 3.0, -4.0 }; Chris@372: double o[2]; Chris@372: double expected[] = { sqrt(5.0), 5.0 }; Chris@372: v_cartesian_interleaved_to_magnitudes(o, a, 2); Chris@372: COMPARE_N(o, expected, 2); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_to_polar_bq) Chris@372: { Chris@372: bq_complex_t a[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } }; Chris@372: bq_complex_element_t mo[3], po[3]; Chris@372: bq_complex_element_t me[] = { 0.0, sqrt(2.0), 1.0 }; Chris@372: bq_complex_element_t pe[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 }; Chris@372: v_cartesian_to_polar(mo, po, a, 3); Chris@372: COMPARE_NC(mo, me, 3); Chris@372: COMPARE_NC(po, pe, 3); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_to_polar_interleaved_bq) Chris@372: { Chris@372: bq_complex_t a[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } }; Chris@372: bq_complex_element_t o[6]; Chris@372: bq_complex_element_t e[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 }; Chris@372: v_cartesian_to_polar_interleaved(o, a, 3); Chris@372: COMPARE_NC(o, e, 6); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_to_polar) Chris@372: { Chris@372: double re[] = { 0.0, 1.0, 0.0 }; Chris@372: double im[] = { 0.0, 1.0, -1.0 }; Chris@372: double mo[3], po[3]; Chris@372: double me[] = { 0.0, sqrt(2.0), 1.0 }; Chris@372: double pe[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 }; Chris@372: v_cartesian_to_polar(mo, po, re, im, 3); Chris@372: COMPARE_N(mo, me, 3); Chris@372: COMPARE_N(po, pe, 3); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_to_polar_interleaved_inplace) Chris@372: { Chris@372: double a[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 }; Chris@372: double e[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 }; Chris@372: v_cartesian_to_polar_interleaved_inplace(a, 3); Chris@372: COMPARE_N(a, e, 6); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(cartesian_interleaved_to_polar) Chris@372: { Chris@372: double a[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 }; Chris@372: double mo[3], po[3]; Chris@372: double me[] = { 0.0, sqrt(2.0), 1.0 }; Chris@372: double pe[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 }; Chris@372: v_cartesian_interleaved_to_polar(mo, po, a, 3); Chris@372: COMPARE_N(mo, me, 3); Chris@372: COMPARE_N(po, pe, 3); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(polar_to_cartesian_bq) Chris@372: { Chris@372: bq_complex_element_t m[] = { 0.0, sqrt(2.0), 1.0 }; Chris@372: bq_complex_element_t p[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 }; Chris@372: bq_complex_t o[3]; Chris@372: bq_complex_t e[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } }; Chris@372: v_polar_to_cartesian(o, m, p, 3); Chris@372: COMPARE_CPLX_N(o, e, 3); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(polar_to_cartesian_interleaved_bq) Chris@372: { Chris@372: bq_complex_t a[] = { { 0.0, 0.0 }, { 1.0, 1.0 }, { 0.0, -1.0 } }; Chris@372: bq_complex_element_t o[6]; Chris@372: bq_complex_element_t e[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 }; Chris@372: v_cartesian_to_polar_interleaved(o, a, 3); Chris@372: COMPARE_NC(o, e, 6); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(polar_to_cartesian) Chris@372: { Chris@372: double m[] = { 0.0, sqrt(2.0), 1.0 }; Chris@372: double p[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 }; Chris@372: double ro[3], io[3]; Chris@372: double re[] = { 0.0, 1.0, 0.0 }; Chris@372: double ie[] = { 0.0, 1.0, -1.0 }; Chris@372: v_polar_to_cartesian(ro, io, m, p, 3); Chris@372: COMPARE_N(ro, re, 3); Chris@372: COMPARE_N(io, ie, 3); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(polar_to_cartesian_interleaved_inplace) Chris@372: { Chris@372: double a[] = { 0.0, 0.0, sqrt(2.0), M_PI / 4.0, 1.0, -M_PI * 0.5 }; Chris@372: double e[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 }; Chris@372: v_polar_interleaved_to_cartesian_inplace(a, 3); Chris@372: COMPARE_N(a, e, 6); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_CASE(polar_to_cartesian_interleaved) Chris@372: { Chris@372: double m[] = { 0.0, sqrt(2.0), 1.0 }; Chris@372: double p[] = { 0.0, M_PI / 4.0, -M_PI * 0.5 }; Chris@372: double o[6]; Chris@372: double e[] = { 0.0, 0.0, 1.0, 1.0, 0.0, -1.0 }; Chris@372: v_polar_to_cartesian_interleaved(o, m, p, 3); Chris@372: COMPARE_N(o, e, 6); Chris@372: } Chris@372: Chris@372: BOOST_AUTO_TEST_SUITE_END() Chris@372: