I have an expression in the time domain
f = -1j*H(t) * exp(-(1j*a+b)*t)
which can be Fourier transformed analytically using known properties (H
is the Heaviside step function). The result of this FT operation is
F = (w-a-1j*b)/((w-a)**2+b**2)
where w
is frequency.
Now I'm using the tips in this article to do numerical Fourier transform on f
in Python, and confirm that I do get the same analytical result F
:
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(-10,10,1e4) # time
w = np.linspace(-10,10,1e4) # frequency
b = 0.1
a = 1
H = lambda x: 1*(x>0) # heaviside function
# function in time
f = -1j*H(t)*np.exp(-(1j*a+b)*t)
# function in frequency (analytical work)
F = (w-a-1j*b)/((w-a)**2+b**2)
hann = np.hanning(len(t)) # hanning window
# function in frequency (numerical work)
F2 = 2/len(t)*np.fft.fft(hann*f)
plt.figure()
plt.plot(w,F.real,'b',label='analytical')
plt.plot(w,F2.real,'b--',label='fft')
plt.xlabel(r'$\omega$')
plt.ylabel(r'Re($F(\omega)$)')
plt.legend(loc='best')
plt.figure()
plt.plot(w,F.imag,'g',label='analytical')
plt.plot(w,F2.imag,'g--',label='fft')
plt.xlabel(r'$\omega$')
plt.ylabel(r'Im($F(\omega)$)')
plt.legend(loc='best')
plt.show()
However Python's FFT function seems to give me something completely wrong. This is evident when F
and F2
are plotted.
Edit: Here are the plots...
It's not obvious in these figures, but if you zoom in around the w=-10
and 10
areas, there are small oscillations, possibly due to the fft
algorithm.