Chapter 4. Frequency and the Fast Fourier Transform
If you want to find the secrets of the universe, think in terms of energy, frequency and vibration.
Nikola Tesla
This chapter was written in collaboration with SW’s father, PW van der Walt.
This chapter will depart slightly from the format of the rest of the book. In particular, you may find the code in the chapter quite modest. Instead, we want to illustrate an elegant algorithm, the Fast Fourier Transform (FFT), that is endlessly useful, is implemented in SciPy, and works, of course, on NumPy arrays.
Introducing Frequency
We’ll start by setting up some plotting styles and importing the usual suspects:
# Make plots appear inline, set custom plotting style%matplotlibinlineimportmatplotlib.pyplotaspltplt.style.use('style/elegant.mplstyle')
importnumpyasnp
The discrete1 Fourier transform (DFT) is a mathematical technique used to convert temporal or spatial data into frequency domain data. Frequency is a familiar concept, due to its colloquial occurrence in the English language: the lowest notes your headphones can rumble out are around 20 Hz, whereas middle C on a piano lies around 261.6 Hz; Hertz, or oscillations per second, in this case literally refers to the number of times per second at which the membrane inside the headphone moves to-and-fro. That, in turn, creates compressed pulses of air which, upon arrival at your eardrum, induces a vibration at the same frequency. So, if you take a simple periodic function, ...