Java Array Interpolate interpolate(double[] x, int newLength)

Here you can find the source of interpolate(double[] x, int newLength)

Description

interpolate

License

Open Source License

Declaration

public static double[] interpolate(double[] x, int newLength) 

Method Source Code

//package com.java2s;
/**/*from w  ww.  j a  v a2 s .  c om*/
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */

public class Main {
    public static double[] interpolate(double[] x, int newLength) {
        double[] y = null;
        if (newLength > 0) {
            int N = x.length;
            if (N == 1) {
                y = new double[1];
                y[0] = x[0];
                return y;
            } else if (newLength == 1) {
                y = new double[1];
                int ind = (int) Math.floor(N * 0.5 + 0.5);
                ind = Math.max(1, ind);
                ind = Math.min(ind, N);
                y[0] = x[ind - 1];
                return y;
            } else {
                y = new double[newLength];
                double Beta = ((double) newLength) / N;
                double newBeta = 1.0;

                if (newLength > 2)
                    newBeta = (N - 2.0) / (newLength - 2.0);

                y[0] = x[0];
                y[1] = x[1];
                y[newLength - 1] = x[N - 1];

                double tmp, alpha;
                int i, j;
                for (i = 2; i <= newLength - 2; i++) {
                    tmp = 1.0 + (i - 1) * newBeta;
                    j = (int) Math.floor(tmp);
                    alpha = tmp - j;
                    y[i] = (1.0 - alpha) * x[Math.max(0, j)] + alpha * x[Math.min(N - 1, j + 1)];
                }
            }
        }

        return y;
    }

    /**
     * Find the maximum of all elements in the array, ignoring elements that are NaN.
     * @param data
     * @return
     */
    public static double max(double[] data) {
        double max = Double.NaN;
        for (int i = 0; i < data.length; i++) {
            if (Double.isNaN(data[i]))
                continue;
            if (Double.isNaN(max) || data[i] > max)
                max = data[i];
        }
        return max;
    }

    public static int max(int[] data) {
        int max = data[0];
        for (int i = 1; i < data.length; i++) {
            if (data[i] > max)
                max = data[i];
        }
        return max;
    }

    /**
     * Find the minimum of all elements in the array, ignoring elements that are NaN.
     * @param data
     * @return
     */
    public static double min(double[] data) {
        double min = Double.NaN;
        for (int i = 0; i < data.length; i++) {
            if (Double.isNaN(data[i]))
                continue;
            if (Double.isNaN(min) || data[i] < min)
                min = data[i];
        }
        return min;
    }

    public static int min(int[] data) {
        int min = data[0];
        for (int i = 1; i < data.length; i++) {
            if (data[i] < min)
                min = data[i];
        }
        return min;
    }
}

Related

  1. interpolate(double[] array, int[] translation, double index)
  2. interpolate(double[] end0, double[] end1, double[] mid)
  3. interpolate(double[] points, double[] values, double interpolateAt)
  4. interpolate(double[] x, double D)
  5. interpolate(double[] X, double[] Y, double[] Z)
  6. interpolate(float out[], float in1[], float in2[], int in2_idx, float coef, int length)
  7. interpolate(int[] data, double x)
  8. interpolate_linear(int[] x, double[] y, int[] xi)
  9. interpolateArray(int[] array, int interval)