I\'m looking for a java library or some help to write my own interpolation function. That is I have two arrays of doubles which are potentially different sizes, but are ord
you can use apache commons-math interpolation functions, such as SplineInterpolator
Light-weight version of one-dimensional array linear interpolator:
public static float[] interpolate(float[] data) {
int startIdx = -1;
float startValue = 0f;
float element;
for (int i = 0; i < data.length - 1; i++) {
element = data[i];
if (element != 0f) {
if (startIdx != -1) {
doInterpolate(startValue, element, startIdx + 1, i - startIdx - 1, data);
}
startValue = element;
startIdx = i;
}
}
return data;
}
private static void doInterpolate(float start, float end, int startIdx, int count, float[] data) {
float delta = (end - start) / (count + 1);
for (int i = startIdx; i < startIdx + count; i++) {
data[i] = start + delta * (i - startIdx + 1);
}
}
I know this is an old answer, but it's the first google hit when searching for Java interpolation. The accepted answer provides some helpful links, but JMSL must be purchased, and the JSpline+ website looks sketchy.
Apache Commons Math has implementations of linear and spline interpolations that appear simple, functional, and trustworthy.
http://commons.apache.org/proper/commons-math/
be very careful with spline-fits and polynomial fits. These two can give nonsensical behavior that can derail many uses of (what is believed to be a representation of) the data.
Anything that uses derivatives (slopes) of data can be totally derailed.
Best thing you can do is plot the data, understand what it's doing, and only then fit (linear, polynomial, log-log) regression; once you've done that you should plot your fit over the original data and make sure you see reasonable agreement. Skipping this comparison-step is a very bad idea.
Certain data-sets will not yield to fitting of polynomials, log-log etc..; if your data-points are appropriately distributed over the range of data there's nothing wrong with piecewise-interpolation (linear or polynomial etc.). To beat a dead horse, if you use piecewise interpolation avoid anything that uses derivatives/slopes of your piecewise interpolation because it will have discontinuities and will cause things to behave badly.
Simple linear interpolation can be calculated using something like:
Point2D interp1_lin(Point2D p1, Point2D p2, double x) {
//Pre conditions
assert p1.x<x;
assert x<p2.x;
//Calculate slope from p1 to p2
double m = (p2.x-p1.x)/(p2.y-p1.y);
//Calculate y position of x
double y = (x-p1.x)*m+p1.y;
//create new point
return new Point2D.Double(x,y);
}
Does this help?
The other answers give you linear interpolations -- these don't really work for complex, nonlinear data. You want a spline fit, (spline interpolation) I believe.
Spline fits describe regions of the data using a set of control points from the data, then apply a polynomial interpolation between control points. More control points gives you a more accurate fit, less a more general fit. Splines are much more accurate than linear fits, faster to use than a general regression fit, better than a high-order polynomial because it won't do crazy things between control points.
I can't remember names off the top of my head, but there are some excellent fitting libraries in Java -- I suggest you look for one rather than writing your own function.
**EDIT: Libraries that might be useful: **
** Theory/code that may be useful: **