[Numpy-discussion] Optical autocorrelation calculated with numpy is slow
João Luís Silva
jsilva at fc.up.pt
Mon Mar 30 14:13:17 EDT 2009
Hi,
I wrote a script to calculate the *optical* autocorrelation of an
electric field. It's like the autocorrelation, but sums the fields
instead of multiplying them. I'm calculating
I(tau) = integral( abs(E(t)+E(t-tau))**2,t=-inf..inf)
with script appended at the end. It's too slow for my purposes (takes ~5
seconds, and scales ~O(N**2)). numpy's correlate is fast enough, but
isn't what I need as it multiplies instead of add the fields. Could you
help me get this script to run faster (without having to write it in
another programming language) ?
Thanks,
João Silva
#--------------------------------------------------------
import numpy as np
#import matplotlib.pyplot as plt
n = 2**12
n_autocorr = 3*n-2
c = 3E2
w0 = 2.0*np.pi*c/800.0
t_max = 100.0
t = np.linspace(-t_max/2.0,t_max/2.0,n)
E = np.exp(-(t/10.0)**2)*np.exp(1j*w0*t) #Electric field
dt = t[1]-t[0]
t_autocorr=np.linspace(-dt*n_autocorr/2.0,dt*n_autocorr/2.0,n_autocorr)
E1 = np.zeros(n_autocorr,dtype=E.dtype)
E2 = np.zeros(n_autocorr,dtype=E.dtype)
Ac = np.zeros(n_autocorr,dtype=np.float64)
E2[n-1:n-1+n] = E[:]
for i in range(2*n-2):
E1[:] = 0.0
E1[i:i+n] = E[:]
Ac[i] = np.sum(np.abs(E1+E2)**2)
Ac *= dt
#plt.plot(t_autocorr,Ac)
#plt.show()
#--------------------------------------------------------
More information about the NumPy-Discussion
mailing list