Hi,
I have written a function that can interpolate a curve described in a two dimensional array. Hereby, each row is a point (xCurve ,yCurve) on my defined curve(s), and the function calculates an linear interpolation for any point x. This works.
Now I want to extend that function so that the array is not just holding one curve, but several. And that the number of rows (points on x-Axis) is also not pre-defined.
Extension means I have several curves (values on y-axis, in severa columns) in the array, and the program is providing the interpolation for the curve to be taken while for all curves the x-axis is the same. Unfortunately, I'm failing in doing so.
Please refer to my code below:
const byte numRowsTable1 = 13; // Rows of Table 1
const int myTable1 [][3]
{
{ 800, 0, 0},
{ 900, 4956, 2000},
{1000, 7754, 4000},
{1100, 9166, 6000},
{1200, 9775, 8000},
{1300, 9978, 9000},
{1350, 10000, 10000},
{1400, 9978, 9000},
{1500, 9975, 8000},
{1600, 9166, 7000},
{1700, 7754, 6000},
{1800, 4956, 5000},
{1900, 0, 0}
};
const byte numRowsTable2 = 5; // Rows of Table 2
const int myTable2 [][2]
{
{ 80, 10},
{ 90, 50},
{100, 100},
{150, 250},
{300, 500},
};
// 2D-Table Linear Interpolation
int Table2D_s16(int x, int const table2D[][3], byte const numRows, byte curve = 1) {
int ret;
// Check for boundaries of 2D-Table, otherwise interpolate
if (x <= table2D[0][ 0 ]) { // Below lower bound?
ret = table2D[0][curve]; // --> return y(x_min) for all x <= x_min
}
else if (x >= table2D[numRows-1][0]) { // Above upper bound?
ret = table2D[numRows-1][curve]; // --> return y(x_max) for all x >= x_max
}
else { // --> interpolate!
int upper_idx = 1;
while (x > table2D[upper_idx][0]) { // Find approbirate slot for interpolation
upper_idx++;
}
// y = y(k) + (x - x(k)) * (y(k+1)-y(k)) / (x(k+1)- x(k))
ret = table2D[upper_idx - 1][curve] +
(int)( (long)(table2D[upper_idx][curve] - table2D[upper_idx - 1][curve]) *
( x - table2D[upper_idx - 1][ 0 ]) /
(table2D[upper_idx][ 0 ] - table2D[upper_idx - 1][ 0 ]) );
}
return(ret);
}
char buffer[80];
int x, ysoll;
byte curve = 2;
void setup() {
Serial.begin(57600);
delay(3000);
Serial.println("Interpolate curve 2 of Table 1");
for (byte idx = 0; idx < numRowsTable1; idx++){
x = myTable1[idx][ 0 ] + 50;
sprintf(buffer,"At x = %4d (idx = %2d), the table 1 is read to %5d.",
x, idx, Table2D_s16(x, myTable1, numRowsTable1, curve));
Serial.println(buffer);
}
Serial.println("Interpolate the 1st and only curve of Table 2");
for (byte idx = 0; idx < numRowsTable2; idx++){
x = myTable2[idx][ 0 ] + 5;
sprintf(buffer,"At x = %4d (idx = %2d), the table 2 is read to %5d.",
x, idx, Table2D_s16(x, myTable2, numRowsTable2));
Serial.println(buffer);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
With the part for Table 2 in, I get following error message:
C:\Users\Sebastian_2\Documents\Projekte\Arduino\Playground\HCLCurves\HCLCurves.ino: In function 'void setup()':
HCLCurves:76:53: error: cannot convert 'const int (*)[2]' to 'const int (*)[3]' for argument '2' to 'int Table2D_s16(int, const int (*)[3], byte, byte)'
x, idx, Table2D_s16(x, myTable2, numRowsTable2));
^
exit status 1
cannot convert 'const int (*)[2]' to 'const int (*)[3]' for argument '2' to 'int Table2D_s16(int, const int (*)[3], byte, byte)'
For better understanding also the output for curve 2 of Table 1 (the code for table 2 hidden by comments //)
Interpolate curve 2 of Table 1
At x = 850 (idx = 0), the table 1 is read to 1000.
At x = 950 (idx = 1), the table 1 is read to 3000.
At x = 1050 (idx = 2), the table 1 is read to 5000.
At x = 1150 (idx = 3), the table 1 is read to 7000.
At x = 1250 (idx = 4), the table 1 is read to 8500.
At x = 1350 (idx = 5), the table 1 is read to 10000.
At x = 1400 (idx = 6), the table 1 is read to 9000.
At x = 1450 (idx = 7), the table 1 is read to 8500.
At x = 1550 (idx = 8), the table 1 is read to 7500.
At x = 1650 (idx = 9), the table 1 is read to 6500.
At x = 1750 (idx = 10), the table 1 is read to 5500.
At x = 1850 (idx = 11), the table 1 is read to 2500.
At x = 1950 (idx = 12), the table 1 is read to 0.
Can somebody help me to adapt the function Table2D_s16
is working with both tables?
Regards
Sebastian