Normalizes the input array to double values between -1.0 and 1.0. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Normalizes the input array to double values between -1.0 and 1.0.

Demo Code


//package com.java2s;

public class Main {
    /**//from  w  w w  .j  a  v a  2  s. c  o  m
     * Normalizes the input array to double values between -1.0 and 1.0.
     * @param input
     * @return
     */
    public static double[] normalize(short[] input) {
        double[] normalizedSamples = new double[input.length];

        int max = maxValue(input);

        for (int i = 0; i < input.length; i++) {
            normalizedSamples[i] = ((double) input[i]) / max;
        }

        return normalizedSamples;
    }

    /**
     * Normalizes the input array to double values between -1.0 and 1.0.
     * @param input
     * @return
     */
    public static double[] normalize(double[] input) {
        double[] normalizedSamples = new double[input.length];

        double max = maxValue(input);

        for (int i = 0; i < input.length; i++) {
            normalizedSamples[i] = input[i] / max;
        }

        return normalizedSamples;
    }

    /**
     * Calculates max absolute value.
     * @param input
     * @return
     */
    public static short maxValue(short[] input) {
        short max = Short.MIN_VALUE;
        for (int i = 0; i < input.length; i++) {
            if (Math.abs(input[i]) > max) {
                max = (short) Math.abs(input[i]);
            }
        }
        return max;
    }

    /**
     * Calculates max absolute value.
     * @param input
     * @return
     */
    public static double maxValue(double[] input) {
        double max = Double.MIN_VALUE;
        for (int i = 0; i < input.length; i++) {
            if (Math.abs(input[i]) > max) {
                max = Math.abs(input[i]);
            }
        }
        return max;
    }
}

Related Tutorials