/** * {@inheritDoc} */ public double value(double x, double y) throws OutOfRangeException { final AkimaSplineInterpolator interpolator = new AkimaSplineInterpolator(); final int offset = 2; final int count = offset + 3; final int i = searchIndex(x, xval, offset, count); final int j = searchIndex(y, yval, offset, count); final double xArray[] = new double[count]; final double yArray[] = new double[count]; final double zArray[] = new double[count]; final double interpArray[] = new double[count]; for (int index = 0; index < count; index++) { xArray[index] = xval[i + index]; yArray[index] = yval[j + index]; } for (int zIndex = 0; zIndex < count; zIndex++) { for (int index = 0; index < count; index++) { zArray[index] = fval[i + index][j + zIndex]; } final PolynomialSplineFunction spline = interpolator.interpolate(xArray, zArray); interpArray[zIndex] = spline.value(x); } final PolynomialSplineFunction spline = interpolator.interpolate(yArray, interpArray); double returnValue = spline.value(y); return returnValue; }
public double predict(double value) { return spline.value(value); } }
public double[] linearInterp(double[] x, double[] y, double[] xi) { LinearInterpolator li = new LinearInterpolator(); // or other interpolator PolynomialSplineFunction psf = li.interpolate(x, y); double[] yi = new double[xi.length]; for (int i = 0; i < xi.length; i++) { yi[i] = psf.value(xi[i]); } return yi; }
private double valueOrZero (final PolynomialSplineFunction psf, final double freq) { return psf.isValidPoint (freq) ? psf.value (freq) : 0; } }
public class Interpolate { public static void main(String[] args) { double[] x = { 0, 50, 100 }; double[] y = { 0, 50, 200 }; LinearInterpolator interp = new LinearInterpolator(); PolynomialSplineFunction f = interp.interpolate(x, y); System.out.println("Piecewise functions:"); Arrays.stream(f.getPolynomials()).forEach(System.out::println); double value = f.value(70); System.out.println("y for xi = 70: " + value); } }
private static double[] interpolate(int[] interpolatedX, double[] xCoordinates, double[] yCoordinates) { double[] interpolatedY = new double[interpolatedX.length]; LinearInterpolator li = new LinearInterpolator(); PolynomialSplineFunction psf = li.interpolate(xCoordinates, yCoordinates); for (int i = 0; i <= interpolatedX.length; ++i) { interpolatedY[0] = psf.value(interpolatedX[i]); } return interpolatedY; }
SplineInterpolator interpolator = new SplineInterpolator(); PolynomialSplineFunction f = interpolator.interpolate(xValues, yValues); //red and blue dots f.value(4.0); //gets y-value for x = 4.0
/** * Creates a double array containing y coordinates of a {@linkplain PolynomialSplineFunction} with the above specified * {@code DETAIL_LEVEL}. * * @param fuction The {@linkplain PolynomialSplineFunction} with the values * @param index the parameterized array to indicate the maximum Values * @return an array of double-precision y values of the specified function */ protected double[] getCoords(final PolynomialSplineFunction fuction, final double[] index) { final double defaultCoordSize = index[index.length - 1]; final double[] coords = new double[(int) Math.round(DETAIL_LEVEL * defaultCoordSize) + 1]; final double stepSize = fuction.getKnots()[fuction.getKnots().length - 1] / coords.length; double curValue = 0; for (int i = 0; i < coords.length; i++) { coords[i] = fuction.value(curValue); curValue += stepSize; } return coords; }
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; }
@Override public Object doWork(Object... objects) throws IOException{ Object first = objects[0]; double[] x = null; double[] y = null; if(objects.length == 1) { //Only the y values passed y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); x = new double[y.length]; for(int i=0; i<y.length; i++) { x[i] = i; } } else if(objects.length == 2) { Object second = objects[1]; x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); } AkimaSplineInterpolator interpolator = new AkimaSplineInterpolator(); PolynomialSplineFunction spline = interpolator.interpolate(x, y); List<Number> list = new ArrayList(); for(double xvalue : x) { list.add(spline.value(xvalue)); } VectorFunction vec = new VectorFunction(spline, list); vec.addToContext("x", x); vec.addToContext("y", y); return vec; }
@Override public Object doWork(Object... objects) throws IOException{ Object first = objects[0]; double[] x = null; double[] y = null; if(objects.length == 1) { //Only the y values passed y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); x = new double[y.length]; for(int i=0; i<y.length; i++) { x[i] = i; } } else if(objects.length == 2) { Object second = objects[1]; x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); } LinearInterpolator interpolator = new LinearInterpolator(); PolynomialSplineFunction spline = interpolator.interpolate(x, y); List<Number> list = new ArrayList(); for(double xvalue : x) { list.add(spline.value(xvalue)); } VectorFunction vec = new VectorFunction(spline, list); vec.addToContext("x", x); vec.addToContext("y", y); return vec; }
@Override public Object doWork(Object... objects) throws IOException{ Object first = objects[0]; double[] x = null; double[] y = null; if(objects.length == 1) { //Only the y values passed y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); x = new double[y.length]; for(int i=0; i<y.length; i++) { x[i] = i; } } else if(objects.length == 2) { Object second = objects[1]; x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(); } SplineInterpolator interpolator = new SplineInterpolator(); PolynomialSplineFunction spline = interpolator.interpolate(x, y); List<Number> list = new ArrayList(); for(double xvalue : x) { list.add(spline.value(xvalue)); } VectorFunction vec = new VectorFunction(spline, list); vec.addToContext("x", x); vec.addToContext("y", y); return vec; }
public double value(double x0) { if (splineFunction != null && splineFunction.isValidPoint(x0)) { return splineFunction.value(x0); } if (x0 <= start.getX()) { return start.getY(); } if (x0 >= end.getX()) { return end.getY(); } throw new IllegalStateException("should not reach undefined function range=" + x0); }
/** * {@inheritDoc} */ public double value(double x, double y) throws OutOfRangeException { final AkimaSplineInterpolator interpolator = new AkimaSplineInterpolator(); final int offset = 2; final int count = offset + 3; final int i = searchIndex(x, xval, offset, count); final int j = searchIndex(y, yval, offset, count); final double xArray[] = new double[count]; final double yArray[] = new double[count]; final double zArray[] = new double[count]; final double interpArray[] = new double[count]; for (int index = 0; index < count; index++) { xArray[index] = xval[i + index]; yArray[index] = yval[j + index]; } for (int zIndex = 0; zIndex < count; zIndex++) { for (int index = 0; index < count; index++) { zArray[index] = fval[i + index][j + zIndex]; } final PolynomialSplineFunction spline = interpolator.interpolate(xArray, zArray); interpArray[zIndex] = spline.value(x); } final PolynomialSplineFunction spline = interpolator.interpolate(yArray, interpArray); double returnValue = spline.value(y); return returnValue; }
/** * {@inheritDoc} */ public double value(double x, double y) throws OutOfRangeException { final AkimaSplineInterpolator interpolator = new AkimaSplineInterpolator(); final int offset = 2; final int count = offset + 3; final int i = searchIndex(x, xval, offset, count); final int j = searchIndex(y, yval, offset, count); final double xArray[] = new double[count]; final double yArray[] = new double[count]; final double zArray[] = new double[count]; final double interpArray[] = new double[count]; for (int index = 0; index < count; index++) { xArray[index] = xval[i + index]; yArray[index] = yval[j + index]; } for (int zIndex = 0; zIndex < count; zIndex++) { for (int index = 0; index < count; index++) { zArray[index] = fval[i + index][j + zIndex]; } final PolynomialSplineFunction spline = interpolator.interpolate(xArray, zArray); interpArray[zIndex] = spline.value(x); } final PolynomialSplineFunction spline = interpolator.interpolate(yArray, interpArray); double returnValue = spline.value(y); return returnValue; }
@Override public double normalize(double x) { double sMin = sample.get(0); double sMax = sample.get(sample.size() - 1); double halfLife = (sMax - sMin) / 4.0; double yDelta = 1.0 / (sample.size() + 1); double y; if (x < sMin) { y = WbMathUtils.toAsymptote(sMin - x, halfLife, yDelta, 0.0); } else if (x > sMax) { y = WbMathUtils.toAsymptote(x - sMax, halfLife, 1.0 - yDelta, 1.0); } else { y = interpolator.value(x); } if (power > 0.0) { y = FastMath.pow(y, power); } return y; }
@Override public Channel transform (final Channel input) { final SplineInterpolator reg = new SplineInterpolator (); final double [] x = new double [input.getSamplesLength () / this.step]; final double [] y = new double [input.getSamplesLength () / this.step]; for (int i = 0 ; i < input.getSamplesLength () ; i += this.step) { if (i / this.step < x.length) { x [i / this.step] = i; y [i / this.step] = input.getSampleAt (i); } } final PolynomialSplineFunction psf = reg.interpolate (x, y); final Channel outputSound = new Channel (new long [input.getSamplesLength ()], input.getFormatInfo (), input.getChannelNum ()); for (int i = 0 ; i < input.getSamplesLength () ; i++) { if (i < x [x.length - 1]) { outputSound.setSampleAt (i, (long) psf.value (i)); } else { outputSound.setSampleAt (i, input.getSampleAt (i)); } } return outputSound; }