librosa.util.pad_center

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

Wrapper for np.pad to automatically center an array prior to padding. This is analogous to str.center()

Parameters:
data : np.ndarray

Vector to be padded and centered

size : int >= len(data) [scalar]

Length to pad data

axis : int

Axis along which to pad and center the data

kwargs : additional keyword arguments

arguments passed to np.pad()

Returns:
data_padded : np.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, 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, 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, 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.]])