Caution
You're reading an old version of this documentation. If you want up-to-date information, please have a look at 0.10.2.
librosa.resample
- librosa.resample(y, *, orig_sr, target_sr, res_type='kaiser_best', fix=True, scale=False, **kwargs)[source]
Resample a time series from orig_sr to target_sr
By default, this uses a high-quality (but relatively slow) method (‘kaiser_best’) for band-limited sinc interpolation. The alternate
res_type
values listed below offer different trade-offs of speed and quality.- Parameters:
- ynp.ndarray [shape=(…, n)]
audio time series. Multi-channel is supported.
- orig_srnumber > 0 [scalar]
original sampling rate of
y
- target_srnumber > 0 [scalar]
target sampling rate
- res_typestr
resample type
- ‘kaiser_best’ (default)
resampy
high-quality mode- ‘kaiser_fast’
resampy
faster method- ‘fft’ or ‘scipy’
scipy.signal.resample
Fourier method.- ‘polyphase’
scipy.signal.resample_poly
polyphase filtering. (fast)- ‘linear’
samplerate linear interpolation. (very fast)
- ‘zero_order_hold’
samplerate repeat the last value between samples. (very fast)
- ‘sinc_best’, ‘sinc_medium’ or ‘sinc_fastest’
samplerate high-, medium-, and low-quality sinc interpolation.
- ‘soxr_vhq’, ‘soxr_hq’, ‘soxr_mq’ or ‘soxr_lq’
soxr
Very high-, High-, Medium-, Low-quality FFT-based bandlimited interpolation.'soxr_hq'
is the default setting ofsoxr
(fast)- ‘soxr_qq’
soxr
Quick cubic interpolation (very fast)
Note
samplerate and
soxr
are not installed withlibrosa
. To use samplerate orsoxr
, they should be installed manually:$ pip install samplerate $ pip install soxr
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
y
andy_hat
have approximately equal total energy.- **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)]
y
resampled fromorig_sr
totarget_sr
- Raises:
- ParameterError
If
res_type='polyphase'
andorig_sr
ortarget_sr
are 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,))