我正在尝试使用SplineInterpolator 和PolynomialSplineFunction加倍数据集。我想我在路上相当远(我可能错过了一些异常处理):
SplineInterpolator splineInterp;
public double[] doubledArray(double[] y){
double[] yy = new double[y.length*2];
// make a double version of y w/ -1 for "null" values
for(int i = 0; i < yy.length; i++){
if(i%2 == 0)
yy[i] = y[i];
else if(i == yy.length-1)
yy[i] = yy[0];
else
yy[i] = -1;
}
// make a corresponding x array to satisfy SplineInterpolator.interpolate
double[] x = new double[y.length];
for(int i = 0; i < x.length; i++)
x[i] = i;
splineInterp = new SplineInterpolator();
PolynomialSplineFunction polySplineF = splineInterp.interpolate(x, y);
for(int i = 0; i < yy.length; i++){
if(yy[i] == -1){
yy[i] = polySplineF.value(i);
// breaks down halfway through polySplineF.value expects and array of y.length
}
}
return yy;
}
但是上面的内容最迟会在最后一个for循环中崩溃。那么,我的第一部分或多或少是正确的吗?拥有多项式样条函数后,如何使用它来创建更大的数据集?
答案 0 :(得分:2)
如果有人在家里跟随,这是我为此提出的实施:
private double[] multiplyArray(double[] y){
// An array 2 or 4 or N times bigger than the original:
double[] yy = new double[y.length*arrayMultiplier];
// An array representing the indices of the original:
double[] x = new double[y.length];
for(int i = 0; i < x.length; i++)
x[i] = i;
// Get and instance of SplineInterpolator:
SplineInterpolator splineInterp = new SplineInterpolator();
// Use that instance's interpolate() function to a PolynomialSplineFunction
// fitting your data, points y at indices x.
PolynomialSplineFunction polySplineF = splineInterp.interpolate(x, y);
// Use the PolynomialSplineFunction to fill in your larger array by supplying
// index values divided by the arrayMultiplier
for(int i = 0; i < yy.length; i++){
yy[i] = polySplineF.value((double)(i/arrayMultiplier));
}
return yy;
}
如果有人需要,我也想出了如何使用可能更有用的填空空白。