Here you can find the source of medianFilter(double[] array, int window)
public static double[] medianFilter(double[] array, int window)
//package com.java2s; //License from project: Open Source License import java.util.Arrays; public class Main { public static double[] medianFilter(double[] array, int window) { int len = array.length; if (window > len) { return null; }/*from w w w .j ava 2 s . c o m*/ int mid = window / 2; double[] result = new double[len]; double[] subarray; for (int i = 0; i < len; i++) { // Use the part of the window centered on i that fits into the array if (i <= mid) { subarray = Arrays.copyOfRange(array, 0, i + mid + 1); } else if (i + mid >= len) { subarray = Arrays.copyOfRange(array, i - mid, len); } else { subarray = Arrays.copyOfRange(array, i - mid, i + mid + 1); } result[i] = median(subarray); } return result; } /** * Calculates the median of an array by sorting using QuickSort and taking * the middle element if the length of the array is odd and the average of * the two middle elements otherwise. Does not check for valid inputs. * * @param array * @return */ public static double median(double[] array) { int len = array.length; int mid = len / 2; quickSort(array, 0, len - 1); // Median is the midpoint of the sorted list if (len % 2 == 0) { return .5 * (array[mid - 1] + array[mid]); } else { return array[mid]; } } /** * Quicksort filter. Use low = 0 and high = length - 1 to sort the whole * array. From <a href= * "http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html" * >http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html<a> * * @param array The array to sort. * @param low Starting datasetIndex of the part of the array to sort. * @param low Ending datasetIndex of the part of the array to sort. */ public static void quickSort(double[] array, int low, int high) { int i = low, j = high; // Get the pivot element from the middle of the list double pivot = array[low + (high - low) / 2]; // Divide into two lists double exchange; while (i <= j) { // If the current value from the left list is smaller then the pivot // element then get the next element from the left list while (array[i] < pivot) { i++; } // If the current value from the right list is larger then the pivot // element then get the next element from the right list while (array[j] > pivot) { j--; } // If we have found a values in the left list which is larger then // the pivot element and if we have found a value in the right list // which is smaller then the pivot element then we exchange the // values. // As we are done we can increase i and j if (i <= j) { exchange = array[i]; array[i] = array[j]; array[j] = exchange; i++; j--; } } // Recursion if (low < j) quickSort(array, low, j); if (i < high) quickSort(array, i, high); } }