librosa.feature.pitch_spectrogram#

librosa.feature.pitch_spectrogram(*, y=None, sr=22050, S=None, n_fft=2048, hop_length=512, win_length=None, window='hann', center=True, pad_mode='constant', power=2.0, tuning=0.0, n_pitches=128, fmin=None, intervals='equal', bins_per_octave=12, filter_window=None, norm=None, **kwargs)[source]#

Compute a musical pitch-anchored spectrogram.

Groups spectral energy from an STFT or input spectrogram into discrete musical pitch bands under specified intonation assumptions (e.g., equal temperament, Pythagorean, Just Intonation).

Parameters:
ynp.ndarray [shape=(…, n)]

Audio time-series. Multi-channel is supported.

srnumber > 0 [scalar]

Sampling rate of y.

Snp.ndarray [shape=(…, d, t)]

Pre-computed spectrogram.

n_fftint > 0 [scalar]

Length of the FFT window.

hop_lengthint > 0 [scalar]

Number of samples between successive frames.

win_lengthint <= n_fft [scalar]

Window length for the STFT.

windowstr, tuple, number, function, or np.ndarray [shape=(n_fft,)]

Window specification for the STFT.

centerbool

If True, the input signal is padded so that frame t is centered at t * hop_length.

pad_modestr

Padding mode for the STFT.

powerfloat > 0 [scalar]

Exponent for the magnitude spectrogram: e.g., 1 for energy, 2 for power, etc.

tuningfloat [scalar] or None

Deviation (in fractions of a bin) from A440 tuning. If None and intervals == 'equal', tuning is automatically estimated from the spectrogram.

n_pitchesint > 0 [scalar], default=128

Number of pitch bands to generate. Default 128 covers MIDI notes 0 to 127.

fminfloat > 0, str, or None, optional

Minimum pitch center frequency (or note name in SPN format, e.g. ‘C1’). If None, defaults to MIDI 0 (~8.18 Hz) when n_pitches=128, or ‘C1’.

intervalsstr or collection of float, default=’equal’

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.

filter_windowstr, callable, or None, default=None

Filter shape for each pitch band. Default None (or ‘ones’) performs exact rectangular energy summation across semitone boundaries. Also supports ‘triangle’, ‘hann’, ‘gaussian’.

norm{None, ‘slaney’, or number}, default=None

Normalization applied to each filter row.

**kwargsadditional keyword arguments

Additional keyword arguments to librosa.filters.pitch_filterbank.

Returns:
pitch_spectrogramnp.ndarray [shape=(…, n_pitches, t)]

Pitch spectrogram.

Examples

Compute a 128-pitch spectrogram with rectangular pooling (libfmp pool_pitch equivalent):

>>> y, sr = librosa.loadx("trumpet")
>>> S_pitch = librosa.feature.pitch_spectrogram(y=y, sr=sr)
>>> S_pitch.shape
(128, 230)

Compute a Just Intonation pitch spectrogram:

>>> S_ji = librosa.feature.pitch_spectrogram(
...     y=y, sr=sr, intervals="ji5", fmin="C1", n_pitches=84
... )
>>> S_ji.shape
(84, 230)

Display a 8-octave (C1-B8) pitch spectrogram using musical note names:

>>> S_p7 = librosa.feature.pitch_spectrogram(y=y, sr=sr, n_pitches=96, fmin="C1")
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> S_dB = librosa.power_to_db(S_p7, ref=np.max)
>>> img = librosa.display.specshow(
...     S_dB,
...     x_axis="time",
...     y_axis="pitch_note",
...     sr=sr,
...     fmin=librosa.note_to_hz("C1"),
...     ax=ax,
... )
>>> librosa.display.colorbar_db(img)
>>> ax.set(title="Pitch spectrogram")
../../_images/librosa-feature-pitch_spectrogram-1.png