我正在尝试将这个'For cycle'替换为numpy矢量化操作,其主要思想是获得更好的性能,因为它将应用于大型数据集。我需要知道这是否可能,或者如果这个循环无法矢量化,我怎样才能获得更好的性能?
import numpy as np
import numpy.fft as fft
def compress(x1, x2, nfft):
return fft.ifft(np.conj(fft.fft(x1, nfft)) * fft.fft(x2, nfft))
def chirp_comp(bw, T, fs, f0=0, phi=0):
b = -bw / 2 + f0
a = (bw / 2 + f0 - b) / (2 * T)
tn = np.arange(0, T, 1 / fs, np.dtype(np.complex64))
y = np.exp(1j * 2 * np.pi * (a * np.power(tn, 2) + b * tn + phi / (2 * np.pi)))
return y, tn
(chirp_rg, t) = chirp_comp(250e6, 500e-6, 1500e6)
raw = np.random.rand(5, np.size(chirp_rg,0))
# Compress each range line
data_rg_compr = np.empty(raw.shape, dtype= np.dtype(np.complex64)) # Initialize matrix
#THIS IS THE CYCLE
for k in np.arange(np.size(raw, 0)):
aux = compress(chirp_rg, raw[k, :], np.size(chirp_rg, 0)) # Correlation
data_rg_compr[k, :] = aux[0:np.size(data_rg_compr, 1)] # Save result for line k
print "ends"