title: Proving Fourier transform operation in Python
body: <p>I have an expression in the time domain</p>
  
  <pre><code>f = -1j*H(t) * exp(-(1j*a+b)*t)
  </code></pre>
  
  <p>which can be Fourier transformed analytically using <a href="https://en.wikipedia.org/wiki/Fourier_transform#Tables_of_important_Fourier_transforms" rel="nofollow noreferrer">known properties</a> (<code>H</code> is the Heaviside step function). The result of this FT operation is</p>
  
  <pre><code>F = (w-a-1j*b)/((w-a)**2+b**2)
  </code></pre>
  
  <p>where <code>w</code> is frequency.</p>
  
  <p>Now I'm using the tips in <a href="http://www.cbcity.de/die-fft-mit-python-einfach-erklaert" rel="nofollow noreferrer">this article</a> to do numerical Fourier transform on <code>f</code> in Python, and confirm that I do get the same analytical result <code>F</code>:</p>
  
  <pre><code>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&gt;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()
  </code></pre>
  
  <p>However Python's FFT function seems to give me something completely wrong. This is evident when <code>F</code> and <code>F2</code> are plotted.</p>
  
  <p><strong>Edit:</strong> Here are the plots...</p>
  
  <p><a href="https://i.stack.imgur.com/FFnRp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FFnRp.png" alt=""></a></p>
  
  <p><a href="https://i.stack.imgur.com/iRj9T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iRj9T.png" alt=""></a></p>
  
  <p>It's not obvious in these figures, but if you zoom in around the <code>w=-10</code> and <code>10</code> areas, there are small oscillations, possibly due to the <code>fft</code> algorithm.</p>