Go Down

Topic: Non-linear interpolation table help needed (Read 1 time) previous topic - next topic

Lsnyman

That is exactly what I intend. I am breaking the curve into 10 ranges and need to interpolate the values. So if the value is example 800, the range above is 830 (10cm) and below is 790 (7.2cm), then the interpolation should return 8.5 cm ( just example).
This is the explanation given in the Arduino Cookbook forthe recipe "measuring distance more accurately".
But I am lost as to how it works.

GoForSmoke

Have you ever interpolated tabled values by hand before? Like a sine table?

Basically your table would have an entry (array element) for each unit of range, possibly 1 per foot or meter. and you would compare your analog input to each entry in turn until you found the first entry bigger than your input. If the first bigger than your input is the first entry (array[0]) then you're out of range as would be the case where your input is bigger than the last entry but in between you can interpolate.

Say your input reads 780 (call it R) and there are table values 740 (call it L) and 800 (call it H).

The percentage of the range between L and H that R falls on is ((100 x (R - L)) / (H - L)). If you want to get 3 places then multiply by 1000 to get 10ths of a percent, etc. Then you have distance corresponding to L plus percentage times 1 unit for your interpolated answer. Just take care about your units if you use integers or use floating-point since exactness isn't really any issue. With FP fastest processing shouldn't be either, it's your choice.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Lsnyman

Thank you
Now I understand the function. Do you have a code example with explanation?

PeterH

I would suggest a slightly different approach.

Use DIV and MOD operations to convert your input value into an array index, and a remainder.

Use the array index to look up two adjacent values in the array. Use the remainder to interpolate between these two values.

The result of the interpolation is your answer.
I only provide help via the forum - please do not contact me for private consultancy.

GoForSmoke

How does that work out for a non-linear table of say, sine values 0 - pi/2?
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up