def recurrence_to_lag(rec, pad=True, axis=-<NUM_LIT:1>):
axis = np.abs(axis)<EOL>if rec.ndim != <NUM_LIT:2> or rec.shape[<NUM_LIT:0>] != rec.shape[<NUM_LIT:1>]:<EOL><INDENT>raise ParameterError('<STR_LIT>'<EOL>'<STR_LIT:{}>'.format(rec.shape))<EOL><DEDENT>sparse = scipy.sparse.issparse(rec)<EOL>roll_ax = None<EOL>if sparse:<EOL><INDENT>roll_ax = <NUM_LIT:1> - axis<EOL>lag_format = rec.format<EOL>if axis == <NUM_LIT:0>:<EOL><INDENT>rec = rec.tocsc()<EOL><DEDENT>elif axis in (-<NUM_LIT:1>, <NUM_LIT:1>):<EOL><INDENT>rec = rec.tocsr()<EOL><DEDENT><DEDENT>t = rec.shape[axis]<EOL>if sparse:<EOL><INDENT>if pad:<EOL><INDENT>kron = np.asarray([[<NUM_LIT:1>, <NUM_LIT:0>]]).swapaxes(axis, <NUM_LIT:0>)<EOL>lag = scipy.sparse.kron(kron.astype(rec.dtype), rec, format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>lag = scipy.sparse.lil_matrix(rec)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if pad:<EOL><INDENT>padding = [(<NUM_LIT:0>, <NUM_LIT:0>), (<NUM_LIT:0>, <NUM_LIT:0>)]<EOL>padding[(<NUM_LIT:1>-axis)] = (<NUM_LIT:0>, t)<EOL>lag = np.pad(rec, padding, mode='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>lag = rec.copy()<EOL><DEDENT><DEDENT>idx_slice = [slice(None)] * lag.ndim<EOL>for i in range(<NUM_LIT:1>, t):<EOL><INDENT>idx_slice[axis] = i<EOL>lag[tuple(idx_slice)] = util.roll_sparse(lag[tuple(idx_slice)], -i, axis=roll_ax)<EOL><DEDENT>if sparse:<EOL><INDENT>return lag.asformat(lag_format)<EOL><DEDENT>return np.ascontiguousarray(lag.T).T<EOL>
Convert a recurrence matrix into a lag matrix.

        `lag[i, j] == rec[i+j, j]`

    Parameters
    ----------
    rec : np.ndarray, or scipy.sparse.spmatrix [shape=(n, n)]
        A (binary) recurrence matrix, as returned by `recurrence_matrix`

    pad : bool
        If False, `lag` matrix is square, which is equivalent to
        assuming that the signal repeats itself indefinitely.

        If True, `lag` is padded with `n` zeros, which eliminates
        the assumption of repetition.

    axis : int
        The axis to keep as the `time` axis.
        The alternate axis will be converted to lag coordinates.

    Returns
    -------
    lag : np.ndarray
        The recurrence matrix in (lag, time) (if `axis=1`)
        or (time, lag) (if `axis=0`) coordinates

    Raises
    ------
    ParameterError : if `rec` is non-square

    See Also
    --------
    recurrence_matrix
    lag_to_recurrence

    Examples
    --------
    >>> y, sr = librosa.load(librosa.util.example_audio_file())
    >>> mfccs = librosa.feature.mfcc(y=y, sr=sr)
    >>> recurrence = librosa.segment.recurrence_matrix(mfccs)
    >>> lag_pad = librosa.segment.recurrence_to_lag(recurrence, pad=True)
    >>> lag_nopad = librosa.segment.recurrence_to_lag(recurrence, pad=False)

    >>> import matplotlib.pyplot as plt
    >>> plt.figure(figsize=(8, 4))
    >>> plt.subplot(1, 2, 1)
    >>> librosa.display.specshow(lag_pad, x_axis='time', y_axis='lag')
    >>> plt.title('Lag (zero-padded)')
    >>> plt.subplot(1, 2, 2)
    >>> librosa.display.specshow(lag_nopad, x_axis='time')
    >>> plt.title('Lag (no padding)')
    >>> plt.tight_layout()