 # Type of command to fix Linearity issues?

I plan to play around with temperature probes etc. I understand how to use regular mapping to achieve this. However…there may be linearity issues. What is the proper way to map one value to another in a non linear fashion? say I was 3 points the curve instead of 2 etc. Is Multimap the right way? Having a hard time finding code example of that I can understand. Or some other way? Like using the value coming in from the sensor to increment an array? That seems not very precise.

Thank You

Multimap show a few examples, like S-curve and trapezium. http://playground.arduino.cc/Main/MultiMap

There is no proper way to map a curve, because it depends on how the values are defined. Some curves go through every point, other curves don't. Have for example a look at the Bezier curve : http://en.wikipedia.org/wiki/B%C3%A9zier_curve It that good ? for some applications it is perfect, for others not at all.

You need a look up table stored in an array. The best is to have one entry for every possible input reading. The next best is to have as many entries as you can, search for the two either side of your actual input and do a linear interpolation. If you want to use three points then use a bi- cubic curve, but there is not much advantage in this.

Store your array in program memory to prevent usage do limited SRAM.

I like the map feature because it does extrapolation. Suppose I am using the change in resistance of a piece of metal flexing like in a digital scale. The number of possible analog inputs might be exhausting to build an array for. Mapping does all the extrapolation but has linearity issues. Best of both worlds anywhere? Like somehow doing a map like (1, 2, 3, 0, 1, 2) instead of (1, 2, 0, 1)? Is there any way to extrapolate without making huge arrays but still correct for linearity?

Thank You

Is there any way to extrapolate without making huge arrays but still correct for linearity?

You are scaring that poor bush, beating around it like that.

Cut the crap, and tell us what non-linear device you are dealing with. Provide some output as you make the sensor go through its range.

The ideal situation is to have a function - y=f(x) - that correctly calculates all values . However this is not always possible, or it takes too much time1. Then a lookup table that contains all possible points can be the next ideal option2. As that is not -always -feasible on an Arduino (or any computer) a lookup table with interpolation is a realistic option. That is exactly what MultiMap() does.

The number of points you need depends on (1) the linearity of the graph between two points and (2) the error - abs or rel - that is acceptable for your application. E.g. if the curve is “very dynamic” - has much non linearity - you need more points. MultiMap() can have different “local densities” of input values to handle increased “local dynamics”. The only restriction is the input values should be increasing.

There is one important difference between Map() and MultiMap(), and that is that the Map() function interpolates also outside the given input/output range where MultiMap() truncates to the first/last value of the output range.
e.g.
x = map(10, 100, 200, 50, 100); results in x = 5 a value outside the output range.

1One could cache calculated values to speed up the math but that only works if input values tend to repeat
2Another second best is an approximation function of course

update: added - always - in line 3,

Then a lookup table that contains all possible points can be the next ideal option2. As that is not feasible on an Arduino (or any computer)

I would disagree with that.

As the sensor is being read with a 10 bit A/D then there are only 1024 possible readings. Having a 1K look up table is perfectly feasible. If it is an int array then this will only take up 2K of memory which can easily be accommodated in program memory if need be. I have used look up tables much bigger than that in the past to do things like transfer function harmonic synthesis.

You're right Grumpy, a lookup table can be placed in FLASH allowing for larger sizes than RAM allows. And if FLASH is limiting external media like SD cards or ... can be used to hold lookup tables.