Efficient Signal Resampling
resampy is a Python module for efficient time-series resampling, primarily designed for audio applications. It implements the band-limited sinc interpolation method for sampling rate conversion, offering performance advantages over `scipy.signal.resample` for long-duration signals. The current version is 0.4.3, with maintenance releases occurring periodically to ensure compatibility and address minor issues.
Warnings
- breaking Since version 0.4.0, integer-valued inputs to `resampy.resample` will always produce floating-point outputs. Prior versions could potentially return integer outputs if the input was integer-valued.
- gotcha Starting from version 0.4.0, parallel computation in `resampy.resample` is disabled by default (`parallel=False`). In previous versions, it was `True` by default.
- deprecated Versions of `resampy` prior to 0.4.3 may raise a `DeprecationWarning` related to the `importlib_resources` API when used with newer Python versions, as the internal usage has been updated in 0.4.3.
- gotcha Version 0.3.0 introduced an efficiency regression that could lead to slower performance compared to previous versions. This regression was fixed in version 0.3.1.
- gotcha The output length of signals resampled with `resampy` might differ slightly from other libraries like `scipy.signal.resample` for the same upsampling ratio, due to different internal calculation and rounding strategies.
- gotcha In version 0.2.0, the core resampling implementation was rewritten from Cython to Numba. This change may affect performance characteristics or introduce new dependency considerations (e.g., Numba installation issues) for users upgrading from 0.1.x versions.
Install
-
pip install resampy
Imports
- resample
import resampy resampy.resample(...)
Quickstart
import numpy as np
import resampy
# Generate a 5-second sine wave at 440 Hz
sr_orig = 44100 # Original sample rate
duration = 5 # seconds
frequency = 440 # Hz
t = np.arange(int(sr_orig * duration)) / sr_orig
x = np.sin(2 * np.pi * frequency * t)
# Resample the signal to a new sample rate (e.g., 22050 Hz)
sr_new = 22050
y = resampy.resample(x, sr_orig, sr_new)
print(f"Original signal length: {len(x)} samples")
print(f"Resampled signal length: {len(y)} samples")
print(f"Original sample rate: {sr_orig} Hz")
print(f"New sample rate: {sr_new} Hz")