Audio Processing Framework (APF) version 0.5.0
math.h
Go to the documentation of this file.
1/******************************************************************************
2 Copyright (c) 2012-2016 Institut für Nachrichtentechnik, Universität Rostock
3 Copyright (c) 2006-2012 Quality & Usability Lab
4 Deutsche Telekom Laboratories, TU Berlin
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23*******************************************************************************/
24
25// https://AudioProcessingFramework.github.io/
26
29
30#ifndef APF_MATH_H
31#define APF_MATH_H
32
33#include <cmath> // for std::pow(), ...
34#include <iterator> // for std::iterator_traits
35#include <numeric> // for std::accumulate()
36#include <algorithm> // for std::max()
37
38namespace apf
39{
41namespace math
42{
43
46template<typename T> constexpr T pi();
47
50template<>
51constexpr float pi<float>()
52{
53 // 9 digits are needed for float, 17 digits for double, 21 for long double
54 // TODO: But this may be different on other hardware platforms ...?
55 return 3.1415926535897932384626433832795f;
56}
57
60template<>
61constexpr double pi<double>()
62{
63 return 3.1415926535897932384626433832795;
64}
65
68template<>
69constexpr long double pi<long double>()
70{
71 return 3.1415926535897932384626433832795l;
72}
73
75template<typename T>
76inline T pi_div_180()
77{
78 // local variable to avoid warning message about conversion
79 T q = 180;
80 return pi<T>() / q;
81}
82
87template<typename T> inline T square(T x) { return x*x; }
88
95template<typename T>
96inline T dB2linear(T L, bool power = false)
97{
98 T f = 20; if (power) f = 10;
99 T base = 10;
100 return std::pow(base, L / f);
101}
102
110template<typename T>
111inline T linear2dB(T x, bool power = false)
112{
113 T f = 20; if (power) f = 10;
114 return f * std::log10(x);
115}
116
121template<typename T>
122inline T deg2rad(T angle)
123{
124 return angle * pi_div_180<T>();
125}
126
131template<typename T>
132inline T rad2deg(T angle)
133{
134 return angle / pi_div_180<T>();
135}
136
140template<typename T>
141inline T fwrap(T x, T full)
142{
143 x = std::fmod(x, full);
144 if (x < 0) x += full;
145 return x;
146}
147
150template<typename T>
151inline T wrap(T x, T full)
152{
153 x %= full;
154 if (x < 0) x += full;
155 return x;
156}
157
160template<>
161inline float wrap(float x, float full)
162{
163 return fwrap(x, full);
164}
165
168template<>
169inline double wrap(double x, double full)
170{
171 return fwrap(x, full);
172}
173
176template<>
177inline long double wrap(long double x, long double full)
178{
179 return fwrap(x, full);
180}
181
182template<typename T>
183inline T wrap_two_pi(T x)
184{
185 // local variable to avoid warning message about conversion
186 T two = 2;
187 return wrap(x, two * pi<T>());
188}
189
195template<typename T>
196inline T next_power_of_2(T number)
197{
198 T power_of_2 = 1;
199 while (power_of_2 < number) power_of_2 *= 2;
200 return power_of_2;
201}
202
208template<typename I>
209inline typename std::iterator_traits<I>::value_type
210max_amplitude(I begin, I end)
211{
212 using T = typename std::iterator_traits<I>::value_type;
213 return std::accumulate(begin, end, T()
214 , [] (T current, T next) { return std::max(current, std::abs(next)); });
215}
216
222template<typename I>
223inline typename std::iterator_traits<I>::value_type rms(I begin, I end)
224{
225 using T = typename std::iterator_traits<I>::value_type;
226
227 // inner product: sum of squares
228 // divided by number: mean
229 // sqrt: root
230 return std::sqrt(std::inner_product(begin, end, begin, T())
231 / static_cast<T>(std::distance(begin, end)));
232}
233
237template<typename I>
238bool has_only_zeros(I first, I last)
239{
240 while (first != last) if (*first++ != 0) return false;
241 return true;
242}
243
247template<typename T>
249{
250 public:
251 using argument_type = T;
252 using result_type = T;
253
255 explicit raised_cosine(T period = 0) : _period(period) {}
256
258 T operator()(T in) const
259 {
260 // local variable to avoid warning about conversion
261 T half = 0.5;
262 return std::cos(in * 2 * pi<T>() / _period) * half + half;
263 }
264
265 private:
266 const T _period;
267};
268
276template<typename T, typename U = T>
278{
279 public:
280 using argument_type = U;
281 using result_type = T;
282
284 linear_interpolator() : _first(), _increment() {}
285
288 linear_interpolator(result_type first, result_type last
289 , argument_type length = argument_type(1))
290 {
291 this->set(first, last, length);
292 }
293
298 void set(result_type first, result_type last
299 , argument_type length = argument_type(1))
300 {
301 _first = first;
302 _increment = (last - first) / length;
303 }
304
306 result_type operator()(argument_type x)
307 {
308 return _first + result_type(x) * _increment;
309 }
310
311 private:
312 result_type _first, _increment;
313};
314
316template<typename T>
317linear_interpolator<T>
319{
320 return linear_interpolator<T>(first, last);
321}
322
324template<typename T, typename U>
325linear_interpolator<T, U>
326make_linear_interpolator(T first, T last, U length)
327{
328 return linear_interpolator<T, U>(first, last, length);
329}
330
332template<typename T>
333struct identity { const T& operator()(const T& in) { return in; } };
334
335} // namespace math
336} // namespace apf
337
338#endif
Function object for linear interpolation.
Definition: math.h:278
result_type operator()(argument_type x)
Function call operator.
Definition: math.h:306
void set(result_type first, result_type last, argument_type length=argument_type(1))
Set range and optional length.
Definition: math.h:298
linear_interpolator(result_type first, result_type last, argument_type length=argument_type(1))
Constructor with range and optional length.
Definition: math.h:288
linear_interpolator()
Default constructor.
Definition: math.h:284
Raised cosine (function object).
Definition: math.h:249
T operator()(T in) const
Function call operator.
Definition: math.h:258
raised_cosine(T period=0)
Constructor.
Definition: math.h:255
constexpr float pi< float >()
.
Definition: math.h:51
constexpr double pi< double >()
.
Definition: math.h:61
T rad2deg(T angle)
Convert an angle in radians to an angle in degrees.
Definition: math.h:132
constexpr T pi()
.
T dB2linear(T L, bool power=false)
Convert a level in decibel to a linear gain factor.
Definition: math.h:96
T linear2dB(T x, bool power=false)
Convert a linear gain factor to a level in dB.
Definition: math.h:111
bool has_only_zeros(I first, I last)
Check if there are only zeros in a range.
Definition: math.h:238
T wrap(T x, T full)
Wrap x into the interval [0, full).
Definition: math.h:151
constexpr long double pi< long double >()
.
Definition: math.h:69
std::iterator_traits< I >::value_type rms(I begin, I end)
Root Mean Square (RMS) value of a series of numbers.
Definition: math.h:223
T fwrap(T x, T full)
Wrap x into the interval [0, full).
Definition: math.h:141
T square(T x)
Product of a number with itself.
Definition: math.h:87
T next_power_of_2(T number)
Find a power of 2 which is >= a given number.
Definition: math.h:196
std::iterator_traits< I >::value_type max_amplitude(I begin, I end)
Return the absolute maximum of a series of numbers.
Definition: math.h:210
T deg2rad(T angle)
Convert an angle in degrees to an angle in radians.
Definition: math.h:122
T pi_div_180()
Definition: math.h:76
linear_interpolator< T > make_linear_interpolator(T first, T last)
Helper function for automatic template type deduction.
Definition: math.h:318
Audio Processing Framework.
Definition: iterator.h:61
Identity function object. Function call returns a const reference to input.
Definition: math.h:333