我希望在 J2ME 或 JAVA 中实现 MATLAB 中可用的 interp1、一维数据插值(表查找)函数。链接在这里
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/interp1.html
J2ME 或 JAVA 中是否有任何可用的库已经实现了相同的功能?如果没有任何人可以帮助我在 J2ME 或 JAVA 中实现 interp1 功能吗?
我希望在 J2ME 或 JAVA 中实现 MATLAB 中可用的 interp1、一维数据插值(表查找)函数。链接在这里
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/interp1.html
J2ME 或 JAVA 中是否有任何可用的库已经实现了相同的功能?如果没有任何人可以帮助我在 J2ME 或 JAVA 中实现 interp1 功能吗?
取自此处的示例代码(仅线性):
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;
}
如果在 interp1 函数的语法中为方法参数选择了“linear”,我刚刚发现了用于线性插值的方法,即:interp1(x,y,xi,'linear')。这是在 multigraph 包中的 LinearInterpolator 类的方法 interp(double x) 中实现的。链接在下面
http://multigraph.sourceforge.net/multigraph/javadoc/multigraph/LinearInterpolator.html
如果你下载包并打开文件 LinearInterpolator.java 你可以找到代码。下载包的链接是
http://sourceforge.net/projects/multigraph/files/multigraph/