Java Array Interpolate interpolate_linear(int[] x, double[] y, int[] xi)

Here you can find the source of interpolate_linear(int[] x, double[] y, int[] xi)

Description

interpolatlinear

License

Open Source License

Declaration

public static double[] interpolate_linear(int[] x, double[] y, int[] xi) 

Method Source Code

//package com.java2s;
/**// ww w  .  ja v  a 2s  . c o m
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * This file is part of MARY TTS.
 *
 * MARY TTS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    public static double[] interpolate_linear(int[] x, double[] y, int[] xi) {
        assert (x.length == y.length);

        double[] yi = new double[xi.length];
        int i, j;
        boolean bFound;
        double alpha;

        for (i = 0; i < xi.length; i++) {
            bFound = false;
            for (j = 0; j < x.length - 1; j++) {
                if (xi[i] >= x[j] && xi[i] < x[j + 1]) {
                    bFound = true;
                    break;
                }
            }

            if (bFound) {
                alpha = (((double) xi[i]) - x[j]) / (x[j + 1] - x[j]);
                yi[i] = (1 - alpha) * y[j] + alpha * y[j + 1];
            }
        }

        if (xi[xi.length - 1] == x[x.length - 1])
            yi[xi.length - 1] = y[x.length - 1];

        return yi;
    }
}

Related

  1. interpolate(double[] x, double D)
  2. interpolate(double[] X, double[] Y, double[] Z)
  3. interpolate(double[] x, int newLength)
  4. interpolate(float out[], float in1[], float in2[], int in2_idx, float coef, int length)
  5. interpolate(int[] data, double x)
  6. interpolateArray(int[] array, int interval)
  7. interpolateColor(float[] result, float[] color0, float[] color1, double s)
  8. interpolateLinear(int[] v1, int[] v2, double t)
  9. interpolateLinear(int[] vec, int start, int end)