Go Down

Topic: multiMap() function - integer linear interpolation (Read 3 times) previous topic - next topic

robtillaart


After playing with my Sharp distancesensor - SHARP 2Y0A02 F 9Y - I needed a mapping from measurements upon centimeters. As the functions I found did not cover the whole range with the accuracy needed I decided to write a linear interpolation function. There was allready one on the playground called reMap() but that used floats, so I redid it in integer to improve performance and robustness. Furthermore I added some snippets how multiMap() can be used.

All remarks, improvements or good examples are - as allways - welcome.

Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Graynomad

Sounds like a good idea, where's the code?

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

robtillaart

#2
Jan 28, 2011, 05:27 pm Last Edit: Feb 02, 2011, 12:17 am by robtillaart Reason: 1
Sorry - code and example snippets can be found on the playground - http://arduino.cc/playground/Main/MultiMap

Rob
[update 30-1-2011]
- didn't post the code here as small optimizations are planned
[update 2-2-2011]
- code on playground updated.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jmcgihon

I know this is an old thread, but I made it work with decremented IN values:

int multiMap(int val, int* _in, int* _out, uint8_t size)
{
 uint8_t pos = 1;
 if(_in[0] < _in[1]) {
   if (val <= _in[0]) return _out[0];
   if (val >= _in[size-1]) return _out[size-1];
   if (val == _in[pos]) return _out[pos];
   while(val > _in[pos]) pos++;
 } else {
   if (val >= _in[0]) return _out[0];
   if (val <= _in[size-1]) return _out[size-1];
   if (val == _in[pos]) return _out[pos];
   while(val < _in[pos]) pos++;
 }
 // interpolate in the right segment for the rest
 return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}

HazardsMind

If you have the option of using both ints and floats, why not use one function and use a template?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up