Audio and sensor pipelines combine signals recorded at different rates. SciPy's resample_poly converts evenly spaced samples to a new sample rate using an up/down ratio and an antialiasing filter.

Passing the target rate as up and the input rate as down converts 800 Hz to 1200 Hz as a 3/2 ratio, so 200 input samples become 300 output samples over the same quarter-second interval.

The default boundary treatment assumes zeros beyond both ends, which can affect values near the edges. scipy.signal.resample() is a separate Fourier method that assumes a periodic signal and takes a target sample count; resample_poly takes integer upsampling and downsampling factors.

Steps to resample a signal with SciPy resample_poly:

Python with NumPy and SciPy installed is required.
Related: Install SciPy in a Python virtual environment

  1. Create signal_resample.py with the sample rates and evenly spaced input signal.
    signal_resample.py
    import numpy as np
    from scipy.signal import resample_poly
     
    input_rate = 800
    target_rate = 1200
    duration = 0.25
     
    time = np.arange(0, duration, 1 / input_rate)
    samples = (
        np.sin(2 * np.pi * 40 * time)
        + 0.25 * np.sin(2 * np.pi * 120 * time)
    )
  2. Append the polyphase resampling call to signal_resample.py.
    resampled = resample_poly(samples, up=target_rate, down=input_rate)

    resample_poly() applies its low-pass FIR filter during the 3/2 conversion. For a multidimensional array, axis= selects the dimension containing consecutive time samples.

  3. Append a line that prints the input and output sample counts to signal_resample.py.
    print(samples.size, resampled.size)
  4. Run signal_resample.py to confirm the sample count.
    $ python3 signal_resample.py
    200 300

    The 300 output samples at 1200 Hz cover the same quarter second as the 200 input samples at 800 Hz.

Share this guide