中值过滤器

A graph with two peaks with the input closely following the target signal.

A statistically robust alternative to the moving-average filter is the median filter. Where a moving average filter takes the arithmetic mean of the input over a moving sample window, a median filter (per the name) takes a median instead.

中值滤波器最有用的是从输入流中删除偶尔的异常值。这使其特别适合于过滤来自距离传感器的输入,这些输入容易受到偶尔的干扰。与移动平均值不同,中值过滤器将完全不受少量异常值的影响,无论多么极端。

The median filter is supported in WPILib through the MedianFilter class (Java, C++, , Python).

创建一个MedianFilter

备注

C ++``MedianFilter’’类是用于输入的数据类型的模板。

备注

因为过滤器具有“内存”,所以每个输入流都需要其自己的过滤器对象。 *不要*尝试对多个输入流使用相同的过滤器对象。

创建一个MedianFilter很简单:

// Creates a MedianFilter with a window size of 5 samples
MedianFilter filter = new MedianFilter(5);
// Creates a MedianFilter with a window size of 5 samples
frc::MedianFilter<double> filter(5);
from wpimath.filter import MedianFilter

# Creates a MedianFilter with a window size of 5 samples
filter = MedianFilter(5)

使用MedianFilter

创建过滤器后,使用起来很容易-只需使用最新输入调用calculate()方法即可获取过滤后的输出:

// Calculates the next value of the output
filter.calculate(input);
// Calculates the next value of the output
filter.Calculate(input);
# Calculates the next value of the output
filter.calculate(input)