Java Gauss gaussianFilter(float[] weights, float sigma)

Here you can find the source of gaussianFilter(float[] weights, float sigma)

Description

Gaussian smoothing of the elements of the array "weights"

License

Open Source License

Parameter

Parameter Description
weights the array
sigma the standard deviation

Return

the smoothed array

Declaration

public static float[] gaussianFilter(float[] weights, float sigma) 

Method Source Code

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

public class Main {
    /**
     * Gaussian smoothing of the elements of the array "weights"
     * @param weights the array
     * @param sigma the standard deviation
     * @return the smoothed array
     */
    public static float[] gaussianFilter(float[] weights, float sigma) {
        float meanFiltered[] = new float[weights.length];
        int center = (int) Math.floor(sigma * 1.5) + 1;
        float kernel[] = new float[(int) Math.ceil(center * 2 + 1)];
        float kernelSum = 0;
        for (int j = 0; j < (center * 2) + 1; j++) {
            kernel[j] = (float) (Math.exp(-0.5 * Math.pow((center - j) / sigma, 2)) / sigma
                    / Math.sqrt(2 * Math.PI));
            kernelSum += kernel[j];
        }
        for (int i = 0; i < meanFiltered.length; i++) {
            float sum = 0;
            for (int j = -center; j <= center; j++) {
                // Out of bounds at left side
                if (i + j < 0)
                    sum += kernel[j + center] * weights[0];
                // Out of bounds at right side
                else if (i + j > weights.length - 1)
                    sum += kernel[j + center] * weights[weights.length - 1];
                // Convolution applied inside the valid part of the signal
                else
                    sum += kernel[j + center] * weights[i + j];
            }
            meanFiltered[i] = sum / kernelSum;
        }
        return meanFiltered;
    }

    /**
     * Uses Math.exp() on all elements of the array
     * Works in place and overwrites array.
     * @param array the array
     */
    public static void exp(float[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = (float) Math.exp(array[i]);
        }
    }

    /**
     * calls Math.pow for each element of the array
     * @param array
     * @param exp the exponent.
     * @return reference to the input array
     */
    public static float[] pow(float[] array, float exp) {
        for (int i = 0; i < array.length; i++) {
            array[i] = (float) Math.pow((double) array[i], exp);
        }
        return array;
    }
}

Related

  1. gaussian(float x, float mean, float sd)
  2. gaussian(int size, double sigma)
  3. gaussianBlur(int[][] pixels, float sigma)
  4. gaussianDensity(double x, double mean, double standardDeviation)
  5. gaussianDerivative(double x)
  6. gaussianIntegral(double x)
  7. gaussianPDF(double mean, double variance, double x)
  8. gaussianPDF(double mu, double sigma, double x)
  9. gaussianWindow(double mean1, double mean2, double std)