amep.utils.weighted_runningmean#

amep.utils.weighted_runningmean(data, nav: int, kernel: str | ndarray = 'homogenous', mode: str = 'valid', width: float = 1.0) ndarray#

Compute the weighted running mean of a 1-dimensional array.

The weights can be specified as a kernel matrix, or one of the preset variants.

Parameters:
  • data (np.ndarray) – input data of shape (N, )

  • nav (int) – length of the kernel if standard is chosen

  • kernel (str|np.ndarray) – Kind of kernel to be used for weights. Possible are homogenous, triangle and gauss. Provided a fitting array will also use normed version of this to weight. Weight array should have positive entries. Otherwise use np.convolve directly.

  • mode (str) – ‘same’, ‘valid’, or ‘full’

  • width (float) – width of the gaussian. Only effective when using gaussian kernel.

Returns:

averaged data

Return type:

np.ndarray of shape (N-(nav-1), )

Examples

>>> import amep
>>> import numpy as np
>>> x = np.linspace(0,2*np.pi,500)
>>> y = np.sin(x)*(1+np.random.rand(len(x)))
>>> yav = amep.utils.weighted_runningmean(
...     y, kernel='triangle', nav=20
... )
>>> xav = amep.utils.weighted_runningmean(
...     x, kernel='homogenous', nav=20
... )
>>> fig, axs = amep.plot.new()
>>> axs.plot(x, y, label='data')
>>> axs.plot(
...     xav, yav, label='weighted running mean',
...     marker='', c='orange'
... )
>>> axs.legend()
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$y$')
>>> fig.savefig('./figures/utils/utils-weighted_runningmean.png')
>>>
../_images/utils-weighted_runningmean.png