When calculating a simple moving average, numpy.convolve
appears to do the job.
Question: How is the calculation done when you use
It is notable also that the kernel is "centered" in the sense that indices for the kernel are taken with respect to the centre element of the array. In other words, for arrays with index starting at 0 (as in python), the function
B = np.convolve (A, K)
computes
where m = (len(K) - 1)//2
(integer division). This is an integer, also when len(K)
is even.
The summation is nominally over all values of i
from -∞ to ∞,
where values of A
out of range are assumed equal to zero. The same is true for values of the kernel. For np.convolution2D
, you have to option of using the mode, boundary and fillvalue options to specify how values of A
out of range are treated.
Thus, for example, you get different answers for np.convolve(A, K)
if
K = np.array([1, 2, 3])
or K = np.array([1, 2, 3, 0, 0])