|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /*******************************************************************************
-
- "A Collection of Useful C++ Classes for Digital Signal Processing"
- By Vinnie Falco
-
- Official project location:
- https://github.com/vinniefalco/DSPFilters
-
- See Documentation.cpp for contact information, notes, and bibliography.
-
- --------------------------------------------------------------------------------
-
- License: MIT License (http://www.opensource.org/licenses/mit-license.php)
- Copyright (c) 2009 by Vinnie Falco
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-
- *******************************************************************************/
-
- #ifndef DSPFILTERS_MATHSUPPLEMENT_H
- #define DSPFILTERS_MATHSUPPLEMENT_H
-
- #include "Common.h"
-
- namespace Dsp
- {
- const double doublePi = 3.1415926535897932384626433832795028841971;
- const double doublePi_2 = 1.5707963267948966192313216916397514420986;
- const double doubleLn2 = 0.69314718055994530941723212145818;//?????
- const double doubleLn10 = 2.3025850929940456840179914546844;//??????
-
- typedef std::complex<double> complex_t;
- typedef std::pair<complex_t, complex_t> complex_pair_t;
-
- template <typename Real>
- std::complex<Real> solve_quadratic_1(Real a, Real b, Real c) { return (-b + sqrt(std::complex<Real>(b * b - 4 * a * c, 0))) / (2. * a); }
-
- template <typename Real>
- std::complex<Real> solve_quadratic_2(Real a, Real b, Real c) { return (-b - sqrt(std::complex<Real>(b * b - 4 * a * c, 0))) / (2. * a); }
-
- inline complex_t infinity() { return complex_t(std::numeric_limits<double>::infinity()); }
-
- inline complex_t adjust_imag(const complex_t& c)
- {
- if (fabs(c.imag()) < 1e-30) { return complex_t(c.real(), 0); }
- return c;
- }
-
- template <typename Ty, typename To>
- std::complex<Ty> addmul(const std::complex<Ty>& c, Ty v, const std::complex<To>& c1)
- {
- return std::complex<Ty>(c.real() + v * c1.real(), c.imag() + v * c1.imag());
- }
-
- template <typename Ty>
- std::complex<Ty> recip(const std::complex<Ty>& c)
- {
- Ty n = 1.0 / std::norm(c);
-
- return std::complex<Ty>(n * c.real(), n * c.imag());
- }
-
- template <typename Ty>
- Ty asinh(Ty x) { return log(x + std::sqrt(x * x + 1)); }
-
- template <typename Ty>
- Ty acosh(Ty x) { return log(x + std::sqrt(x * x - 1)); }
-
- template <typename Ty>
- bool is_nan(Ty v) { return !(v == v); }
-
- template <>
- inline bool is_nan<complex_t>(complex_t v) { return is_nan(v.real()) || is_nan(v.imag()); }
-
- //------------------------------------------------------------------------------
-
- /*
- * Hack to prevent denormals
- *
- */
-
- //const double anti_denormal_vsa = 1e-16; // doesn't prevent denormals
- //const double anti_denormal_vsa = 0;
- const double anti_denormal_vsa = 1e-8;
-
- class DenormalPrevention
- {
- public:
- DenormalPrevention() : m_v(anti_denormal_vsa) { }
-
- // small alternating current
- double ac() { return m_v = -m_v; }
-
- // small direct current
- static double dc() { return anti_denormal_vsa; }
-
- private:
- double m_v = 0;
- };
- } // namespace Dsp
-
- #endif
|