From d9f4d540ef334013eb404ce91b3b446e5fc917ff Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Tue, 31 Mar 2020 21:38:13 -0700 Subject: uhd: math: Add interpolation.hpp - Moves linear_interp from cal to utils - Moves the interp_mode enum class to interpolation.hpp - Adds three interpolation methods for maps: at_interpolate_1d(), at_nearest(), at_lin_interp() - Adds unit tests --- host/tests/interpolation_test.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 host/tests/interpolation_test.cpp (limited to 'host/tests/interpolation_test.cpp') diff --git a/host/tests/interpolation_test.cpp b/host/tests/interpolation_test.cpp new file mode 100644 index 000000000..b3314c34a --- /dev/null +++ b/host/tests/interpolation_test.cpp @@ -0,0 +1,35 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include +#include + +BOOST_AUTO_TEST_CASE(test_interp) +{ + const double x0 = 1.0, x1 = 2.0; + const double y0 = 2.0, y1 = 4.0; + const double x = 1.5; + const double y_exp = 3.0; + + BOOST_CHECK_EQUAL(uhd::math::linear_interp(x, x0, y0, x1, y1), y_exp); +} + +BOOST_AUTO_TEST_CASE(test_map_interp) +{ + using namespace uhd::math; + std::map test_data{{1.0, 1.0}, {2.0, 2.0}, {3.0, 3.0}}; + + BOOST_CHECK_EQUAL(at_nearest(test_data, 1.1), 1.0); + BOOST_CHECK_EQUAL(at_nearest(test_data, 1.9), 2.0); + BOOST_CHECK_EQUAL(at_nearest(test_data, 2.1), 2.0); + BOOST_CHECK_EQUAL(at_nearest(test_data, 1e9), 3.0); + + BOOST_CHECK_CLOSE(at_lin_interp(test_data, 1.5), 1.5, 1e-6); + BOOST_CHECK_EQUAL(at_lin_interp(test_data, 0.1), 1.0); + BOOST_CHECK_EQUAL(at_lin_interp(test_data, 2.0), 2.0); + BOOST_CHECK_EQUAL(at_lin_interp(test_data, 137.0), 3.0); +} + -- cgit v1.2.3