Pads the data float array to the specified number of data points. - Java java.lang

Java examples for java.lang:int Array

Description

Pads the data float array to the specified number of data points.

Demo Code

/*//from w  w  w. j  a  v  a 2s . com
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main{
    /**
     * Pads the data to the specified number of data points.
     *
     * @param data   the data to pad
     * @param numPoints   the number of data points for the result
     * @return      the padded data
     */
    public static float[] pad(float[] data, int numPoints, PaddingType type) {
        float[] result;
        int i;

        if (numPoints < data.length)
            throw new IllegalArgumentException(
                    "Number of output data points is smaller than input data points: "
                            + numPoints + " < " + data.length);

        result = new float[numPoints];
        System.arraycopy(data, 0, result, 0, data.length);

        switch (type) {
        case ZERO:
            for (i = data.length; i < numPoints; i++)
                result[i] = 0;
            break;
        case LAST:
            for (i = data.length; i < numPoints; i++)
                result[i] = result[data.length - 1];
            break;
        default:
            throw new IllegalStateException("Padding " + type
                    + " not implemented!");
        }

        return result;
    }
    /**
     * Pads the data to the specified number of data points.
     *
     * @param data   the data to pad
     * @param numPoints   the number of data points for the result
     * @return      the padded data
     */
    public static double[] pad(double[] data, int numPoints,
            PaddingType type) {
        double[] result;
        int i;

        if (numPoints < data.length)
            throw new IllegalArgumentException(
                    "Number of output data points is smaller than input data points: "
                            + numPoints + " < " + data.length);

        result = new double[numPoints];
        System.arraycopy(data, 0, result, 0, data.length);

        switch (type) {
        case ZERO:
            for (i = data.length; i < numPoints; i++)
                result[i] = 0;
            break;
        case LAST:
            for (i = data.length; i < numPoints; i++)
                result[i] = result[data.length - 1];
            break;
        default:
            throw new IllegalStateException("Padding " + type
                    + " not implemented!");
        }

        return result;
    }
}

Related Tutorials