如何使用线性插值在C(SDCC编译器)中构建查找表

Ste*_*fan 3 c linear-interpolation

对于LPC922微控制器(带SDCC),我想创建一个带线性插值的查找表.让我们假设我有x和y值

x=300 y=10,0201 
x=700 y=89,542 
x=800 y=126,452 
x=900 y=171,453 
x=1500 y=225,123
Run Code Online (Sandbox Code Playgroud)

具有线性插值的查找表的代码怎么样,所以我得到x = 850的正确值y((171,453 + 126,452)/ 2)?

Met*_*est 5

typedef struct { double x; double y; } coord_t;

coord_t c[5] = 
{
    {300, 10.02},
    {700, 89.542},
    {800, 126.452}, 
    {900, 171.453}, 
    {1500,225.123}
};    

double interp( coord_t* c, double x, int n )
{
    int i;

    for( i = 0; i < n-1; i++ )
    {
        if ( c[i].x <= x && c[i+1].x >= x )
        {
            double diffx = x - c[i].x;
            double diffn = c[i+1].x - c[i].x;

            return c[i].y + ( c[i+1].y - c[i].y ) * diffx / diffn; 
        }
    }

    return 0; // Not in Range
}

int main(int argc, char** argv) 
{
    double y = interp( c, 850, 5 );
}
Run Code Online (Sandbox Code Playgroud)