You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MathSupplement.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*******************************************************************************
  2. "A Collection of Useful C++ Classes for Digital Signal Processing"
  3. By Vinnie Falco
  4. Official project location:
  5. https://github.com/vinniefalco/DSPFilters
  6. See Documentation.cpp for contact information, notes, and bibliography.
  7. --------------------------------------------------------------------------------
  8. License: MIT License (http://www.opensource.org/licenses/mit-license.php)
  9. Copyright (c) 2009 by Vinnie Falco
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. *******************************************************************************/
  26. #ifndef DSPFILTERS_MATHSUPPLEMENT_H
  27. #define DSPFILTERS_MATHSUPPLEMENT_H
  28. #include "Common.h"
  29. namespace Dsp
  30. {
  31. const double doublePi = 3.1415926535897932384626433832795028841971;
  32. const double doublePi_2 = 1.5707963267948966192313216916397514420986;
  33. const double doubleLn2 = 0.69314718055994530941723212145818;//?????
  34. const double doubleLn10 = 2.3025850929940456840179914546844;//??????
  35. typedef std::complex<double> complex_t;
  36. typedef std::pair<complex_t, complex_t> complex_pair_t;
  37. template <typename Real>
  38. 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); }
  39. template <typename Real>
  40. 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); }
  41. inline complex_t infinity() { return complex_t(std::numeric_limits<double>::infinity()); }
  42. inline complex_t adjust_imag(const complex_t& c)
  43. {
  44. if (fabs(c.imag()) < 1e-30) { return complex_t(c.real(), 0); }
  45. return c;
  46. }
  47. template <typename Ty, typename To>
  48. std::complex<Ty> addmul(const std::complex<Ty>& c, Ty v, const std::complex<To>& c1)
  49. {
  50. return std::complex<Ty>(c.real() + v * c1.real(), c.imag() + v * c1.imag());
  51. }
  52. template <typename Ty>
  53. std::complex<Ty> recip(const std::complex<Ty>& c)
  54. {
  55. Ty n = 1.0 / std::norm(c);
  56. return std::complex<Ty>(n * c.real(), n * c.imag());
  57. }
  58. template <typename Ty>
  59. Ty asinh(Ty x) { return log(x + std::sqrt(x * x + 1)); }
  60. template <typename Ty>
  61. Ty acosh(Ty x) { return log(x + std::sqrt(x * x - 1)); }
  62. template <typename Ty>
  63. bool is_nan(Ty v) { return !(v == v); }
  64. template <>
  65. inline bool is_nan<complex_t>(complex_t v) { return is_nan(v.real()) || is_nan(v.imag()); }
  66. //------------------------------------------------------------------------------
  67. /*
  68. * Hack to prevent denormals
  69. *
  70. */
  71. //const double anti_denormal_vsa = 1e-16; // doesn't prevent denormals
  72. //const double anti_denormal_vsa = 0;
  73. const double anti_denormal_vsa = 1e-8;
  74. class DenormalPrevention
  75. {
  76. public:
  77. DenormalPrevention() : m_v(anti_denormal_vsa) { }
  78. // small alternating current
  79. double ac() { return m_v = -m_v; }
  80. // small direct current
  81. static double dc() { return anti_denormal_vsa; }
  82. private:
  83. double m_v = 0;
  84. };
  85. } // namespace Dsp
  86. #endif