Here you can find the source of arrayResize(double[] source, int targetSize)
Parameter | Description |
---|---|
source | source |
targetSize | targetSize |
public static double[] arrayResize(double[] source, int targetSize)
//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; } }