implementation of interp1 function of MATLAB in J2ME
Asked Answered
L

2

0

i am looking to implement the interp1, 1-D data interpolation (table lookup), 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 available in J2ME or JAVA which has already implemented the same function ? If not can anybody help me in implementing interp1 function in J2ME or JAVA ?

Latona answered 7/6, 2010 at 1:41 Comment(4)
It is very easy to implement linear interpolation in Java. Just a loop + normalizing + weighted sumHoes
If you have access to Matlab you'll find an m-file for interp1. Studying it will give you some clues about writing a Java version -- but don't simpy re-implement the Matlab in Java, that would breach laws relating to intellecutal property rights.Pettifer
Thanks Mikhail ! Can you give me a bit more detail of how will do linear interpolation as my math is not that strong ?Latona
Thanks High Performance Mark ! i do not have MATLAB installed. How can i access the interp1.m file ?Latona
L
0

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

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

if u download the package and open the file LinearInterpolator.java u can find the code. The link to download the package is

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

Latona answered 8/6, 2010 at 2:11 Comment(0)
M
2

Example code taken from 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 (i.e. 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;
    }
Misconstrue answered 9/1, 2012 at 10:17 Comment(0)
L
0

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

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

if u download the package and open the file LinearInterpolator.java u can find the code. The link to download the package is

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

Latona answered 8/6, 2010 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.