Numpy/Scipy 中的卷积计算
对我正在进行的一些计算工作进行分析表明,我的程序中的一个瓶颈是一个基本上执行此操作的函数(np
是 numpy
、sp
是 scipy
):
def mix1(signal1, signal2):
spec1 = np.fft.fft(signal1, axis=1)
spec2 = np.fft.fft(signal2, axis=1)
return np.fft.ifft(spec1*spec2, axis=1)
两个信号都具有形状 (C, N)
,其中 C
是数据集的数量(通常小于 20), N
是每个中的样本数集(5000左右)。每个集合(行)的计算完全独立于任何其他集合。
我认为这只是一个简单的卷积,所以我尝试将其替换为:
def mix2(signal1, signal2):
outputs = np.empty_like(signal1)
for idx, row in enumerate(outputs):
outputs[idx] = sp.signal.convolve(signal1[idx], signal2[idx], mode='same')
return outputs
...只是为了看看是否得到相同的结果。但我没有,我的问题是:
- 为什么不呢?
- 有没有更好的方法来计算 mix1() 的等价物?
(我意识到 mix2 可能不会更快,但它可能是并行化的一个很好的起点。)
这是我用来快速检查这一点的完整脚本:
import numpy as np
import scipy as sp
import scipy.signal
N = 4680
C = 6
def mix1(signal1, signal2):
spec1 = np.fft.fft(signal1, axis=1)
spec2 = np.fft.fft(signal2, axis=1)
return np.fft.ifft(spec1*spec2, axis=1)
def mix2(signal1, signal2):
outputs = np.empty_like(signal1)
for idx, row in enumerate(outputs):
outputs[idx] = sp.signal.convolve(signal1[idx], signal2[idx], mode='same')
return outputs
def test(num, chans):
sig1 = np.random.randn(chans, num)
sig2 = np.random.randn(chans, num)
res1 = mix1(sig1, sig2)
res2 = mix2(sig1, sig2)
np.testing.assert_almost_equal(res1, res2)
if __name__ == "__main__":
np.random.seed(0x1234ABCD)
test(N, C)
Profiling some computational work I'm doing showed me that one bottleneck in my program was a function that basically did this (np
is numpy
, sp
is scipy
):
def mix1(signal1, signal2):
spec1 = np.fft.fft(signal1, axis=1)
spec2 = np.fft.fft(signal2, axis=1)
return np.fft.ifft(spec1*spec2, axis=1)
Both signals have shape (C, N)
where C
is the number of sets of data (usually less than 20) and N
is the number of samples in each set (around 5000). The computation for each set (row) is completely independent of any other set.
I figured that this was just a simple convolution, so I tried to replace it with:
def mix2(signal1, signal2):
outputs = np.empty_like(signal1)
for idx, row in enumerate(outputs):
outputs[idx] = sp.signal.convolve(signal1[idx], signal2[idx], mode='same')
return outputs
...just to see if I got the same results. But I didn't, and my questions are:
- Why not?
- Is there a better way to compute the equivalent of
mix1()
?
(I realise that mix2
probably wouldn't have been faster as-is, but it might have been a good starting point for parallelisation.)
Here's the full script I used to quickly check this:
import numpy as np
import scipy as sp
import scipy.signal
N = 4680
C = 6
def mix1(signal1, signal2):
spec1 = np.fft.fft(signal1, axis=1)
spec2 = np.fft.fft(signal2, axis=1)
return np.fft.ifft(spec1*spec2, axis=1)
def mix2(signal1, signal2):
outputs = np.empty_like(signal1)
for idx, row in enumerate(outputs):
outputs[idx] = sp.signal.convolve(signal1[idx], signal2[idx], mode='same')
return outputs
def test(num, chans):
sig1 = np.random.randn(chans, num)
sig2 = np.random.randn(chans, num)
res1 = mix1(sig1, sig2)
res2 = mix2(sig1, sig2)
np.testing.assert_almost_equal(res1, res2)
if __name__ == "__main__":
np.random.seed(0x1234ABCD)
test(N, C)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以我对此进行了测试,现在可以确认一些事情:
1)numpy.convolve 不是循环的,这就是 fft 代码为您提供的:
2)FFT 不会在内部填充到 2 的幂。比较截然不同的速度以下运算的结果:
3) 归一化没有区别——如果您通过将 a(k)*b(ik) 相加来进行朴素循环卷积,您将得到 FFT 代码的结果。
填充到 2 的幂将会改变答案。我听说有一些方法可以通过巧妙地使用长度的素因数来解决这个问题(在数值食谱中提到但没有编码),但我从未见过人们真正这样做。
So I tested this out and can now confirm a few things:
1) numpy.convolve is not circular, which is what the fft code is giving you:
2) FFT does not internally pad to a power of 2. Compare the vastly different speeds of the following operations:
3) Normalization is not a difference -- if you do a naive circular convolution by adding up a(k)*b(i-k), you will get the result of the FFT code.
The thing is padding to a power of 2 is going to change the answer. I've heard tales that there are ways to deal with this by cleverly using prime factors of the length (mentioned but not coded in Numerical Recipes) but I've never seen people actually do that.
scipy.signal.fftconvolve 通过 FFT 进行卷积,它是 python 代码。您可以研究源代码,并纠正您的 mix1 函数。
scipy.signal.fftconvolve does convolve by FFT, it's python code. You can study the source code, and correct you mix1 function.
如前所述,scipy.signal.convolve 函数不执行循环卷积。如果您想在实空间中执行循环卷积(与使用 fft 相比),我建议使用 scipy.ndimage.convolve 函数。它有一个模式参数,可以设置为“wrap”,使其成为循环卷积。
As mentioned before, the scipy.signal.convolve function does not perform a circular convolution. If you want a circular convolution performed in realspace (in contrast to using fft's) I suggest using the scipy.ndimage.convolve function. It has a mode parameter which can be set to 'wrap' making it a circular convolution.