USRP Hardware Driver and USRP Manual Version: 4.8.0.0-0-unknown
UHD and USRP Manual
 
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1//
2// Copyright 2014-2015 Ettus Research LLC
3// Copyright 2018 Ettus Research, a National Instruments Company
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7
8#pragma once
9
10#include <uhd/config.hpp>
11#include <boost/numeric/conversion/bounds.hpp>
12#include <boost/version.hpp>
13#include <cmath>
14#if BOOST_VERSION >= 106700
15# include <boost/integer/common_factor.hpp>
16// "bmint" for "boost math integer"
17namespace _bmint = boost::integer;
18#else
19# include <boost/math/common_factor.hpp>
20namespace _bmint = boost::math;
21#endif
22
23
24namespace uhd {
25
30namespace math {
31
32static const double PI = 3.14159265358979323846;
33
50static const float SINGLE_PRECISION_EPSILON = 1.19e-7f;
51static const double DOUBLE_PRECISION_EPSILON = 2.22e-16;
52
54//
55// Note that this is a UHD/USRP-specific constant. In UHD, frequencies are on
56// the order of 1e9 (GHz) or below. We will declare frequencies as equal if they
57// are within a millihertz. For the purpose of
58// comparing frequencies, we "lose" 9 decimal places for the integer
59// component of the frequency, so we choose this epsilon value for floating
60// point comparison of frequencies.
61static constexpr double FREQ_COMPARE_EPSILON = 1e-12;
62
63namespace fp_compare {
64
78template <typename float_t>
80{
81public:
83 UHD_INLINE fp_compare_epsilon(float_t value, float_t epsilon);
86 UHD_INLINE void operator=(const fp_compare_epsilon& copy);
87
88 float_t _value;
89 float_t _epsilon;
90};
91
92/* A Note on Floating Point Equality with Epsilons
93 *
94 * There are obviously a lot of strategies for defining floating point
95 * equality, and in the end it all comes down to the domain at hand. UHD's
96 * floating-point-with-epsilon comparison algorithm is based on the method
97 * presented in Knuth's "The Art of Computer Science" called "close enough
98 * with tolerance epsilon".
99 *
100 * [(|u - v| / |u|) <= e] || [(|u - v| / |v|) <= e]
101 *
102 * UHD's modification to this algorithm is using the denominator's epsilon
103 * value (since each float_t object has its own epsilon) for each
104 * comparison.
105 */
106
107template <typename float_t>
110template <typename float_t>
113template <typename float_t>
116template <typename float_t>
119template <typename float_t>
122template <typename float_t>
125
126/* If these operators are used with floats, we rely on type promotion to
127 * double. */
128template <typename float_t>
130template <typename float_t>
132template <typename float_t>
134template <typename float_t>
136template <typename float_t>
138template <typename float_t>
140
141template <typename float_t>
143template <typename float_t>
145template <typename float_t>
147template <typename float_t>
149template <typename float_t>
151template <typename float_t>
153
156{
157public:
159 : fp_compare_epsilon<double>(value, FREQ_COMPARE_EPSILON)
160 {
161 }
162
164 : fp_compare_epsilon<double>(copy)
165 {
166 }
167};
168
169
170} // namespace fp_compare
171
172
179static const float SINGLE_PRECISION_DELTA = 1e-3f;
180static const double DOUBLE_PRECISION_DELTA = 1e-5;
181
183static const double FREQ_COMPARISON_DELTA_HZ = 0.1;
184
185
186namespace fp_compare {
187
201template <typename float_t>
203{
204public:
206 UHD_INLINE fp_compare_delta(float_t value, float_t delta);
209 UHD_INLINE void operator=(const fp_compare_delta& copy);
210
211 float_t _value;
212 float_t _delta;
213};
214
215template <typename float_t>
217template <typename float_t>
219template <typename float_t>
221template <typename float_t>
223template <typename float_t>
225template <typename float_t>
227
228/* If these operators are used with floats, we rely on type promotion to
229 * double. */
230template <typename float_t>
232template <typename float_t>
234template <typename float_t>
236template <typename float_t>
238template <typename float_t>
240template <typename float_t>
242
243template <typename float_t>
245template <typename float_t>
247template <typename float_t>
249template <typename float_t>
251template <typename float_t>
253template <typename float_t>
255
256} // namespace fp_compare
257
258UHD_INLINE bool frequencies_are_equal(double lhs, double rhs)
259{
260 return (fp_compare::fp_compare_delta<double>(lhs, FREQ_COMPARISON_DELTA_HZ)
261 == fp_compare::fp_compare_delta<double>(rhs, FREQ_COMPARISON_DELTA_HZ));
262}
263
264inline double dB_to_lin(const double dB_val)
265{
266 return std::pow(10, (dB_val) / 10.0);
267}
268
269inline double lin_to_dB(const double val)
270{
271 return 10 * std::log10(val);
272}
273
274
276template <typename IntegerType>
277inline IntegerType lcm(IntegerType x, IntegerType y)
278{
279 // Note: _bmint is defined conditionally at the top of the file
280 return _bmint::lcm<IntegerType>(x, y);
281}
282
284template <typename IntegerType>
285inline IntegerType gcd(IntegerType x, IntegerType y)
286{
287 // Note: _bmint is defined conditionally at the top of the file
288 return _bmint::gcd<IntegerType>(x, y);
289}
290
292//
293// Note: This is equivalent to the Boost.Math version, but without the
294// dependency.
295//
296// Returns +1 for positive arguments, -1 for negative arguments, and 0 if x is
297// zero.
298template <typename T>
299inline constexpr int sign(T x)
300{
301 // Note: If T is unsigned, then this will compile with a warning. Should
302 // we need that, expand the template logic.
303 return (T(0) < x) - (x < T(0));
304}
305
307// Nyquist zone.
308//
309// Examples:
310// - Just above the sampling rate:
311// wrap_frequency(250e6, 200e6) == 50e6
312// - Just outside the Nyquist zone:
313// wrap_frequency(120e6, 200e6) == -80e6
314// - Also works for negative frequencies:
315// wrap_frequency(-250e6, 200e6) == -50e6
316inline double wrap_frequency(const double requested_freq, const double rate)
317{
318 double freq = std::fmod(requested_freq, rate);
319 if (std::abs(freq) > rate / 2.0)
320 freq -= uhd::math::sign(freq) * rate;
321 return freq;
322}
323
324} // namespace math
325} // namespace uhd
326
UHD_INLINE fp_compare_delta(float_t value)
float_t _delta
Definition math.hpp:212
UHD_INLINE void operator=(const fp_compare_delta &copy)
Definition fp_compare_delta.ipp:56
UHD_INLINE ~fp_compare_delta()
Definition fp_compare_delta.ipp:51
float_t _value
Definition math.hpp:211
float_t _epsilon
Definition math.hpp:89
UHD_INLINE ~fp_compare_epsilon()
Definition fp_compare_epsilon.ipp:46
UHD_INLINE void operator=(const fp_compare_epsilon &copy)
Definition fp_compare_epsilon.ipp:51
float_t _value
Definition math.hpp:88
UHD_INLINE fp_compare_epsilon(float_t value)
An alias for fp_compare_epsilon, but with defaults for frequencies.
Definition math.hpp:156
UHD_INLINE freq_compare_epsilon(const freq_compare_epsilon &copy)
Definition math.hpp:163
UHD_INLINE freq_compare_epsilon(double value)
Definition math.hpp:158
#define UHD_INLINE
Definition config.h:65
UHD_INLINE bool operator!=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:71
UHD_INLINE bool operator<=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:84
UHD_INLINE bool operator==(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:64
UHD_INLINE bool operator<(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:77
UHD_INLINE bool operator>(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:90
UHD_INLINE bool operator>=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:97
IntegerType lcm(IntegerType x, IntegerType y)
Portable version of lcm() across Boost versions.
Definition math.hpp:277
UHD_INLINE bool frequencies_are_equal(double lhs, double rhs)
Definition math.hpp:258
double dB_to_lin(const double dB_val)
Definition math.hpp:264
constexpr int sign(T x)
Returns the sign of x.
Definition math.hpp:299
IntegerType gcd(IntegerType x, IntegerType y)
Portable version of gcd() across Boost versions.
Definition math.hpp:285
double lin_to_dB(const double val)
Definition math.hpp:269
double wrap_frequency(const double requested_freq, const double rate)
Return a wrapped frequency that is the equivalent frequency in the first.
Definition math.hpp:316
Definition build_info.hpp:12