I was testing the .fft package of numpy 1.16.1 in Python 3.7.2. In particular I was trying to verify that the transform resembles the analytical one for: $$f(x) = \mathrm{exp}\left[-\left(\frac{x-5}{2}\right)^{2}\right]$$
I get from Wolfram Alpha that $\hat{f} = \mathcal{F}[f]$ looks like this:
Then I tried to replicate this plot with numpy and matplotlib, with the following code:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 1/1000)
y = np.exp(-((x-5)**2)/4)
y_hat = np.fft.fftshift(np.fft.fft(y))
re_y_hat = np.real(y_hat)
im_y_hat = np.imag(y_hat)
fig, ax = plt.subplots()
ax.plot(x, re_y_hat, "b-", x, im_y_hat, "r-")
plt.show()
plt.close()
But the image I obtain differs greatly from the one Wolfram gives:
In the last image the zero frequecy was shifted to the center by using np.fft.fftshift()
so the spike corresponds to frequency zero.
I already figured out that the problem is that nowhere in np.fft.fft()
is the $\Delta x$ being specified, so what numpy is interpreting is that I have data that varies very slowly, almost constant$^{1}$, and thus the transform is close to that of a constant function.
I looked at the numpy documentation and other SE posts to see how this can be fixed but found nothing. Does anyone know how to fix this?
$^{1}$ We can calculate the average slope of the function numpy sees by $\frac{\mathrm{max}\{f\}-\mathrm{min}\{f\}}{x_{f_{\mathrm{max}}}-x_{f_{\mathrm{min}}}} = \frac{f(5)-f(0)}{n\Delta x} \approx \frac{1}{n\Delta x}$ where $n$ is the number of nodes separating the maximum from the minumum. In this case, since numpy takes $\Delta x = 1$ by default, the slope is about 1/5000=0.0002