Implementing the MATLAB interp1 function in J2ME

I am looking to implement interp1 interpolation, 1-D data (search in a table), a function available in MATLAB in J2ME or JAVA. here is the link

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/interp1.html

Is there any library in J2ME or JAVA that has already implemented the same function? If no one can help me implement the interp1 function in J2ME or JAVA?

0
java matlab java-me interpolation linear-interpolation
source share
2 answers

I just found out the method used for linear interpolation if the "linear" parameter is selected for the syntax of the interp1 function, which is: interp1 (x, y, xi, 'linear'). This is implemented in the interp (double x) method of the LinearInterpolator class, which is present in the multigraph package. link below

http://multigraph.sourceforge.net/multigraph/javadoc/multigraph/LinearInterpolator.html

If u download the package and open the LinearInterpolator.java u file, you can find the code. Package Download Link

http://sourceforge.net/projects/multigraph/files/multigraph/

0
source share

Example code taken here (linear only):

public static final double[] interpLinear(double[] x, double[] y, double[] xi) throws IllegalArgumentException { if (x.length != y.length) { throw new IllegalArgumentException("X and Y must be the same length"); } if (x.length == 1) { throw new IllegalArgumentException("X must contain more than one value"); } double[] dx = new double[x.length - 1]; double[] dy = new double[x.length - 1]; double[] slope = new double[x.length - 1]; double[] intercept = new double[x.length - 1]; // Calculate the line equation (ie slope and intercept) between each point for (int i = 0; i < x.length - 1; i++) { dx[i] = x[i + 1] - x[i]; if (dx[i] == 0) { throw new IllegalArgumentException("X must be montotonic. A duplicate " + "x-value was found"); } if (dx[i] < 0) { throw new IllegalArgumentException("X must be sorted"); } dy[i] = y[i + 1] - y[i]; slope[i] = dy[i] / dx[i]; intercept[i] = y[i] - x[i] * slope[i]; } // Perform the interpolation here double[] yi = new double[xi.length]; for (int i = 0; i < xi.length; i++) { if ((xi[i] > x[x.length - 1]) || (xi[i] < x[0])) { yi[i] = Double.NaN; } else { int loc = Arrays.binarySearch(x, xi[i]); if (loc < -1) { loc = -loc - 2; yi[i] = slope[loc] * xi[i] + intercept[loc]; } else { yi[i] = y[loc]; } } } return yi; } 
+1
source share

All Articles