2

我正在尝试使用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 循环中崩溃。那么,我的第一部分或多或少是对的吗?拥有多项式样条函数后,如何使用它来创建更大的数据集?

4

1 回答 1

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

如果有人需要,我还想出了如何使用可能更有用的填充空白。

于 2012-12-09T21:39:44.010 回答