LMMS
Loading...
Searching...
No Matches
juce_LookupTable.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28namespace dsp
29{
30
31template <typename FloatType>
36
37template <typename FloatType>
38LookupTable<FloatType>::LookupTable (const std::function<FloatType (size_t)>& functionToApproximate,
39 size_t numPointsToUse)
40{
41 initialise (functionToApproximate, numPointsToUse);
42}
43
44//==============================================================================
45template <typename FloatType>
46void LookupTable<FloatType>::initialise (const std::function<FloatType (size_t)>& functionToApproximate,
47 size_t numPointsToUse)
48{
49 data.resize (static_cast<int> (getRequiredBufferSize (numPointsToUse)));
50
51 for (size_t i = 0; i < numPointsToUse; ++i)
52 {
53 auto value = functionToApproximate (i);
54
55 jassert (! std::isnan (value));
56 jassert (! std::isinf (value));
57 // Make sure functionToApproximate returns a sensible value for the entire specified range.
58 // E.g., this won't work for zero: [] (size_t i) { return 1.0f / i; }
59
60 data.getReference (static_cast<int> (i)) = value;
61 }
63 prepare();
64}
65
66template <typename FloatType>
68{
69 auto guardIndex = static_cast<int> (getGuardIndex());
70 data.getReference (guardIndex) = data.getUnchecked (guardIndex - 1);
71}
72
73template <typename FloatType>
74void LookupTableTransform<FloatType>::initialise (const std::function<FloatType (FloatType)>& functionToApproximate,
75 FloatType minInputValueToUse,
76 FloatType maxInputValueToUse,
77 size_t numPoints)
78{
79 jassert (maxInputValueToUse > minInputValueToUse);
80
81 minInputValue = minInputValueToUse;
82 maxInputValue = maxInputValueToUse;
83 scaler = FloatType (numPoints - 1) / (maxInputValueToUse - minInputValueToUse);
84 offset = -minInputValueToUse * scaler;
85
86 const auto initFn = [functionToApproximate, minInputValueToUse, maxInputValueToUse, numPoints] (size_t i)
87 {
88 return functionToApproximate (
89 jlimit (
90 minInputValueToUse, maxInputValueToUse,
91 jmap (FloatType (i), FloatType (0), FloatType (numPoints - 1), minInputValueToUse, maxInputValueToUse))
92 );
93 };
94
95 lookupTable.initialise (initFn, numPoints);
96}
97
98//==============================================================================
99template <typename FloatType>
100double LookupTableTransform<FloatType>::calculateMaxRelativeError (const std::function<FloatType (FloatType)>& functionToApproximate,
101 FloatType minInputValue,
102 FloatType maxInputValue,
103 size_t numPoints,
104 size_t numTestPoints)
105{
107
108 if (numTestPoints == 0)
109 numTestPoints = 100 * numPoints; // use default
110
111 LookupTableTransform transform (functionToApproximate, minInputValue, maxInputValue, numPoints);
112
113 double maxError = 0;
114
115 for (size_t i = 0; i < numTestPoints; ++i)
116 {
117 auto inputValue = jmap (FloatType (i), FloatType (0), FloatType (numTestPoints - 1), minInputValue, maxInputValue);
118 auto approximatedOutputValue = transform.processSample (inputValue);
119 auto referenceOutputValue = functionToApproximate (inputValue);
120
121 maxError = jmax (maxError, calculateRelativeDifference ((double) referenceOutputValue, (double) approximatedOutputValue));
122 }
123
124 return maxError;
125}
126
127//==============================================================================
128template <typename FloatType>
130{
131 static const auto eps = std::numeric_limits<double>::min();
132
133 auto absX = std::abs (x);
134 auto absY = std::abs (y);
135 auto absDiff = std::abs (x - y);
136
137 if (absX < eps)
138 {
139 if (absY >= eps)
140 return absDiff / absY;
141
142 return absDiff; // return the absolute error if both numbers are too close to zero
143 }
144
145 return absDiff / std::min (absX, absY);
146}
148//==============================================================================
149template class LookupTable<float>;
150template class LookupTable<double>;
151
152template class LookupTableTransform<float>;
153template class LookupTableTransform<double>;
154
155} // namespace dsp
156} // namespace juce
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
#define noexcept
Definition DistrhoDefines.h:72
size_t getGuardIndex() const noexcept
Definition juce_LookupTable.h:149
static size_t getRequiredBufferSize(size_t numPointsToUse) noexcept
Definition juce_LookupTable.h:148
void prepare() noexcept
Definition juce_LookupTable.cpp:67
void initialise(const std::function< FloatType(size_t)> &functionToApproximate, size_t numPointsToUse)
Definition juce_LookupTable.cpp:46
Array< FloatType > data
Definition juce_LookupTable.h:145
LookupTable()
Definition juce_LookupTable.cpp:32
Definition juce_LookupTable.h:175
FloatType minInputValue
Definition juce_LookupTable.h:323
void initialise(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValueToUse, FloatType maxInputValueToUse, size_t numPoints)
Definition juce_LookupTable.cpp:74
FloatType processSample(FloatType value) const noexcept
Definition juce_LookupTable.h:253
static double calculateRelativeDifference(double, double) noexcept
Definition juce_LookupTable.cpp:129
FloatType maxInputValue
Definition juce_LookupTable.h:323
static double calculateMaxRelativeError(const std::function< FloatType(FloatType)> &functionToApproximate, FloatType minInputValue, FloatType maxInputValue, size_t numPoints, size_t numTestPoints=0)
Definition juce_LookupTable.cpp:100
int y
Definition inflate.c:1588
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
static PuglViewHint int value
Definition pugl.h:1708
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
constexpr Type jmap(Type value0To1, Type targetRangeMin, Type targetRangeMax)
Definition juce_MathsFunctions.h:120
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Definition juce_MathsFunctions.h:262