lut.c 510 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
double interp(double x, double *xs, double *ys, int count)
{
    int i;
    double dx, dy;

    if (x < xs[0]) {
        return 1.0; /* return minimum element */
    }

    if (x > xs[count-1]) {
        return 0.0; /* return maximum */
    }

    /* find i, such that xs[i] <= x < xs[i+1] */
    for (i = 0; i < count-1; i++) {
        if (xs[i+1] > x) {
            break;
        }
    }

    /* interpolate */
    dx = xs[i+1] - xs[i];
    dy = ys[i+1] - ys[i];
    return ys[i] + (x - xs[i]) * dy / dx;
}