librosa.filters.pitch_basis#

librosa.filters.pitch_basis(*, sr=None, n_fft=None, freq_in=None, n_pitches=84, fmin=None, intervals='equal', bins_per_octave=12, tuning=0.0, window='triangle', harmonics=None, harmonic_weights=None, offset=0.0001, n_residual=5, residual=None, norm=2, dtype=<class 'numpy.float32'>)[source]#

Construct a pitch template basis matrix for decomposition.

Produces a non-negative spectral basis matrix suitable for initializing or constraining non-negative matrix factorization (NMF) or other dictionary-based decompositions.

By default, pitch templates model a harmonic comb decaying as \(1/m\), supplemented by 5 non-negative Discrete Cosine Transform (DCT) residual templates (broadband, low shelf, high shelf, mid-scoop, and mid-hump) to capture transients and unpitched noise.

Parameters:
srnumber > 0 [scalar, optional]

Audio sampling rate. Required if freq_in is not specified.

n_fftint > 0 [scalar, optional]

Length of the FFT frame. Required if freq_in is not specified.

freq_innp.ndarray [shape=(n_in,)], optional

Coordinates of input frequency bins. If not provided, computed as fft_frequencies(sr=sr, n_fft=n_fft).

n_pitchesint > 0 [scalar], default=84

Number of pitch bands to generate. Default 84 covers 7 octaves.

fminfloat > 0, str, or None, optional

Minimum pitch center frequency (or note name in SPN format, e.g. ‘C1’). If None, defaults to ‘C1’ (~32.7 Hz).

intervalsstr or collection of float, default=’equal’

Tuning / interval system to use:

  • ‘equal’: Equal temperament.

  • ‘pythagorean’: Pythagorean intervals.

  • ‘ji3’, ‘ji5’, ‘ji7’: 3-limit, 5-limit, or 7-limit Just Intonation.

  • Collection of floats in [1, 2): Custom interval set.

bins_per_octaveint > 0 [scalar], default=12

Number of bins per octave in the interval system.

tuningfloat [scalar], default=0.0

Tuning deviation from A440 in fractions of a bin.

windowstr or callable, default=’triangle’

Filter shape for each pitch / harmonic peak: ‘triangle’, ‘boxcar’, ‘hann’, or ‘gaussian’.

harmonicssequence of float, optional

Harmonics to include in each pitch template. If None, includes all harmonics up to Nyquist with \(1/m\) decay. Pass harmonics=[1] for fundamental-only templates (transposed pitch filter).

harmonic_weightssequence of float, optional

Weights applied to each harmonic. If None, defaults to \(1.0 / m\).

offsetfloat >= 0, default=1e-4

Small additive offset applied to all template entries to ensure strictly positive values, preventing multiplicative updates from freezing zero bins.

n_residualint >= 0, default=5

Number of DCT-based residual templates to append for broadband transients/noise. Odd numbers (e.g., 1, 3, 5) produce symmetric shelf/hump pairs.

residualnp.ndarray [shape=(n_in, n_residual)], optional

Pre-defined residual templates to append instead of the DCT basis.

norm{None, number}, default=2

Column normalization applied to each basis template.

dtypenp.dtype, default=np.float32

Data type of the output basis matrix.

Returns:
basisnp.ndarray [shape=(n_in, n_components)]

Non-negative basis matrix with n_components = n_pitches + n_residual. Columns 0 to n_pitches - 1 are the pitch templates. Columns n_pitches to n_components - 1 are the residual templates.

Notes

This function caches at level 10.

Examples

Construct a pitch template basis covering 7 octaves with 5 residual templates:

>>> basis = librosa.filters.pitch_basis(sr=22050, n_fft=2048)
>>> basis.shape
(1025, 89)

Construct fundamental-only pitch templates without residual components:

>>> basis_fund = librosa.filters.pitch_basis(
...     sr=22050, n_fft=2048, harmonics=[1], n_residual=0
... )
>>> basis_fund.shape
(1025, 84)

Plot the basis templates:

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> img = librosa.display.specshow(
...     librosa.amplitude_to_db(basis, ref=np.max),
...     sr=22050,
...     n_fft=2048,
...     y_axis="log",
...     ax=ax,
... )
>>> ax.set(
...     xlabel="Component", ylabel="Frequency (Hz)", title="Pitch template basis"
... )
>>> fig.colorbar(img, ax=ax, format="%+2.0f dB")
../../_images/librosa-filters-pitch_basis-1.png