在J2ME中实现MATLAB的interp1功能

时间:2010-06-07 01:41:35

标签: java matlab java-me interpolation linear-interpolation

我希望实现interp1,1-D数据插值(表查找),在J2ME或JAVA中的MATLAB中可用。这是链接

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

J2ME或JAVA中是否有可用的库已经实现了相同的功能?如果没有,任何人都可以帮助我在J2ME或JAVA中实现interp1功能吗?

2 个答案:

答案 0 :(得分:1)

示例code taken from here(仅限线性):

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;
    }

答案 1 :(得分:0)

我刚刚发现了用于线性插值的方法,如果在interp1函数的语法中为方法参数选择了'linear',那就是:interp1(x,y,xi,'linear')。这是在类图LinearInterpolator的interp(double x)方法中实现的,它存在于多图包中。链接在

下面

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

如果您下载该软件包并打开文件LinearInterpolator.java,您可以找到该代码。下载软件包的链接是

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