快速和非常慢 scipy.signal.resample 具有相同的输入大小
Both fast and very slow scipy.signal.resample with the same input size
根据scipy.signal.resample
的文档,速度应该根据输入的长度而变化:
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.
但我有非常不同的时间(因子 x14)具有相同的输入,并且所需的输出大小只有很小的变化:
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!
问题:我可以对输入进行零填充以获得 2 的幂(像往常一样加速 FFT 计算),但是由于我的重采样因子是固定的,我不能同时拥有两者输入大小的 2 次方和所需输出大小的 2 次方.
如何加速scipy.signal.resample
?
如果不可能,并且如果 scipy.signal.resample
的性能可以变化 如此之多 并且有很大的因素,那么它真的不适合实际使用。那么它对什么应用有用呢?
注意:我的目标是音频重采样(重调等)
编辑:最好的解决办法终于是.
文档字符串陈述了故事的一部分,有点误导。重采样过程包括 FFT(输入大小)、零填充和逆 FFT(输出大小)。因此,不方便的输出大小会像不方便的输入大小一样减慢速度。
Cris Luengo 建议在空间域中使用直接插值,这里应该更快。比如ndimage.zoom
就用它(默认三次样条插值):
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
与 resample 的输出不同(毕竟是不同的方法),但对于平滑数据(与此处使用的随机输入不同)它们应该接近。
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.
只是补充一点,这只是上采样的情况。下采样的过程是:FFT -> multiply -> iFFT -> downsample。所以在下采样中,FFT/iFFT 与输出大小无关,仅与输入大小有关。
根据scipy.signal.resample
的文档,速度应该根据输入的长度而变化:
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.
但我有非常不同的时间(因子 x14)具有相同的输入,并且所需的输出大小只有很小的变化:
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!
问题:我可以对输入进行零填充以获得 2 的幂(像往常一样加速 FFT 计算),但是由于我的重采样因子是固定的,我不能同时拥有两者输入大小的 2 次方和所需输出大小的 2 次方.
如何加速scipy.signal.resample
?
如果不可能,并且如果 scipy.signal.resample
的性能可以变化 如此之多 并且有很大的因素,那么它真的不适合实际使用。那么它对什么应用有用呢?
注意:我的目标是音频重采样(重调等)
编辑:最好的解决办法终于是
文档字符串陈述了故事的一部分,有点误导。重采样过程包括 FFT(输入大小)、零填充和逆 FFT(输出大小)。因此,不方便的输出大小会像不方便的输入大小一样减慢速度。
Cris Luengo 建议在空间域中使用直接插值,这里应该更快。比如ndimage.zoom
就用它(默认三次样条插值):
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
与 resample 的输出不同(毕竟是不同的方法),但对于平滑数据(与此处使用的随机输入不同)它们应该接近。
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.
只是补充一点,这只是上采样的情况。下采样的过程是:FFT -> multiply -> iFFT -> downsample。所以在下采样中,FFT/iFFT 与输出大小无关,仅与输入大小有关。