![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/318456/image.png)
Exponential moving average doesn't seem to work properly
45 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a question about the moving average function:
ma = movavg(Data,type,windowSize)
In the following example:
movavg([1;2;3;4;5;6;7;8],'exponential',3)
gives 7.0078 as the last moving average.
Removing the first two numbers:
movavg([3;4;5;6;7;8],'exponential',3)
gives 7.0313 as the last moving average.
I was expecting the answer to be identical between the two examples since I specified to look at only the current and preceding 3 data. The answer is identical for simple moving average. See below:
windowSize — Number of observations of the input series to include in moving average
positive integer
How do I calculate an exponential to only use exclusive the the data in the lookback window? Using a loop takes too long.
0 个评论
回答(2 个)
Ashwin
2020-6-18
The general equation of the Exponential Moving Average is given as follows:
EMA = (Current value x Multiplier) + (Prev. EMA x (1-Multiplier))
Where Multiplier = (2/(windowsize+1))
Thus, when we apply the movavg function to the two set of values, we naturally see a discrepancy in final averages, because the first EMAs calculated are not the same.
In the first case, the first actual EMA value is calculated at the third value, ie, 3, thus making the EMA = 2.25 as compared to the 3 in the other distribution (because of the lack of previous values)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/318456/image.png)
Thus, this is not a mistake of the function, and the same function can be used to calculate the moving averages that you require to.
0 个评论
Image Analyst
2020-6-18
If you want a filter that looks just at the numbers within the filter window ONLY, and not be affected by numbers prior to the window location, you can use nlfilter() in the Image Processing Toolbox. It slides a window along and gives you the window values which are passed to your function. Your function can be whatever you want. I'm attaching a demo for it for an image (not your function - sorry).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!