The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

137 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace dsp
  16. {
  17. double SpecialFunctions::besselI0 (double x) noexcept
  18. {
  19. auto ax = std::abs (x);
  20. if (ax < 3.75)
  21. {
  22. auto y = x / 3.75;
  23. y *= y;
  24. return 1.0 + y * (3.5156229 + y * (3.0899424 + y * (1.2067492
  25. + y * (0.2659732 + y * (0.360768e-1 + y * 0.45813e-2)))));
  26. }
  27. auto y = 3.75 / ax;
  28. return (std::exp (ax) / std::sqrt (ax))
  29. * (0.39894228 + y * (0.1328592e-1 + y * (0.225319e-2 + y * (-0.157565e-2 + y * (0.916281e-2
  30. + y * (-0.2057706e-1 + y * (0.2635537e-1 + y * (-0.1647633e-1 + y * 0.392377e-2))))))));
  31. }
  32. void SpecialFunctions::ellipticIntegralK (double k, double& K, double& Kp) noexcept
  33. {
  34. constexpr int M = 4;
  35. K = MathConstants<double>::halfPi;
  36. auto lastK = k;
  37. for (int i = 0; i < M; ++i)
  38. {
  39. lastK = std::pow (lastK / (1 + std::sqrt (1 - std::pow (lastK, 2.0))), 2.0);
  40. K *= 1 + lastK;
  41. }
  42. Kp = MathConstants<double>::halfPi;
  43. auto last = std::sqrt (1 - k * k);
  44. for (int i = 0; i < M; ++i)
  45. {
  46. last = std::pow (last / (1.0 + std::sqrt (1.0 - std::pow (last, 2.0))), 2.0);
  47. Kp *= 1 + last;
  48. }
  49. }
  50. Complex<double> SpecialFunctions::cde (Complex<double> u, double k) noexcept
  51. {
  52. constexpr int M = 4;
  53. double ke[M + 1];
  54. double* kei = ke;
  55. *kei = k;
  56. for (int i = 0; i < M; ++i)
  57. {
  58. auto next = std::pow (*kei / (1.0 + std::sqrt (1.0 - std::pow (*kei, 2.0))), 2.0);
  59. *++kei = next;
  60. }
  61. // NB: the spurious cast to double here is a workaround for a very odd link-time failure
  62. std::complex<double> last = std::cos (u * (double) MathConstants<double>::halfPi);
  63. for (int i = M - 1; i >= 0; --i)
  64. last = (1.0 + ke[i + 1]) / (1.0 / last + ke[i + 1] * last);
  65. return last;
  66. }
  67. Complex<double> SpecialFunctions::sne (Complex<double> u, double k) noexcept
  68. {
  69. constexpr int M = 4;
  70. double ke[M + 1];
  71. double* kei = ke;
  72. *kei = k;
  73. for (int i = 0; i < M; ++i)
  74. {
  75. auto next = std::pow (*kei / (1 + std::sqrt (1 - std::pow (*kei, 2.0))), 2.0);
  76. *++kei = next;
  77. }
  78. // NB: the spurious cast to double here is a workaround for a very odd link-time failure
  79. std::complex<double> last = std::sin (u * (double) MathConstants<double>::halfPi);
  80. for (int i = M - 1; i >= 0; --i)
  81. last = (1.0 + ke[i + 1]) / (1.0 / last + ke[i + 1] * last);
  82. return last;
  83. }
  84. Complex<double> SpecialFunctions::asne (Complex<double> w, double k) noexcept
  85. {
  86. constexpr int M = 4;
  87. double ke[M + 1];
  88. double* kei = ke;
  89. *kei = k;
  90. for (int i = 0; i < M; ++i)
  91. {
  92. auto next = std::pow (*kei / (1.0 + std::sqrt (1.0 - std::pow (*kei, 2.0))), 2.0);
  93. *++kei = next;
  94. }
  95. std::complex<double> last = w;
  96. for (int i = 1; i <= M; ++i)
  97. last = 2.0 * last / ((1.0 + ke[i]) * (1.0 + std::sqrt (1.0 - std::pow (ke[i - 1] * last, 2.0))));
  98. return 2.0 / MathConstants<double>::pi * std::asin (last);
  99. }
  100. } // namespace dsp
  101. } // namespace juce