librosa.util.fix_length

librosa.util.fix_length(data, size, axis=-1, **kwargs)[source]

Fix the length an array data to exactly size.

If data.shape[axis] < n, pad according to the provided kwargs. By default, data is padded with trailing zeros.

Parameters:
data : np.ndarray

array to be length-adjusted

size : int >= 0 [scalar]

desired length of the array

axis : int, <= data.ndim

axis along which to fix length

kwargs : additional keyword arguments

Parameters to np.pad()

Returns:
data_fixed : np.ndarray [shape=data.shape]

data either trimmed or padded to length size along the specified axis.

See also

numpy.pad

Examples

>>> y = np.arange(7)
>>> # Default: pad with zeros
>>> librosa.util.fix_length(y, 10)
array([0, 1, 2, 3, 4, 5, 6, 0, 0, 0])
>>> # Trim to a desired length
>>> librosa.util.fix_length(y, 5)
array([0, 1, 2, 3, 4])
>>> # Use edge-padding instead of zeros
>>> librosa.util.fix_length(y, 10, mode='edge')
array([0, 1, 2, 3, 4, 5, 6, 6, 6, 6])