阻止价值波动,过滤掉峰值

时间:2016-08-08 09:54:20

标签: javascript math

我正在使用JavaScript中的物理学,我有一个值表示“物理”世界中的联系人数量。这个值可以在0到6之间。问题是我波动很多。我想要的是过滤掉尖峰。假设该数字为3,然后在几分之一秒内变为零,然后再次变为3。有关如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:3)

良好的拟合可以是滑动窗口中的中值滤波器。考虑最后的N个值,其中N至少是下降持续时间的两倍。然后你的过滤值是N的中位数。你可以永久重复N个样本移动一个位置。

由于您的值在很小的范围内是离散的,因此中值算法将非常简单。保留七个可能值的直方图,即七个计数器。每当您移动一个位置时,倒计时最旧值并计算新值。中值使得累积计数达到N / 2.

会有一定的惯性,即真正的变化需要一段时间才能得到承认,但这是不可避免的,因为任何算法都需要确认永久性变化而不是临时变化。

答案 1 :(得分:0)

感谢@ Yves-Daoust,他指出了中间滤波器的方向。

更多相关信息:https://www.youtube.com/watch?v=xFaddafLbcg

我为其他遇到同样问题的人开了一堂课。 看看:

MedianFilter = function (size) {

    var that = this;
    that.vales = [];
    that.sorted = [];
    that.size = size || 7;
    that.middelIndex = Math.round(that.size / 2);

};

MedianFilter.prototype = {};

MedianFilter.prototype.input = function (val) {

    var that = this;

    //If it's empty fill it up
    if (that.vales.length === 0) {
        that.fill(val);
        return val;
    }

    //Remove last
    that.vales.shift();
    //Add new value
    that.vales.push(val);

    //Sort
    that.sorted = that.vales.slice(0);
    that.sorted = that.sorted.sort(function (a, b) { return a - b; });
    //return medium value
    return that.sorted[that.middelIndex];

};

MedianFilter.prototype.fill = function (val) {

    var that = this;
    if (that.vales.length === 0) {
        for (var i = 0; i < that.size; i++) {
            that.vales.push(val);
        }
    }

};

//Usage

var toucheFix = new MedianFilter(10); //Number is size of array to get median value from, default 7
var val = toucheFix.input(touches); //Apply a median filter to this to make it more stable