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.

150 lines
5.4KB

  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. template <typename FloatType>
  18. LookupTable<FloatType>::LookupTable()
  19. {
  20. data.resize (1);
  21. }
  22. template <typename FloatType>
  23. LookupTable<FloatType>::LookupTable (const std::function<FloatType (size_t)>& functionToApproximate,
  24. size_t numPointsToUse)
  25. {
  26. initialise (functionToApproximate, numPointsToUse);
  27. }
  28. //==============================================================================
  29. template <typename FloatType>
  30. void LookupTable<FloatType>::initialise (const std::function<FloatType (size_t)>& functionToApproximate,
  31. size_t numPointsToUse)
  32. {
  33. data.resize (static_cast<int> (getRequiredBufferSize (numPointsToUse)));
  34. for (size_t i = 0; i < numPointsToUse; ++i)
  35. {
  36. auto value = functionToApproximate (i);
  37. jassert (! std::isnan (value));
  38. jassert (! std::isinf (value));
  39. // Make sure functionToApproximate returns a sensible value for the entire specified range.
  40. // E.g., this won't work for zero: [] (size_t i) { return 1.0f / i; }
  41. data.getReference (static_cast<int> (i)) = value;
  42. }
  43. prepare();
  44. }
  45. template <typename FloatType>
  46. void LookupTable<FloatType>::prepare() noexcept
  47. {
  48. auto guardIndex = static_cast<int> (getGuardIndex());
  49. data.getReference (guardIndex) = data.getUnchecked (guardIndex - 1);
  50. }
  51. template <typename FloatType>
  52. void LookupTableTransform<FloatType>::initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
  53. FloatType minInputValueToUse,
  54. FloatType maxInputValueToUse,
  55. size_t numPoints)
  56. {
  57. jassert (maxInputValueToUse > minInputValueToUse);
  58. minInputValue = minInputValueToUse;
  59. maxInputValue = maxInputValueToUse;
  60. scaler = FloatType (numPoints - 1) / (maxInputValueToUse - minInputValueToUse);
  61. offset = -minInputValueToUse * scaler;
  62. const auto initFn = [functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints] (size_t i)
  63. {
  64. return functionToApproximate (
  65. jlimit (
  66. minInputValueToUse, maxInputValueToUse,
  67. jmap (FloatType (i), FloatType (0), FloatType (numPoints - 1), minInputValueToUse, maxInputValueToUse))
  68. );
  69. };
  70. lookupTable.initialise (initFn, numPoints);
  71. }
  72. //==============================================================================
  73. template <typename FloatType>
  74. double LookupTableTransform<FloatType>::calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
  75. FloatType minInputValue,
  76. FloatType maxInputValue,
  77. size_t numPoints,
  78. size_t numTestPoints)
  79. {
  80. jassert (maxInputValue > minInputValue);
  81. if (numTestPoints == 0)
  82. numTestPoints = 100 * numPoints; // use default
  83. LookupTableTransform transform (functionToApproximate, minInputValue, maxInputValue, numPoints);
  84. double maxError = 0;
  85. for (size_t i = 0; i < numTestPoints; ++i)
  86. {
  87. auto inputValue = jmap (FloatType (i), FloatType (0), FloatType (numTestPoints - 1), minInputValue, maxInputValue);
  88. auto approximatedOutputValue = transform.processSample (inputValue);
  89. auto referenceOutputValue = functionToApproximate (inputValue);
  90. maxError = jmax (maxError, calculateRelativeDifference ((double) referenceOutputValue, (double) approximatedOutputValue));
  91. }
  92. return maxError;
  93. }
  94. //==============================================================================
  95. template <typename FloatType>
  96. double LookupTableTransform<FloatType>::calculateRelativeDifference (double x, double y) noexcept
  97. {
  98. static const auto eps = std::numeric_limits<double>::min();
  99. auto absX = std::abs (x);
  100. auto absY = std::abs (y);
  101. auto absDiff = std::abs (x - y);
  102. if (absX < eps)
  103. {
  104. if (absY >= eps)
  105. return absDiff / absY;
  106. return absDiff; // return the absolute error if both numbers are too close to zero
  107. }
  108. return absDiff / std::min (absX, absY);
  109. }
  110. //==============================================================================
  111. template class LookupTable<float>;
  112. template class LookupTable<double>;
  113. template class LookupTableTransform<float>;
  114. template class LookupTableTransform<double>;
  115. } // namespace dsp
  116. } // namespace juce