Java Array Resize arrayResize(double[] source, int targetSize)

Here you can find the source of arrayResize(double[] source, int targetSize)

Description

array resize to target size using linear interpolation

License

Open Source License

Parameter

Parameter Description
source source
targetSize targetSize

Return

source if source.length == targetSize, newSignal otherwise

Declaration

public static double[] arrayResize(double[] source, int targetSize) 

Method Source Code

//package com.java2s;
/**//from w w w .  j a v a  2s .c om
 * 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 {
    /**
     * array resize to target size using linear interpolation
     * 
     * @param source
     *            source
     * @param targetSize
     *            targetSize
     * @return source if source.length == targetSize, newSignal otherwise
     */
    public static double[] arrayResize(double[] source, int targetSize) {

        if (source.length == targetSize) {
            return source;
        }

        int sourceSize = source.length;
        double fraction = (double) source.length / (double) targetSize;
        double[] newSignal = new double[targetSize];

        for (int i = 0; i < targetSize; i++) {
            double posIdx = fraction * i;
            int nVal = (int) Math.floor(posIdx);
            double diffVal = posIdx - nVal;

            if (nVal >= sourceSize - 1) {
                newSignal[i] = source[sourceSize - 1];
                continue;
            }
            // Linear Interpolation
            // newSignal[i] = (diffVal * samples[nVal+1]) + ((1 - diffVal) * samples[nVal]);
            // System.err.println("i "+i+" fraction "+fraction+" posIdx "+posIdx+" nVal "+nVal+" diffVal "+diffVal+" !!");
            double fVal = (diffVal * source[nVal + 1]) + ((1 - diffVal) * source[nVal]);
            newSignal[i] = fVal;
        }
        return newSignal;
    }
}

Related

  1. prepend(String firstElement, String[] remaining)
  2. prepend(T element, T[] array)
  3. prependArg(String args[], String newArg)
  4. prependZeros(int n, byte[] message)