Monophonic resampling

The following code block demonstrates how to resample an audio signal.

We use librosa for loading the audio, but this is purely for ease of demonstration. resampy does not depend on librosa.

 1import librosa
 2import resampy
 3
 4# Load in librosa's example audio file at its native sampling rate
 5x, sr_orig = librosa.load(librosa.ex('trumpet'), sr=None)
 6
 7# x is now a 1-d numpy array, with `sr_orig` audio samples per second
 8
 9# We can resample this to any sampling rate we like, say 16000 Hz
10y_low = resampy.resample(x, sr_orig, 16000)
11
12# That's it!

Stereo and multi-dimensional data

The previous example operates on monophonic signals, but resampy also supports stereo resampling, as demonstrated below.

 1import librosa
 2import resampy
 3
 4# Load in librosa's example audio file at its native sampling rate.
 5# This time, also disable the stereo->mono downmixing
 6x, sr_orig = librosa.load(librosa.ex('trumpet', hq=True), sr=None, mono=False)
 7
 8# x is now a 2-d numpy array, with `sr_orig` audio samples per second
 9# The first dimension of x indexes the channels, the second dimension indexes
10# samples.
11# x[0] is the left channel, x[1] is the right channel.
12
13# We can again resample.  By default, resample assumes the last index is time.
14y_low = resampy.resample(x, sr_orig, 16000)
15
16# To be more explicit, provide a target axis
17y_low = resampy.resample(x, sr_orig, 16000, axis=1)

The next block illustrates resampling along an arbitrary dimension.

 1import numpy as np
 2import resampy
 3
 4# Generate 4-dimensional white noise.  The third axis (axis=2) will index time.
 5sr_orig = 22050
 6x = np.random.randn(10, 3, sr_orig * 5, 2)
 7
 8# x is now a 10-by-3-by-(5*22050)-by-2 tensor of data.
 9
10# We can resample along the time axis as follows
11y_low = resampy.resample(x, sr_orig, 11025, axis=2)
12
13# y_low is now a 10-by-3-(5*11025)-by-2 tensor of data

Integer-valued samples

Integer-valued inputs are supported, but because resampy interpolates between sample values, it will always produce a floating-point output. If you really need integer-valued outputs after resampling, you’ll have to cast the output array as demonstrated below.

 1 import numpy as np
 2 import resampy
 3
 4 sr_orig = 22050
 5
 6 # Create 5 seconds of random integer noise
 7 x = np.random.randint(-32768, high=32767, size=5*sr_orig, dtype=np.int16)
 8
 9 # resample, y will be floating-point type
10 y = resampy.resample(x, sr_orig, 11025)
11
12 # Cast back to match x's dtype
13 y_int = y.astype(x.dtype)

Advanced filtering

resampy allows you to control the design of the filters used in resampling operations.

 1import numpy as np
 2import scipy.signal
 3import librosa
 4import resampy
 5
 6# Load in some audio
 7x, sr_orig = librosa.load(librosa.ex('trumpet'), sr=None, mono=False)
 8
 9# Resample to 22050Hz using a Hann-windowed sinc-filter
10y = resampy.resample(x, sr_orig, sr_new, filter='sinc_window', window=scipy.signal.hann)
11
12# Or a shorter sinc-filter than the default (num_zeros=64)
13y = resampy.resample(x, sr_orig, sr_new, filter='sinc_window', num_zeros=32)
14
15# Or use the pre-built high-quality filter
16y = resampy.resample(x, sr_orig, sr_new, filter='kaiser_best')
17
18# Or use the pre-built fast filter
19y = resampy.resample(x, sr_orig, sr_new, filter='kaiser_fast')