From 5ee6b828debbd60e03aff805bfd80e2030715a6f Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Tue, 31 Aug 2021 11:49:51 +0200 Subject: uhd: math: Replace wrap-frequency math with a single function In multiple places in the UHD code, we were doing the same calculation for a wrapped frequency (wrap it into the first Nyquist zone). This math was using boost::math, too. Instead of editing every instance, we create a new function, uhd::math::wrap_frequency(), and replace all of its separate implementations with this function. The new function also no longer relies on boost::math::sign. --- host/include/uhd/utils/math.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'host/include') diff --git a/host/include/uhd/utils/math.hpp b/host/include/uhd/utils/math.hpp index 4918ddf2e..6c8fceae9 100644 --- a/host/include/uhd/utils/math.hpp +++ b/host/include/uhd/utils/math.hpp @@ -278,6 +278,24 @@ inline constexpr int sign(T x) return (T(0) < x) - (x < T(0)); } +//! Return a wrapped frequency that is the equivalent frequency in the first +// Nyquist zone. +// +// Examples: +// - Just above the sampling rate: +// wrap_frequency(250e6, 200e6) == 50e6 +// - Just outside the Nyquist zone: +// wrap_frequency(120e6, 200e6) == -80e6 +// - Also works for negative frequencies: +// wrap_frequency(-250e6, 200e6) == -50e6 +inline double wrap_frequency(const double requested_freq, const double rate) +{ + double freq = std::fmod(requested_freq, rate); + if (std::abs(freq) > rate / 2.0) + freq -= uhd::math::sign(freq) * rate; + return freq; +} + } // namespace math } // namespace uhd -- cgit v1.2.3