block_mean#
- pypillometry.signal.block_mean(signal, factor, mask=None, min_frac=0.5)[source]#
Means of the valid samples in consecutive blocks of factor samples.
\[n_t = \sum_{j \in \text{block } t} v_j, \qquad \bar x_t = \frac{1}{n_t} \sum_{j \in \text{block } t} v_j x_j\]with \(v_j = 1\) for a valid sample and 0 otherwise.
- Parameters:
signal (np.ndarray or np.ma.MaskedArray) – the signal; masked, NaN and inf samples are invalid
factor (int) – number of samples per block; samples after the last complete block are dropped
mask (np.ndarray, optional) – boolean array, True for additional invalid samples
min_frac (float, optional) – a block is missing (NaN) unless at least this fraction of its samples is valid (default 0.5; 0: one valid sample is enough)
- Return type:
- Returns:
y (np.ndarray) – mean of the valid samples of each block (NaN where missing)
n (np.ndarray) – number of valid samples of each block (int; also for the blocks that are missing because of min_frac)
Examples
>>> x = np.array([1., 2., 3., np.nan, 5., np.nan, np.nan, np.nan, 9.]) >>> y, n = block_mean(x, 4) >>> y array([ 2., nan]) >>> n array([3, 1])