pythonnumpyfftdftcontinuous-fourier

Fourier Transformation vs Numpy FFT


This is probably a very naive question but here it is.

I want to calculate Fourier transform of a function f(x). So I define a numpy array X and pass through vectorized function f. Now if I calculate the FFT of this array f(X) it does not come out to be Fourier Transform of f(x) as it would if I do it on a piece of paper. For example If I calculate FFT of Gaussian I should get a Gaussian or an array whose real part would resemble a Gaussian very closely.

here is the code. please let me know what I have to change to get the usual Fourier Transform.

import matplotlib.pyplot as plt
import numpy as np

N = 128
x = np.linspace(-5, 5, N)
y = np.exp(-x**2)

y_fft = np.fft.fftshift(np.fft.fft(y).real)
plt.plot(x, y_fft)

plt.show()

let me reiterate. I want to calculate Fourier transform of any function (e.g. gaussian). FFT is way to calculate Fourier transform of an array of numbers but this is not same as simple discretization of continuous Fourier transform formula.


Solution

  • No, the FFT is not a way to calculate the Fourier transform (FT) of a function. The FFT is a fast algorithm to calculate the DFT, discrete Fourier transform of an array of samples. This array of samples can be interpretated as the sampling of a function at equi-spaced points.

    The DFT and the FT are 2 different things, and you can't use the DFT to calculate the FT. See this link on their differences.

    If your function is periodic, then its spectrum is a function defined only at points, and you can use the DFT on equi-spaced samples of the function to infer the FT with good success, if you choose very carefully your domain and sampling rate, and the domain is a multiple of all the periods of all the harmonics of your function.