Linear Interpolation in C..

Something that's been bugging me for a while, and happy I've finally cracked! there might be a less-expensive way todo it, but seems to work ok for me..
Pretty basic 2D Linear Interpolation, I'm not sure if you would call this piece-meal interpolation or not..

check attachment below for better source too.


double tbl_rpm_to_duty[11][2] =
  {
    {8600, 15},
    {6600, 10},
    {6000, 9.0},
    {5000, 8.0},
    {4500, 20.0},
    {4000, 8.0},
    {3500, 7},
    {3000, 7},
    {2000, 6.9},
    {1000, 6.5},
    {800, 7.5 }
  };


/**
  * My interpolation function
  * should return linear interpolated second dimension value
  * if the col_a value is greater it will return the highest value (capped results)
  * results off the lower end of the chart will interpolate to zero
  */
double interp_table(double dbl_table[][2], int col_a)
{
  short i;
  double col_b_diff;

  for (i=0; dbl_table[i][0]; i++) {
    if( (dbl_table[i][0] >= col_a) && (dbl_table[i+1][0] <= col_a)) {
      col_b_diff = dbl_table[i][1]-dbl_table[i+1][1];
      return dbl_table[i+1][1]+col_b_diff *(col_a-dbl_table[i+1][0]) / (dbl_table[i][0]-dbl_table[i+1][0]);
    }
  }

  if(col_a > dbl_table[0][0]) {
    return dbl_table[0][1];
  }

  return 0;
}

int main(void)
{
  printf("%f\n", interp_table(tbl_rpm_to_duty,9422) );
  printf("%f\n", interp_table(tbl_rpm_to_duty,500) );
  printf("%f\n", interp_table(tbl_rpm_to_duty,5400) );

  return 0;
}
AttachmentSize
map.c1.12 KB