librosa.filters.frequency_filterbank#

librosa.filters.frequency_filterbank(freq_in, freq_out, *, bandwidths=None, window='triangle', scale='linear', norm=None, dtype=<class 'numpy.float32'>)[source]#

Construct a linear filterbank matrix over arbitrary input and output frequency coordinates.

Parameters:
freq_innp.ndarray [shape=(n_in,)]

Coordinates of input frequency bins (e.g. from fft_frequencies, cqt_frequencies). Must be a 1-dimensional array and monotonically non-decreasing.

freq_outnp.ndarray [shape=(n_out,)]

Center coordinates of target filter bands. Must be a 1-dimensional non-empty array and strictly increasing.

bandwidthsfloat or np.ndarray [shape=(n_out,)], optional

Bandwidth of each filter. If None, bandwidths and filter boundaries are automatically derived from adjacent centers in freq_out.

windowstr or callable, default=”triangle”

Filter window shape:

  • ‘triangle’ or ‘triang’: Piecewise linear ramp from the lower boundary to the center and down to the upper boundary.

  • ‘boxcar’ or ‘rect’ or ‘rectangular’: Rectangular indicator window between adjacent midpoints.

  • ‘hann’ or ‘cosine’: Raised-cosine bell.

  • ‘gaussian’: Gaussian bell centered at each band center.

  • Callable: Any window function \(w(x)\) mapping normalized coordinates \(x \in [-1, 1]\) to filter weights.

scale{‘linear’, ‘log’}, default=’linear’

Domain in which bandwidths and filter supports are evaluated:

  • ‘linear’: Distances and bandwidths are measured in linear Hz.

  • ‘log’: Distances and bandwidths are measured in logarithmic units (octaves). Geometric midpoints are used for filter boundaries, preserving constant-Q relationships.

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

Normalization applied to each filter row:

  • None: Unnormalized peak height (1.0).

  • ‘slaney’: Continuous area normalization (energy density per Hz), scaling each filter by \(2 / \text{width}\) (or \(1 / \text{width}\) for boxcar).

  • Numeric: Use librosa.util.normalize to normalize each filter to unit \(L_p\) norm.

dtypenp.dtype, default=np.float32

Data type of the output basis matrix.

Returns:
filterbanknp.ndarray [shape=(n_out, n_in)]

Transformation matrix: Y = filterbank.dot(X)

Raises:
ParameterError

If freq_in is not 1D or not monotonically non-decreasing. If freq_out is not 1D, is empty, or not strictly increasing. If scale is not ‘linear’ or ‘log’. If scale='log' and any frequency in freq_out is non-positive. If bandwidths is negative or improperly shaped. If window or norm is unsupported.

Notes

This function caches at level 10.

Examples

Construct a triangular filterbank mapping FFT bins onto linearly spaced bands:

>>> sr, n_fft = 22050, 2048
>>> f_in = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
>>> f_out = np.linspace(200, 4000, 32)
>>> fb = librosa.filters.frequency_filterbank(f_in, f_out, window="triangle")
>>> fb.shape
(32, 1025)

Construct a logarithmic boxcar filterbank (exact energy pooling):

>>> f_pitches = librosa.midi_to_hz(np.arange(21, 109))
>>> fb_pitch = librosa.filters.frequency_filterbank(
...     f_in, f_pitches, window="boxcar", scale="log"
... )
>>> fb_pitch.shape
(88, 1025)
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(nrows=2, sharex=True)
>>> ax[0].plot(f_in, fb.T)
>>> ax[0].set(xlim=[0, 4500], ylabel="Weight", title="Filter responses")
>>> img = librosa.display.specshow(fb, x_coords=f_in, x_axis="linear", ax=ax[1])
>>> ax[1].set(ylabel="Filter band", title="Filterbank matrix")
>>> fig.colorbar(img, ax=ax[1])
../../_images/librosa-filters-frequency_filterbank-1.png