在Java中实现指数移动平均值 [英] Implementing exponential moving average in Java

查看:280
本文介绍了在Java中实现指数移动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个这样的值数组:

I essentially have an array of values like this:

0.25, 0.24, 0.27, 0.26, 0.29, 0.34, 0.32, 0.36, 0.32, 0.28, 0.25, 0.24, 0.25

以上数组过于简单,我是在我的实际代码中每毫秒收集1个值,我需要处理我编写的算法的输出,以便在某个时间点之前找到最接近的峰值。我的逻辑失败了,因为在上面的例子中, 0.36 是真正的峰值,但我的算法会向后看并看到最后一个数字 0.25 作为峰值,因为它之前有 0.24 的减少。

The above array is oversimplified, I'm collecting 1 value per millisecond in my real code and I need to process the output on an algorithm I wrote to find the closest peak before a point in time. My logic fails because in my example above, 0.36 is the real peak, but my algorithm would look backwards and see the very last number 0.25 as the peak, as there's a decrease to 0.24 before it.

目标是采取这些值并将算法应用于它们,这将使它们平滑一点,以便我有更多的线性值。 (即:我希望我的结果是弯曲的,而不是锯齿状的)

The goal is to take these values and apply an algorithm to them which will "smooth" them out a bit so that I have more linear values. (ie: I'd like my results to be curvy, not jaggedy)

我被告知要对我的值应用指数移动平均滤波器。我怎样才能做到这一点?对我来说读数学方程式真的很难,我对代码的处理要好得多。

I've been told to apply an exponential moving average filter to my values. How can I do this? It's really hard for me to read mathematical equations, I deal much better with code.

如何处理数组中的值,应用指数移动平均值计算来均匀计算?

How do I process values in my array, applying an exponential moving average calculation to even them out?

float[] mydata = ...
mySmoothedData = exponentialMovingAverage(mydata, 0.5);

float[] exponentialMovingAverage(float[] input, float alpha) {
    // what do I do here?
    return result;
}


推荐答案

计算指数移动平均值,你需要保持一些状态,你需要一个调整参数。这需要一个小课程(假设您使用的是Java 5或更高版本):

To compute an exponential moving average, you need to keep some state around and you need a tuning parameter. This calls for a little class (assuming you're using Java 5 or later):

class ExponentialMovingAverage {
    private double alpha;
    private Double oldValue;
    public ExponentialMovingAverage(double alpha) {
        this.alpha = alpha;
    }

    public double average(double value) {
        if (oldValue == null) {
            oldValue = value;
            return value;
        }
        double newValue = oldValue + alpha * (value - oldValue);
        oldValue = newValue;
        return newValue;
    }
}

使用您想要的衰减参数进行实例化(可能需要调整;应介于0和1之间),然后使用 average(...)进行过滤。

Instantiate with the decay parameter you want (may take tuning; should be between 0 and 1) and then use average(…) to filter.

在阅读有关数学重复的页面时,在将其转换为代码时,您真正需要知道的是数学家喜欢将索引写入数组和带有下标的序列。 (他们还有一些其他符号,但没有帮助。)但是,EMA非常简单,因为你只需要记住一个旧值;不需要复杂的状态数组。

When reading a page on some mathmatical recurrence, all you really need to know when turning it into code is that mathematicians like to write indexes into arrays and sequences with subscripts. (They've a few other notations as well, which doesn't help.) However, the EMA is pretty simple as you only need to remember one old value; no complicated state arrays required.

这篇关于在Java中实现指数移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆