rm_control
Loading...
Searching...
No Matches
math_utilities.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2021, Qiayuan Liao
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34//
35// Created by qiayuan on 12/22/19.
36//
37
38#pragma once
39
40#include <cmath>
41template <typename T>
42T angularMinus(T a, T b)
43{
44 a = fmod(a, 2.0 * M_PI);
45 b = fmod(b, 2.0 * M_PI);
46
47 T res1 = a - b;
48 T res2 = (a < b) ? (a + 2 * M_PI - b) : (a - 2 * M_PI - b);
49
50 return (std::abs(res1) < std::abs(res2)) ? res1 : res2;
51}
52
53template <typename T>
54T minAbs(T a, T b)
55{
56 T sign = (a < 0.0) ? -1.0 : 1.0;
57 return sign * fmin(fabs(a), b);
58}
59
60template <typename T>
61int sgn(T val)
62{
63 return (T(0) < val) - (val < T(0));
64}
65
66template <typename T>
67T square(T val)
68{
69 return val * val;
70}
71
72template <typename T>
73T alpha(T cutoff, double freq)
74{
75 T tau = 1.0 / (2 * M_PI * cutoff);
76 T te = 1.0 / freq;
77 return 1.0 / (1.0 + tau / te);
78}
int sgn(T val)
Definition math_utilities.h:61
T alpha(T cutoff, double freq)
Definition math_utilities.h:73
T angularMinus(T a, T b)
Definition math_utilities.h:42
T minAbs(T a, T b)
Definition math_utilities.h:54
T square(T val)
Definition math_utilities.h:67