【发布时间】:2021-06-09 12:49:35
【问题描述】:
我正在寻找一种有效的方法来执行以下操作;这是一个最小的工作代码:
import numpy as np
from scipy.signal import fftconvolve
n = 7
m = 100
N = 3000
a = np.random.rand( n,m,N ) + np.random.rand( n,m,N )*1j
b = np.random.rand( n,m,N ) + np.random.rand( n,m,N )*1j
# we want product over the n-dimension, with fftconvolve in the m-indices elementwise
old_shape= a.shape
new_shape= ( n*m, N )
a = a.reshape( new_shape )
for i in range( n ):
b_tiled = np.tile( b[ i, :, : ], ( n, 1, 1 )).reshape( new_shape )
result = ( fftconvolve( b_tiled, a, mode="same", axes=-1 ) ).reshape( old_shape )
result = result.sum( axis=0 )
该操作在第一个索引中以类似乘积的方式计算两个数组的 FFT(因此我避免了 range(n) 索引上的双循环,仅使用一个)。
【问题讨论】:
标签: python-3.x numpy scipy