Interpolated lookup table from data points

Hello guys,

I would like to create a lookup table from measured data points. For example:

point1 { -2.5f, -0.5f }
point2 { 0.5f, 0.5f }
point3 { 1.0f, 1.5f }

The lookup table should perform a linear interpolation between the “gaps”. This means when providing an x value of 0.75f (which is the x-middle of point2 and point3) the lookup table should return an interpolated y value which is 1 in this example - the y-middle of point2 and point3.

Would it be possible to use the dsp::LookupTable resp. dsp::LookupTableTransform classes to create such a table? How would I do that?

Thanks in advance!

Dirk

The dsp::LookupTable is generated from a function in the form f(x) = something;.

Your case can be implemented quite easily with a std::map<float, float>. A map has the benefit, it is sorted by default and has lookup times of log(n).

std::map<float, float> lookup;

float getValue (float x) const
{
    auto& next = lookup.lower_bound (x);
    if (next == lookup.begin())
        return next->second;

    if (next == lookup.end())
        next.rbegin()->second;

    auto& prev = std::prev (next, 1);
    return jmap (x, prev->first, next->first, prev.second, next->second);
}

untested code!

Hey Daniel,

thanks for your very fast response. Indeed, your proposal works perfectly. Thank you very much!