Both fast and very slow scipy.signal.resample with

2019-08-18 22:47发布

问题:

According to the documentation of scipy.signal.resample, the speed should vary according to the length of input:

As noted, resample uses FFT transformations, which can be very slow if the number of input samples is large and prime, see scipy.fftpack.fft.

But I have very different timings (factor x14) with the same input, and only a small variation of desired output size:

import numpy as np, time
from scipy.signal import resample

x = np.random.rand(262144, 2)
y = np.random.rand(262144, 2)

t0 = time.time()
resample(x, 233543, axis=0)
print time.time() - t0          # 2.9 seconds here

t0 = time.time()
resample(y, 220435, axis=0)
print time.time() - t0          # 40.9 seconds here!

Question: I can zero-pad the input to have a power of 2 (to speed up FFT computations, as usual), but as my resampling factor is fixed, I can't have both a power of 2 for the input size and a power of 2 for the desired output size.

How to speed up scipy.signal.resample?

If not possible, and if scipy.signal.resample's performance can vary so much with a large factor, it makes it really not handy for real use. Then for which application is it useful?

Note: my goal is audio resampling (repitching, etc.)

Edit: the best solution is finally to use this.

回答1:

The docstring, somewhat misleadingly, states one part of the story. The resampling process consists of FFT (input size), zero-padding, and inverse FFT (output size). So an inconvenient output size will slow it down just as much as an inconvenient input size will.

Cris Luengo suggested using direct interpolation in the spatial domain, which should be faster here. For example, ndimage.zoom uses it (cubic spline interpolation by default):

from scipy.ndimage import zoom
t0 = time.time()
zoom(y, (220435./262144., 1))   # maybe with prefilter=False ? up to you
print(time.time() - t0)         # about 200 times faster than resample

Not the same output as resample (a different method after all), but for smooth data (unlike random input used here) they should be close.



回答2:

The resampling process consists of FFT (input size), zero-padding, and inverse FFT (output size). So an inconvenient output size will slow it down just as much as an inconvenient input size will.

just to add that this is the case for upsampling only. for downsampling the process is: FFT -> multiply -> iFFT -> downsample. so in downsampling, the FFT/iFFT has nothing to do with the output size, only the input size.