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.

158 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. template <typename FloatType>
  24. LookupTable<FloatType>::LookupTable()
  25. {
  26. data.resize (1);
  27. }
  28. template <typename FloatType>
  29. LookupTable<FloatType>::LookupTable (const std::function<FloatType (size_t)>& functionToApproximate,
  30. size_t numPointsToUse)
  31. {
  32. initialise (functionToApproximate, numPointsToUse);
  33. }
  34. //==============================================================================
  35. template <typename FloatType>
  36. void LookupTable<FloatType>::initialise (const std::function<FloatType (size_t)>& functionToApproximate,
  37. size_t numPointsToUse)
  38. {
  39. data.resize (static_cast<int> (getRequiredBufferSize (numPointsToUse)));
  40. for (size_t i = 0; i < numPointsToUse; ++i)
  41. {
  42. auto value = functionToApproximate (i);
  43. jassert (! std::isnan (value));
  44. jassert (! std::isinf (value));
  45. // Make sure functionToApproximate returns a sensible value for the entire specified range.
  46. // E.g., this won't work for zero: [] (size_t i) { return 1.0f / i; }
  47. data.getReference (static_cast<int> (i)) = value;
  48. }
  49. prepare();
  50. }
  51. template <typename FloatType>
  52. void LookupTable<FloatType>::prepare() noexcept
  53. {
  54. auto guardIndex = static_cast<int> (getGuardIndex());
  55. data.getReference (guardIndex) = data.getUnchecked (guardIndex - 1);
  56. }
  57. template <typename FloatType>
  58. void LookupTableTransform<FloatType>::initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
  59. FloatType minInputValueToUse,
  60. FloatType maxInputValueToUse,
  61. size_t numPoints)
  62. {
  63. jassert (maxInputValueToUse > minInputValueToUse);
  64. minInputValue = minInputValueToUse;
  65. maxInputValue = maxInputValueToUse;
  66. scaler = FloatType (numPoints - 1) / (maxInputValueToUse - minInputValueToUse);
  67. offset = -minInputValueToUse * scaler;
  68. const auto initFn = [functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints] (size_t i)
  69. {
  70. return functionToApproximate (
  71. jlimit (
  72. minInputValueToUse, maxInputValueToUse,
  73. jmap (FloatType (i), FloatType (0), FloatType (numPoints - 1), minInputValueToUse, maxInputValueToUse))
  74. );
  75. };
  76. lookupTable.initialise (initFn, numPoints);
  77. }
  78. //==============================================================================
  79. template <typename FloatType>
  80. double LookupTableTransform<FloatType>::calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
  81. FloatType minInputValue,
  82. FloatType maxInputValue,
  83. size_t numPoints,
  84. size_t numTestPoints)
  85. {
  86. jassert (maxInputValue > minInputValue);
  87. if (numTestPoints == 0)
  88. numTestPoints = 100 * numPoints; // use default
  89. LookupTableTransform transform (functionToApproximate, minInputValue, maxInputValue, numPoints);
  90. double maxError = 0;
  91. for (size_t i = 0; i < numTestPoints; ++i)
  92. {
  93. auto inputValue = jmap (FloatType (i), FloatType (0), FloatType (numTestPoints - 1), minInputValue, maxInputValue);
  94. auto approximatedOutputValue = transform.processSample (inputValue);
  95. auto referenceOutputValue = functionToApproximate (inputValue);
  96. maxError = jmax (maxError, calculateRelativeDifference ((double) referenceOutputValue, (double) approximatedOutputValue));
  97. }
  98. return maxError;
  99. }
  100. //==============================================================================
  101. template <typename FloatType>
  102. double LookupTableTransform<FloatType>::calculateRelativeDifference (double x, double y) noexcept
  103. {
  104. static const auto eps = std::numeric_limits<double>::min();
  105. auto absX = std::abs (x);
  106. auto absY = std::abs (y);
  107. auto absDiff = std::abs (x - y);
  108. if (absX < eps)
  109. {
  110. if (absY >= eps)
  111. return absDiff / absY;
  112. return absDiff; // return the absolute error if both numbers are too close to zero
  113. }
  114. return absDiff / std::min (absX, absY);
  115. }
  116. //==============================================================================
  117. template class LookupTable<float>;
  118. template class LookupTable<double>;
  119. template class LookupTableTransform<float>;
  120. template class LookupTableTransform<double>;
  121. } // namespace dsp
  122. } // namespace juce