librosa.segment.diagonal_convolve#
- librosa.segment.diagonal_convolve(data, kernel, *, k=0, mode='same', boundary='fill', fillvalue=0.0)[source]#
Efficient 2D convolution along a diagonal.
Computes the 2-dimensional convolution of data and kernel evaluated only along the diagonal specified by k, without allocating or calculating the full 2D convolution output [1].
- Parameters:
- datanp.ndarray [shape=(…, n_rows, n_cols)]
Input 2D matrix (or multi-channel batch of matrices), such as a recurrence or self-similarity matrix. Sparse matrices are not supported.
- kernelnp.ndarray [shape=(m, m)] or tuple of np.ndarray [shape=((m1,), (m2,))]
Filter kernel. Can be:
A 2D array of shape
(m1, m2)A 1D array
vof shape(m,)representing a symmetric separable kernelv @ v.TA tuple of two 1D arrays
(u, v)representing a separable kernelu @ v.T
- kint, default=0
Diagonal offset.
k=0is the main diagonal.k > 0is above the main diagonal,k < 0is below. Matches numpy.diagonal(…, offset=k).- mode{‘same’, ‘valid’, ‘full’}, default=’same’
Convolution mode, matching
scipy.signal.convolve2d:'same': Output has the same length as the diagonal of data, centered.'valid': Output contains only positions where kernel completely overlaps data.'full': Output contains all positions where kernel overlaps data by at least one element.
- boundarystr, default=’fill’
Boundary condition matching
scipy.signal.convolve2d:'fill': pad with fillvalue (default 0.0).'symm': symmetric reflection padding.'wrap': circular padding.Standard
numpy.padmode strings (e.g.'constant','edge','reflect') are also accepted.
- fillvaluefloat, default=0.0
Value to fill past boundaries when boundary=’fill’ or ‘constant’.
- Returns:
- diag_convnp.ndarray [shape=(…, n_diag)]
The 2D convolution evaluated along diagonal k.
- Raises:
- ParameterTypeError
If data is sparse.
- ParameterShapeError
If kernel shape is invalid.
- ParameterValueError
If mode is unsupported.
References
Examples
Compute the main diagonal of a 2D convolution with a 17x17 checkerboard kernel on a short excerpt:
>>> y, sr = librosa.loadx("nutcracker", duration=30) >>> hop_length = 512 >>> chroma = librosa.feature.chroma_cqt(y=y, sr=sr, hop_length=hop_length) >>> rec = librosa.segment.recurrence_matrix(chroma, mode="affinity", self=True) >>> kernel = librosa.filters.checkerboard_kernel(17) >>> nov = librosa.segment.diagonal_convolve(rec, kernel)
Plot the recurrence matrix and the resulting novelty trace with proportional heights:
>>> import matplotlib.pyplot as plt >>> times = librosa.times_like(nov, sr=sr, hop_length=hop_length) >>> fig, ax = plt.subplots( ... nrows=2, sharex=True, height_ratios=[3, 1], layout="compressed" ... ) >>> librosa.display.specshow( ... rec, x_axis="time", y_axis="time", sr=sr, hop_length=hop_length, ax=ax[0] ... ) >>> ax[0].set(title="Recurrence Matrix") >>> ax[0].label_outer() >>> ax[1].plot(times, nov, label="Novelty") >>> ax[1].set(title="Diagonal 2D Convolution (Novelty)", xlabel="Time (s)") >>> ax[1].legend()