librosa.interp_frequency#

librosa.interp_frequency(x, *, target_freqs, freqs, kind='linear', fill_value=0.0, axis=-2, assume_sorted=False)[source]#

Interpolate an array along a frequency axis.

Given a frequency-based energy representation (such as an STFT spectrogram, tempogram, or reassigned spectrogram), this function interpolates the values along the specified frequency axis onto a new grid of target frequencies.

This function supports both uniform frequency representations and time-varying frequency grids (e.g. from reassigned_spectrogram).

Parameters:
xnp.ndarray

The input array to interpolate.

target_freqsnp.ndarray

The target frequency coordinates at which to evaluate the interpolator. If 1-dimensional, all slices along other dimensions are evaluated at these frequencies. If multi-dimensional, the shape along axis must match the target length, and other dimensions must be broadcast-compatible with x.

freqsnp.ndarray

The frequency values corresponding to x’s elements along axis. Can be 1-dimensional with length x.shape[axis] (e.g., from fft_frequencies, cqt_frequencies, or tempo_frequencies), or multi-dimensional with shape matching or broadcastable to x (e.g., instantaneous frequencies from reassigned_spectrogram).

kindstr

Interpolation type. See scipy.interpolate.interp1d. Common choices: 'linear', 'nearest', 'cubic'.

fill_valuefloat, tuple of float, or ‘extrapolate’

Value to fill when evaluating outside the observed frequency bounds. Defaults to 0.0.

axisint

The axis along which to interpolate frequency. Defaults to -2.

assume_sortedbool

If True, the source frequency coordinates are assumed to be monotonically increasing. Defaults to False.

Returns:
x_interpnp.ndarray

The interpolated array. Dimension axis will have length matching the target frequency dimension.

Examples

Interpolate an STFT spectrogram onto a logarithmic CQT frequency grid:

>>> y, sr = librosa.loadx("trumpet", duration=1.0)
>>> S = np.abs(librosa.stft(y))
>>> fft_freqs = librosa.fft_frequencies(sr=sr)
>>> cqt_freqs = librosa.cqt_frequencies(n_bins=84, fmin=librosa.note_to_hz("C1"))
>>> S_cqt_grid = librosa.interp_frequency(
...     S, target_freqs=cqt_freqs, freqs=fft_freqs
... )
>>> S_cqt_grid.shape
(84, 44)

Lock a reassigned spectrogram to a uniform frequency grid:

>>> freqs, times, mags = librosa.reassigned_spectrogram(y, sr=sr)
>>> f_grid = np.linspace(0, sr / 2, 512)
>>> S_locked = librosa.interp_frequency(mags, target_freqs=f_grid, freqs=freqs)
>>> S_locked.shape
(512, 44)