librosa.util.pad_center

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

Pad an array to a target length along a target axis.

This differs from np.pad by centering the data prior to padding, analogous to str.center

Parameters:
datanp.ndarray

Vector to be padded and centered

sizeint >= len(data) [scalar]

Length to pad data

axisint

Axis along which to pad and center the data

**kwargsadditional keyword arguments

arguments passed to np.pad

Returns:
data_paddednp.ndarray

data centered and padded to length size along the specified axis

Raises:
ParameterError

If size < data.shape[axis]

See also

numpy.pad

Examples

>>> # Generate a vector
>>> data = np.ones(5)
>>> librosa.util.pad_center(data, size=10, mode='constant')
array([ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.])
>>> # Pad a matrix along its first dimension
>>> data = np.ones((3, 5))
>>> librosa.util.pad_center(data, size=7, axis=0)
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> # Or its second dimension
>>> librosa.util.pad_center(data, size=7, axis=1)
array([[ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  0.]])