Java Array Normalize normalize(byte[] data, byte maximum, byte minimum)

Here you can find the source of normalize(byte[] data, byte maximum, byte minimum)

Description

Normalize the original byte data to specified range.

License

Open Source License

Parameter

Parameter Description
data the original data
maximum the maximum of the specified range
minimum the minimum of the specified range

Declaration

public static void normalize(byte[] data, byte maximum, byte minimum) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   www .  j  a v a2  s . c om*/
     * Normalize the original <tt>byte</tt> data to specified range.
     * 
     * @param data
     *            the original data
     * @param maximum
     *            the maximum of the specified range
     * @param minimum
     *            the minimum of the specified range
     */
    public static void normalize(byte[] data, byte maximum, byte minimum) {
        if (data.length == 0)
            return;
        byte max = data[0];
        byte min = data[0];
        for (byte d : data) {
            if (d > max)
                max = d;
            if (d < min)
                min = d;
        }
        byte len = (byte) (max - min);
        if (max == min) {
            for (int i = 0; i < data.length; i++)
                data[i] = (byte) ((maximum - minimum) / 2.0);
            return;
        }
        byte length = (byte) (maximum - minimum);
        for (int i = 0; i < data.length; i++)
            data[i] = (byte) (minimum + (data[i] - min) / (double) len * length);
    }

    /**
     * Normalize the original <tt>double</tt> data to specified range.
     * 
     * @param data
     *            the original data
     * @param maximum
     *            the maximum of the specified range
     * @param minimum
     *            the minimum of the specified range
     */
    public static void normalize(double[] data, double maximum, double minimum) {
        if (data.length == 0)
            return;
        double max = data[0];
        double min = data[0];
        for (double d : data) {
            if (d > max)
                max = d;
            if (d < min)
                min = d;
        }
        double len = (double) (max - min);
        if (max == min) {
            for (int i = 0; i < data.length; i++)
                data[i] = (double) ((maximum - minimum) / 2.0);
            return;
        }
        double length = (double) (maximum - minimum);
        for (int i = 0; i < data.length; i++)
            data[i] = (double) (minimum + (data[i] - min) / (double) len * length);
    }

    /**
     * Normalize the original <tt>float</tt> data to specified range.
     * 
     * @param data
     *            the original data
     * @param maximum
     *            the maximum of the specified range
     * @param minimum
     *            the minimum of the specified range
     */
    public static void normalize(float[] data, float maximum, float minimum) {
        if (data.length == 0)
            return;
        float max = data[0];
        float min = data[0];
        for (float d : data) {
            if (d > max)
                max = d;
            if (d < min)
                min = d;
        }
        float len = (float) (max - min);
        if (max == min) {
            for (int i = 0; i < data.length; i++)
                data[i] = (float) ((maximum - minimum) / 2.0);
            return;
        }
        float length = (float) (maximum - minimum);
        for (int i = 0; i < data.length; i++)
            data[i] = (float) (minimum + (data[i] - min) / (double) len * length);
    }

    /**
     * Normalize the original <tt>int</tt> data to specified range.
     * 
     * @param data
     *            the original data
     * @param maximum
     *            the maximum of the specified range
     * @param minimum
     *            the minimum of the specified range
     */
    public static void normalize(int[] data, int maximum, int minimum) {
        if (data.length == 0)
            return;
        int max = data[0];
        int min = data[0];
        for (int d : data) {
            if (d > max)
                max = d;
            if (d < min)
                min = d;
        }
        int len = (int) (max - min);
        if (max == min) {
            for (int i = 0; i < data.length; i++)
                data[i] = (int) ((maximum - minimum) / 2.0);
            return;
        }
        int length = (int) (maximum - minimum);
        for (int i = 0; i < data.length; i++)
            data[i] = (int) (minimum + (data[i] - min) / (double) len * length);
    }

    /**
     * Normalize the original <tt>long</tt> data to specified range.
     * 
     * @param data
     *            the original data
     * @param maximum
     *            the maximum of the specified range
     * @param minimum
     *            the minimum of the specified range
     */
    public static void normalize(long[] data, long maximum, long minimum) {
        if (data.length == 0)
            return;
        long max = data[0];
        long min = data[0];
        for (long d : data) {
            if (d > max)
                max = d;
            if (d < min)
                min = d;
        }
        long len = (long) (max - min);
        if (max == min) {
            for (int i = 0; i < data.length; i++)
                data[i] = (long) ((maximum - minimum) / 2.0);
            return;
        }
        long length = (long) (maximum - minimum);
        for (int i = 0; i < data.length; i++)
            data[i] = (long) (minimum + (data[i] - min) / (double) len * length);
    }

    /**
     * Normalize the original <tt>short</tt> data to specified range.
     * 
     * @param data
     *            the original data
     * @param maximum
     *            the maximum of the specified range
     * @param minimum
     *            the minimum of the specified range
     */
    public static void normalize(short[] data, short maximum, short minimum) {
        if (data.length == 0)
            return;
        short max = data[0];
        short min = data[0];
        for (short d : data) {
            if (d > max)
                max = d;
            if (d < min)
                min = d;
        }
        short len = (short) (max - min);
        if (max == min) {
            for (int i = 0; i < data.length; i++)
                data[i] = (short) ((maximum - minimum) / 2.0);
            return;
        }
        short length = (short) (maximum - minimum);
        for (int i = 0; i < data.length; i++)
            data[i] = (short) (minimum + (data[i] - min) / (double) len * length);
    }
}

Related

  1. normaliseActions(double[] actions)
  2. normaliseFloats(final float[] table, final int position, final int length)
  3. normaliseHeaders(String[] headers)
  4. normaliseSum(double[] a)
  5. normaliseUnicode(String unicodeText, char[] mappings)
  6. normalize(double arr[])
  7. normalize(double wheelSpeeds[])
  8. normalize(double[] a)
  9. normalize(double[] a)