The Arduino Interpolation Library provides functions for Step, Linear, Smooth, Catmull Spline, and Constrained Spline interpolation.
User Manual
All library functions are static functions that receive two arrays of X-Values and Y-Values, the size of the arrays, and the X point where the interpolation should be performed, and return the estimated Y value at the X point.
Interpolation Modes
Step
Simple Step interpolation, estimates the value as Yn-1 or Yn based on the location of the X point within each interval. The relative point of change within the interval is an optional parameter. 0.0 means change at the beginning of the interval, 1.0 at the end of the interval, while 0.5 means change in the middle of the interval.
Linear
Linear interpolation. An additional parameter controls whether values outside the X-Values range are extrapolated or clipped.
Smooth
Applies a cubic smoothing function to the change in values.
Catmull spline
Typical Catmull spline interpolation.
Constrained spline
A special type of spline that does not overshoot.
Examples
The Arduino Interpolation Library includes the following examples to illustrate its use.
- Example: Example showing the use of the different interpolation functions
#include "InterpolationLib.h"
const int numValues = 10;
double xValues[10] = { 5, 12, 30, 50, 60, 70, 74, 84, 92, 100 };
double yValues[10] = { 150, 200, 200, 200, 180, 100, 100, 150, 220, 320 };
void setup()
{
while (!Serial) { ; }
Serial.begin(115200);
for (float xValue = 0; xValue <= 110; xValue += .25)
{
Serial.print(Interpolation::Step(xValues, yValues, numValues, xValue, 0.0));
Serial.print(',');
Serial.print(Interpolation::Step(xValues, yValues, numValues, xValue, 0.5));
Serial.print(',');
Serial.print(Interpolation::Step(xValues, yValues, numValues, xValue, 1.0));
Serial.print(',');
Serial.print(Interpolation::SmoothStep(xValues, yValues, numValues, xValue));
Serial.print(',');
Serial.print(Interpolation::Linear(xValues, yValues, numValues, xValue, false));
Serial.print(',');
Serial.print(Interpolation::Linear(xValues, yValues, numValues, xValue, true));
Serial.print(',');
Serial.print(Interpolation::CatmullSpline(xValues, yValues, numValues, xValue));
Serial.print(',');
Serial.println(Interpolation::ConstrainedSpline(xValues, yValues, numValues, xValue));
}
}
void loop()
{
}
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE
Download the Code
All the code from this post is available for download on Github.