Core functionality¶
Core resampling interface
- resampy.core.resample(x, sr_orig, sr_new, axis=- 1, filter='kaiser_best', **kwargs)[source]¶
Resample a signal x from sr_orig to sr_new along a given axis.
- Parameters
- xnp.ndarray, dtype=np.float*
The input signal(s) to resample.
- sr_origint > 0
The sampling rate of x
- sr_newint > 0
The target sampling rate of the output signal(s)
- axisint
The target axis along which to resample x
- filteroptional, str or callable
The resampling filter to use.
By default, uses the kaiser_best (pre-computed filter).
- kwargs
additional keyword arguments provided to the specified filter
- Returns
- ynp.ndarray
x resampled to sr_new
- Raises
- ValueError
if sr_orig or sr_new is not positive
- TypeError
if the input signal x has an unsupported data type.
Examples
>>> # Generate a sine wave at 440 Hz for 5 seconds >>> sr_orig = 44100.0 >>> x = np.sin(2 * np.pi * 440.0 / sr_orig * np.arange(5 * sr_orig)) >>> x array([ 0. , 0.063, ..., -0.125, -0.063]) >>> # Resample to 22050 with default parameters >>> resampy.resample(x, sr_orig, 22050) array([ 0.011, 0.123, ..., -0.193, -0.103]) >>> # Resample using the fast (low-quality) filter >>> resampy.resample(x, sr_orig, 22050, filter='kaiser_fast') array([ 0.013, 0.121, ..., -0.189, -0.102]) >>> # Resample using a high-quality filter >>> resampy.resample(x, sr_orig, 22050, filter='kaiser_best') array([ 0.011, 0.123, ..., -0.193, -0.103]) >>> # Resample using a Hann-windowed sinc filter >>> resampy.resample(x, sr_orig, 22050, filter='sinc_window', ... window=scipy.signal.hann) array([ 0.011, 0.123, ..., -0.193, -0.103])
>>> # Generate stereo data >>> x_right = np.sin(2 * np.pi * 880.0 / sr_orig * np.arange(len(x)))]) >>> x_stereo = np.stack([x, x_right]) >>> x_stereo.shape (2, 220500) >>> # Resample along the time axis (1) >>> y_stereo = resampy.resample(x, sr_orig, 22050, axis=1) >>> y_stereo.shape (2, 110250)
Filters¶
Filter construction and loading.¶
resampy provides two pre-computed resampling filters which are tuned for either high-quality or fast calculation:
- kaiser_best64 zero-crossings, a Kaiser window with beta=14.769656459379492,
and a roll-off frequency of Nyquist * 0.9475937167399596.
- kaiser_fast16 zero-crossings, a Kaiser window with beta=8.555504641634386,
and a roll-off frequency of Nyquist * 0.85.
- These filters can be used by calling resample as follows:
>>> resampy.resample(x, sr_orig, sr_new, filter='kaiser_best') # High-quality >>> resampy.resample(x, sr_orig, sr_new, filter='kaiser_fast') # Fast calculation
It is also possible to construct custom filters as follows:
>>> resampy.resample(x, sr_orig, sr_new, filter='sinc_window',
... **kwargs)
where **kwargs
are additional parameters to `resampy.filters.sinc_window`_.
- resampy.filters.get_filter(name_or_function, **kwargs)[source]¶
Retrieve a window given its name or function handle.
- Parameters
- name_or_functionstr or callable
If a function, returns name_or_function(**kwargs).
If a string, and it matches the name of one of the defined filter functions, the corresponding function is called with **kwargs.
If a string, and it matches the name of a pre-computed filter, the corresponding filter is retrieved, and kwargs is ignored.
- Valid pre-computed filter names are:
‘kaiser_fast’
‘kaiser_best’
- Returns
- half_windownp.ndarray
The right wing of the interpolation filter
- precisionint > 0
The number of samples between zero-crossings of the filter
- rollofffloat > 0
The roll-off frequency of the filter as a fraction of Nyquist
- Raises
- NotImplementedError
If name_or_function cannot be found as a filter.
- resampy.filters.sinc_window(num_zeros=64, precision=9, window=None, rolloff=0.945)[source]¶
Construct a windowed sinc interpolation filter
- Parameters
- num_zerosint > 0
The number of zero-crossings to retain in the sinc filter
- precisionint > 0
The number of filter coefficients to retain for each zero-crossing
- windowcallable
The window function. By default, uses Blackman-Harris.
- rollofffloat > 0
The roll-off frequency (as a fraction of nyquist)
- Returns
- interp_window: np.ndarray [shape=(num_zeros * num_table + 1)]
The interpolation window (right-hand side)
- num_bits: int
The number of bits of precision to use in the filter table
- rollofffloat > 0
The roll-off frequency of the filter, as a fraction of Nyquist
- Raises
- TypeError
if window is not callable or None
- ValueError
if num_zeros < 1, precision < 1, or rolloff is outside the range (0, 1].
Examples
>>> # A filter with 10 zero-crossings, 32 samples per crossing, and a ... # Hann window for tapering. >>> halfwin, prec, rolloff = resampy.filters.sinc_window(num_zeros=10, precision=5, ... window=scipy.signal.hann) >>> halfwin array([ 9.450e-01, 9.436e-01, ..., -7.455e-07, -0.000e+00]) >>> prec 32 >>> rolloff 0.945
>>> # Or using sinc-window filter construction directly in resample >>> y = resampy.resample(x, sr_orig, sr_new, filter='sinc_window', ... num_zeros=10, precision=5, ... window=scipy.signal.hann)