Here is how to generate the Fourier transform of the sine wave in
Eq. () using the
numpy package in Python.
Let's do it in interactive mode. So start by running
Here is a line-by-line explanation of the script.
npas the short name of the package.
np.arange(N)generates a vector of integers ranging from
N-1. More generally,
np.arange(xfirst,xlast,xincr)generates a vector with sequential values starting at
xfirst, increasing by
xincrand ending just before
xlast. The numbers can be floats, but be careful of roundoff errors. So this command sets
tequal to the one-dimensional array, i.e. the vector, .
t, component by component, calculating . So
fis assigned to be a vector with eight elements. Try the command
print fto see the result.
np.fftpackage has a bunch of Fourier transform procedures. The one that actually does the Fourier transform is
np.fft.fft. The command performs the discrete Fourier transform on
fand assigns the result to
ft. The Fourier components
ft[m]belong to the discrete frequencies .