moving_average
- mcalf.utils.smooth.moving_average(array, width)[source]
Boxcar moving average.
Calculate the moving average of an array with a boxcar of defined width. An odd width is recommended.
- Parameters:
array (numpy.ndarray, ndim=1) – Array to find the moving average of.
width (int) – Width of the boxcar. Odd integer recommended. Less than or equal to length of array.
- Returns:
averaged – Averaged array.
- Return type:
numpy.ndarray, shape=`array`
Notes
The moving average is calculated at each point of the array by finding the (unweighted) mean of the subarrays of length given by width. These subarrays are centred at the point in the array that the current average is currently being calculated for. If an odd width is chosen, the sub array will include the current point plus an equal number of points on either side. However, if an even width is chosen, the sub array will bias including the extra point to the left of the current index. If the subarray spans past the boundaries, the values beyond the boundary is ignored and the mean is calculated by dividing by the number of points that are inside the boundaries.
Examples
>>> x = np.array([1, 2, 3, 4, 5]) >>> moving_average(x, 3) array([1.5, 2. , 3. , 4. , 4.5]) >>> moving_average(x, 2) array([1. , 1.5, 2.5, 3.5, 4.5])