librosa.util.fix_length

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

Fix the length an array data to exactly size along a target axis.

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

Parameters
datanp.ndarray

array to be length-adjusted

sizeint >= 0 [scalar]

desired length of the array

axisint, <= data.ndim

axis along which to fix length

kwargsadditional keyword arguments

Parameters to np.pad

Returns
data_fixednp.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])