32 template <
typename FloatType>
38 template <
typename FloatType>
40 size_t numPointsToUse)
42 initialise (functionToApproximate, numPointsToUse);
46 template <
typename FloatType>
48 size_t numPointsToUse)
50 data.resize (static_cast<int> (getRequiredBufferSize (numPointsToUse)));
52 for (
size_t i = 0; i < numPointsToUse; ++i)
54 auto value = functionToApproximate (i);
56 jassert (! std::isnan (value));
57 jassert (! std::isinf (value));
61 data.getReference (static_cast<int> (i)) = value;
67 template <
typename FloatType>
70 auto guardIndex =
static_cast<int> (getGuardIndex());
71 data.getReference (guardIndex) = data.
getUnchecked (guardIndex - 1);
74 template <
typename FloatType>
76 FloatType minInputValueToUse,
77 FloatType maxInputValueToUse,
80 jassert (maxInputValueToUse > minInputValueToUse);
82 minInputValue = minInputValueToUse;
83 maxInputValue = maxInputValueToUse;
84 scaler = FloatType (numPoints - 1) / (maxInputValueToUse - minInputValueToUse);
85 offset = -minInputValueToUse * scaler;
87 const auto initFn = [functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints] (
size_t i)
89 return functionToApproximate (
91 minInputValueToUse, maxInputValueToUse,
92 jmap (FloatType (i), FloatType (0), FloatType (numPoints - 1), minInputValueToUse, maxInputValueToUse))
96 lookupTable.initialise (initFn, numPoints);
100 template <
typename FloatType>
102 FloatType minInputValue,
103 FloatType maxInputValue,
105 size_t numTestPoints)
107 jassert (maxInputValue > minInputValue);
109 if (numTestPoints == 0)
110 numTestPoints = 100 * numPoints;
112 LookupTableTransform transform (functionToApproximate, minInputValue, maxInputValue, numPoints);
116 for (
size_t i = 0; i < numTestPoints; ++i)
118 auto inputValue = jmap (FloatType (i), FloatType (0), FloatType (numTestPoints - 1), minInputValue, maxInputValue);
119 auto approximatedOutputValue = transform.
processSample (inputValue);
120 auto referenceOutputValue = functionToApproximate (inputValue);
122 maxError = jmax (maxError, calculateRelativeDifference ((
double) referenceOutputValue, (
double) approximatedOutputValue));
129 template <
typename FloatType>
132 static const auto eps = std::numeric_limits<double>::min();
134 auto absX = std::abs (x);
135 auto absY = std::abs (y);
136 auto absDiff = std::abs (x - y);
141 return absDiff / absY;
146 return absDiff / std::min (absX, absY);
void initialise(const std::function< FloatType(size_t)> &functionToApproximate, size_t numPointsToUse)
Initialises or changes the parameters of a LookupTable object.
FloatType getUnchecked(FloatType index) const noexcept
Calculates the approximated value for the given index without range checking.
LookupTable()
Creates an uninitialised LookupTable object.
Class for efficiently approximating expensive arithmetic operations.