librosa.resample
- librosa.resample(y, *, orig_sr, target_sr, res_type='soxr_hq', fix=True, scale=False, axis=-1, **kwargs)[source]
Resample a time series from orig_sr to target_sr
By default, this uses a high-quality method (soxr_hq) for band-limited sinc interpolation. The alternate
res_typevalues listed below offer different trade-offs of speed and quality.- Parameters:
- ynp.ndarray [shape=(…, n, …)]
audio time series, with n samples along the specified axis.
- orig_srnumber > 0 [scalar]
original sampling rate of
y- target_srnumber > 0 [scalar]
target sampling rate
- res_typestr (default: soxr_hq)
resample type
- ‘soxr_vhq’, ‘soxr_hq’, ‘soxr_mq’ or ‘soxr_lq’
soxrVery high-, High-, Medium-, Low-quality FFT-based bandlimited interpolation.'soxr_hq'is the default setting ofsoxr.- ‘soxr_qq’
soxrQuick cubic interpolation (very fast, but not bandlimited)- ‘kaiser_best’
resampyhigh-quality mode- ‘kaiser_fast’
resampyfaster method- ‘fft’ or ‘scipy’
scipy.signal.resampleFourier method.- ‘polyphase’
scipy.signal.resample_polypolyphase filtering. (fast)- ‘linear’
samplerate linear interpolation. (very fast, but not bandlimited)
- ‘zero_order_hold’
samplerate repeat the last value between samples. (very fast, but not bandlimited)
- ‘sinc_best’, ‘sinc_medium’ or ‘sinc_fastest’
samplerate high-, medium-, and low-quality bandlimited sinc interpolation.
Note
Not all options yield a bandlimited interpolator. If you use soxr_qq, polyphase, linear, or zero_order_hold, you need to be aware of possible aliasing effects.
Note
samplerate and
resampyare not installed withlibrosa. To use samplerate orresampy, they should be installed manually:$ pip install samplerate $ pip install resampy
Note
When using
res_type='polyphase', only integer sampling rates are supported.- fixbool
adjust the length of the resampled signal to be of size exactly
ceil(target_sr * len(y) / orig_sr)- scalebool
Scale the resampled signal so that
yandy_hathave approximately equal total energy.- axisint
The target axis along which to resample. Defaults to the trailing axis.
- **kwargsadditional keyword arguments
If
fix==True, additional keyword arguments to pass tolibrosa.util.fix_length.
- Returns:
- y_hatnp.ndarray [shape=(…, n * target_sr / orig_sr, …)]
yresampled fromorig_srtotarget_sralong the target axis
- Raises:
- ParameterError
If
res_type='polyphase'andorig_srortarget_srare not both integer-valued.
See also
Notes
This function caches at level 20.
Examples
Downsample from 22 KHz to 8 KHz
>>> y, sr = librosa.load(librosa.ex('trumpet'), sr=22050) >>> y_8k = librosa.resample(y, orig_sr=sr, target_sr=8000) >>> y.shape, y_8k.shape ((117601,), (42668,))