Java mean meanFilter(float[] weights, int context)

Here you can find the source of meanFilter(float[] weights, int context)

Description

Performs mean filtering of the array.

License

Open Source License

Parameter

Parameter Description
weights the array
context the context to be used for smoothing (from -context/2 to context/2)

Return

the smoothed array

Declaration

public static float[] meanFilter(float[] weights, int context) 

Method Source Code

//package com.java2s;
/*/* w  ww. j a va 2  s. c  o m*/
 * Copyright (C) 2010-2014  Andreas Maier
 * CONRAD is developed as an Open Source project under the GNU General Public License (GPL).
*/

public class Main {
    /**
     * Performs mean filtering of the array.
     * @param weights the array
     * @param context the context to be used for smoothing (from -context/2 to context/2)
     * @return the smoothed array
     */
    public static float[] meanFilter(float[] weights, int context) {
        float meanFiltered[] = new float[weights.length];
        float mean = 0;
        for (int i = 0; i < weights.length; i++) {
            if (i > context / 2) {
                mean -= weights[i - (context / 2)];
            }
            if (i < (weights.length - 1) - (context / 2)) {
                mean += weights[i + (context / 2)];
            }
            if (i < context / 2) {
                meanFiltered[i] = computeMean(weights, 0, i);
            } else if ((i + context / 2) >= weights.length) {
                meanFiltered[i] = computeMean(weights, i, weights.length - 1);
            } else {
                meanFiltered[i] = computeMean(weights, i - context / 2, i + context / 2);
            }
        }
        return meanFiltered;
    }

    /**
     * computes the mean of the array "values" on the interval [start, end].
     * @param values the array
     * @param start the start index
     * @param end the end index
     * @return the mean value
     */
    public static float computeMean(float[] values, int start, int end) {
        float revan = 0;
        for (int i = start; i <= end; i++) {
            revan += values[i];
        }
        revan /= end - start + 1;
        return revan;
    }

    /**
     * Computes the mean value of a given array
     * @param array the array
     * @return the mean value as float
     */
    public static float computeMean(float[] array) {
        float mean = 0;
        for (int i = 0; i < array.length; i++) {
            mean += array[i];
        }
        return mean / array.length;
    }
}

Related

  1. meanArithmetic(LinkedList a)
  2. meanArray(double[] arr)
  3. meandiff(double[] v1, double[] v2)
  4. meanEnt(double[] nums)
  5. meanFast(final double[] values)
  6. meanGreenwichSideralTime(double t)
  7. meanImage(float[][]... images)
  8. meanLow(final int a, final int b)
  9. means(double[][] input)