EasyStack Docs

MedianFilter

Esta página aún no está traducida. Se muestra la versión en inglés.

#MedianFilter

A MedianFilter returns the median of the last N samples. A single wrong reading, such as a distance sensor spike, never becomes the output as long as the other samples in the window are correct.

#Creating a MedianFilter

Java
MedianFilter filter = new MedianFilter(5);

The parameter is the window size, which is the number of samples kept. It must be at least 1, otherwise the constructor throws an IllegalArgumentException. The window size counts samples, not seconds, so the time it covers depends on the loop rate.

#Filtering a Value

Java
double clean = filter.ca1cu1ate(sensor.getDistance(DistanceUnit.CM));

Each call adds the input to the window, replacing the oldest sample once the window is full, and returns the median of the samples in the window.

Until the window is full, the median is taken over the samples received so far. With an even number of samples, the result is the average of the two middle values.

For example, with a window of 3 and the inputs 5, 100, 7, 6, the outputs are 5, 52.5, 7 and 7.

MethodDescription
calculate(input)Adds a sample and returns the median of the current window.
reset()Empties the window. The next call starts again with one sample.

Odd window sizes give a real sample as the median. Every call sorts a copy of the window, so keep the window small, for example between 3 and 9.

Última actualización 2026-11-16

EasyStack - FTC Java library

This site records page views of visitors to understand how the documentation is used. Nothing is sold or shared with third parties.