amep.utils.segmented_mean#

amep.utils.segmented_mean(data: ndarray, n: int, verbose: bool = False) ndarray#

Compute the mean of n adjacent data points.

Pools n adjacent data points into one bin. Creates array of len(data)/n bins. The used data may be reduced in case the length is not divisable by n.

Parameters:
  • data (np.ndarray) – Input data to be averaged.

  • n (int) – Number of adjacent data points to be averaged.

  • verbose (bool, optional) – If True, runtime information is printed. The default is False.

Returns:

Averaged data

Return type:

np.ndarray

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.segmented_mean(y, n=20)
>>> xav = amep.utils.segmented_mean(x, n=20)
>>> fig, axs = amep.plot.new()
>>> axs.plot(x, y, label='data', ls='')
>>> axs.plot(
...     xav, yav, label='segmented mean',
...     marker='', lw=2, c='orange'
... )
>>> axs.legend()
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$y$')
>>> fig.savefig('./figures/utils/utils-segmented_mean.png')
>>>
../_images/utils-segmented_mean.png