具有相同输入大小的快速和非常慢的scipy.signal.resample [英] Both fast and very slow scipy.signal.resample with the same input size

查看:245
本文介绍了具有相同输入大小的快速和非常慢的scipy.signal.resample的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 scipy.signal.resample ,速度应根据输入的长度 :

如前所述,重采样使用FFT转换,如果输入采样的数量较大且质数较大,则转换速度可能非常慢,请参阅scipy.fftpack.fft.

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)非常不同,并且期望输出尺寸只有很小的变化:

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!

问题:我可以将输入零填充为2的幂(以照常加速FFT计算),但是由于我的重采样因子是固定的,所以我不能同时具有2的幂表示输入大小,2的幂表示所需的输出大小..

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.

如何加快scipy.signal.resample?

How to speed up scipy.signal.resample?

如果不可能,并且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.)

最好的解决方案最终是使用此

the best solution is finally to use this.

推荐答案

文档字符串有些误导性地陈述了故事的一部分.重采样过程由FFT(输入大小),零填充和逆FFT(输出大小)组成.因此,不便的输出大小将使其速度降低,与不便的输入大小一样.

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建议在空间域中使用直接插值,此处应更快.例如, ndimage.zoom 使用它(默认情况下,三次样条插值):

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.

这篇关于具有相同输入大小的快速和非常慢的scipy.signal.resample的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆