#NEWLINE# @ cache #NEWLINE# def pad_center( data , size , axis = ( - 1 ) , **kwargs ) : #NEWLINE# #INDENT# "Wrapper for np.pad to automatically center an array prior to padding.\n    This is analogous to `str.center()`\n\n    Examples\n    --------\n    >>> # Generate a vector\n    >>> data = np.ones(5)\n    >>> librosa.util.pad_center(data, 10, mode='constant')\n    array([ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.])\n\n    >>> # Pad a matrix along its first dimension\n    >>> data = np.ones((3, 5))\n    >>> librosa.util.pad_center(data, 7, axis=0)\n    array([[ 0.,  0.,  0.,  0.,  0.],\n           [ 0.,  0.,  0.,  0.,  0.],\n           [ 1.,  1.,  1.,  1.,  1.],\n           [ 1.,  1.,  1.,  1.,  1.],\n           [ 1.,  1.,  1.,  1.,  1.],\n           [ 0.,  0.,  0.,  0.,  0.],\n           [ 0.,  0.,  0.,  0.,  0.]])\n    >>> # Or its second dimension\n    >>> librosa.util.pad_center(data, 7, axis=1)\n    array([[ 0.,  1.,  1.,  1.,  1.,  1.,  0.],\n           [ 0.,  1.,  1.,  1.,  1.,  1.,  0.],\n           [ 0.,  1.,  1.,  1.,  1.,  1.,  0.]])\n\n    Parameters\n    ----------\n    data : np.ndarray\n        Vector to be padded and centered\n\n    size : int >= len(data) [scalar]\n        Length to pad `data`\n\n    axis : int\n        Axis along which to pad and center the data\n\n    kwargs : additional keyword arguments\n      arguments passed to `np.pad()`\n\n    Returns\n    -------\n    data_padded : np.ndarray\n        `data` centered and padded to length `size` along the\n        specified axis\n\n    Raises\n    ------\n    ParameterError\n        If `size < data.shape[axis]`\n\n    See Also\n    --------\n    numpy.pad\n    " #NEWLINE# kwargs . setdefault ( 'mode' , 'constant' ) #NEWLINE# n = data . shape [ axis ] #NEWLINE# lpad = int ( ( ( size - n ) // 2 ) ) #NEWLINE# lengths = ( [ ( 0 , 0 ) ] * data . ndim ) #NEWLINE# lengths [ axis ] = ( lpad , int ( ( ( size - n ) - lpad ) ) ) #NEWLINE# if ( lpad < 0 ) : #NEWLINE# #INDENT# raise ParameterError ( 'Target size ({:d}) must be at least input size ({:d})' . format ( size , n ) ) #NEWLINE# #UNINDENT# return np . pad ( data , lengths , ** kwargs )