在不同时间采样的指数移动平均线 [英] Exponential Moving Average Sampled at Varying Times

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

问题描述

我有一个连续值,我想为其计算一个指数移动平均值.通常,我只使用标准公式:

I have a continuous value for which I'd like to calculate an exponential moving average. Normally I'd just use the standard formula for this:

  • S n =α Y +(1-α)S n-1
  • Sn = αY + (1-α)Sn-1

其中S n 是新的平均值,α是alpha,Y是样本,S n-1 是先前的平均值.

where Sn is the new average, α is the alpha, Y is the sample, and Sn-1 is the previous average.

不幸的是,由于各种问题,我没有一致的采样时间.我可能知道我最多可以每毫秒采样一次,但是由于无法控制的因素,我可能一次不能采样几毫秒.但是,一种可能更常见的情况是,我提前或迟进行了简单采样:而不是在0、1和2 ms采样.我在0、0.9和2.1毫秒采样.我确实希望无论延迟如何,我的采样频率都将远远超过奈奎斯特极限,因此我不必担心混叠.

Unfortunately, due to various issues I don't have a consistent sample time. I may know I can sample at the most, say, once per millisecond, but due to factors out of my control, I may not be able to take a sample for several milliseconds at a time. A likely more common case, however, is that I simple sample a bit early or late: instead of sampling at 0, 1 and 2 ms. I sample at 0, 0.9 and 2.1 ms. I do anticipate that, regardless of delays, my sampling frequency will be far, far above the Nyquist limit, and thus I need not worry about aliasing.

我认为,根据自上次采样以来的时间长度,通过适当地更改alpha值,可以以一种或多或少的合理方式来处理此问题.

I reckon that I can deal with this in a more-or-less reasonable way by varying the alpha appropriately, based on the length of time since the last sample.

我认为这行得通的部分原因是EMA在先前数据点和当前数据点之间线性插值".如果我们考虑以时间间隔t计算以下样本列表的EMA:[0,1,2,3,4].如果使用间隔2t,输入应该是[0,2,4],我们应该得到相同的结果,对吗?如果EMA假设在t 2 处,该值自t 0 起为2,则该值与基于[0,2, 2,4,4],它没有执行.还是完全有道理?

Part of my reasoning that this will work is that the EMA "interpolates linearly" between the previous data point and the current one. If we consider calculating an EMA of the following list of samples at intervals t: [0,1,2,3,4]. We should get the same result if we use interval 2t, where the inputs become [0,2,4], right? If the EMA had assumed that, at t2 the value had been 2 since t0, that would be the same as the interval t calculation calculating on [0,2,2,4,4], which it's not doing. Or does that make sense at all?

有人可以告诉我如何适当更改Alpha吗? 请展示你的作品."也就是说,请向我展示数学,证明您的方法确实在做正确的事情.

Can someone tell me how to vary the alpha appropriately? "Please show your work." I.e., show me the math that proves that your method really is doing the right thing.

推荐答案

此答案基于我对低通滤波器的很好理解(指数移动平均"实际上只是一个单极点低通滤波器),但我的想法很模糊您正在寻找的东西.我认为以下是您想要的:

This answer based on my good understanding of low-pass filters ("exponential moving average" is really just a single-pole lowpass filter), but my hazy understanding of what you're looking for. I think the following is what you want:

首先,您可以稍微简化方程式(看起来更复杂,但是在代码中更容易).我将使用"Y"作为输出,使用"X"作为输入(代替S的输出和Y的输入,就像您所做的那样).

First, you can simplify your equation a little bit (looks more complicated but it's easier in code). I'm going to use "Y" for output and "X" for input (instead of S for output and Y for input, as you have done).

Y n =α X +(1-α)Y n-1 → Y n = Y n-1 +α(X-Y n-1 )

Yn = αX + (1-α)Yn-1 → Yn = Yn-1 + α(X - Yn-1)

编码为:

 Y += alpha * (X-Y);

第二,α的值这里是等于" 1-e -Δt/τ ,其中< t是样本之间的时间,τ是低通滤波器的时间常数.我用引号说等于"是因为当< t/τ与1相比较小,而α = 1-e -Δ t/τ ≈ < t/τ. (但不要太小:您将遇到量化问题的麻烦,除非您诉诸某些外来技术,否则通常在状态变量S中需要额外的N位分辨率,其中N = -log 2 (&);)对于较大的< t/τ值;过滤效果开始消失,直到达到α接近1,基本上就是将输入分配给输出.

Second, the value of α here is "equal" to 1-e-Δt/τ where Δt is the time between samples, and τ is the time constant of the low-pass filter. I say "equal" in quotes because this works well when Δt/τ is small compared to 1, and α = 1-e-Δt/τ ≈ Δt/τ. (But not too small: you'll run into quantizing issues, and unless you resort to some exotic techniques you usually need an extra N bits of resolution in your state variable S, where N = -log2(α). ) For larger values of Δt/τ the filtering effect starts to disappear, until you get to the point where α is close to 1 and you're basically just assigning the input to the output.

这应该在变化的< t值下正常工作(只要alpha很小,< t的变化就不是很重要,否则您会遇到一些相当奇怪的Nyquist问题/混叠等),并且如果您正在处理乘法比除法便宜,或者定点问题很重要的处理器,请预先计算ω = 1/&tau ;,并考虑尝试近似α的公式.

This should work properly with varying values of Δt (the variation of Δt is not very important as long as alpha is small, otherwise you will run into some rather weird Nyquist issues / aliasing / etc.), and if you are working on a processor where multiplication is cheaper than division, or fixed-point issues are important, precalculate ω = 1/τ, and consider trying to approximate the formula for α.

如果您真的想知道如何推导公式

If you really want to know how to derive the formula

α = 1-e -&t; t/τ

α = 1-e-Δt/τ

然后考虑其微分方程的来源:

then consider its differential equation source:

Y +τ dY/dt = X

Y + τ dY/dt = X

,当X是单位步长函数时,

的解Y = 1-e -t/tau; .对于较小的Δt值,可以将导数近似为ΔY/Δt,从而得出

which, when X is a unit step function, has the solution Y = 1 - e-t/τ. For small values of Δt, the derivative can be approximated by ΔY/Δt, yielding

Y +τ < Y/&t; t = X

Y + τ ΔY/Δt = X

ΔY/Δt=(X-Y)/tau;

ΔY/Δt = (X-Y)/τ

ΔY=(X-Y)(Δt/tau;)=α(X-Y)

ΔY = (X-Y)(Δt/τ) = α(X-Y)

和α的外推" = 1-e -Δ t/τ 来自试图将行为与单位阶跃函数大小写匹配.

and the "extrapolation" of α = 1-e-Δt/τ comes from trying to match up the behavior with the unit step function case.

这篇关于在不同时间采样的指数移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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