Hi all,
Currently working on implementing an anomaly detection algorithm in python, but strangely I found myself blocked by the lack of function for moving average and similar function that exists in matlab.
Has anyone else encountered the same issue?
Best regards
Moving average is just convolution with ones(). Maybe there could be a convenience function? Or do you want to do things other than average? Would pandas work?
A moving average may interpreted as a FIR-Filter. Hence, lfilter is probably the easiest way to implement it:
import numpy as np
from scipy.signal import lfilter
m = 4 # length of averaging block
x = np.arange(10) # x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = lfilter(np.ones(m) / m, 1, x) # y = [0. , 0.25, 0.75, 1.5 , 2.5 , 3.5 , 4.5 , 5.5 , 6.5 , 7.5 ]