The Fourier transform is a powerful tool for analyzing signals and is used in everything from audio processing to image compression. SciPy provides a mature implementation in its scipy.fft module, and in this tutorial, you’ll learn how to use it.

The scipy.fft module may look intimidating at first since there are many functions, often with similar names, and the documentation uses a lot of technical terms without explanation. The good news is that you only need to understand a few core concepts to start using the module.

Don’t worry if you’re not comfortable with math! You’ll get a feel for the algorithm through concrete examples, and there will be links to further resources if you want to dive into the equations. For a visual introduction to how the Fourier transform works, you might like 3Blue1Brown’s video.

In this tutorial, you’ll learn:

  • How and when to use the Fourier transform
  • How to select the correct function from scipy.fft for your use case
  • How to view and modify the frequency spectrum of a signal
  • Which different transforms are available in scipy.fft

If you’d like a summary of this tutorial to keep after you finish reading, then download the cheat sheet below. It has explanations of all the functions in the scipy.fft module as well as a breakdown of the different types of transform that are available:

The Fourier transform is a crucial tool in many applications, especially in scientific computing and data science. As such, SciPy has long provided an implementation of it and its related transforms. Initially, SciPy provided the scipy.fftpack module, but they have since updated their implementation and moved it to the scipy.fft module.

SciPy is packed full of functionality. For a more general introduction to the library, check out Scientific Python: Using SciPy for Optimization.

Before you can get started, you’ll need to install SciPy and Matplotlib. You can do this one of two ways:

  1. Install with Anaconda: Download and install the Anaconda Individual Edition. It comes with SciPy and Matplotlib, so once you follow the steps in the installer, you’re done!

  2. Install with pip: If you already have pip installed, then you can install the libraries with the following command:

    $ python -m pip install -U scipy matplotlib

You can verify the installation worked by typing python in your terminal and running the following code:

>>> import scipy, matplotlib
>>> print(scipy.__file__)
>>> print(matplotlib.__file__)

This code imports SciPy and Matplotlib and prints the location of the modules. Your computer will probably show different paths, but as long as it prints a path, the installation worked.

SciPy is now installed! Now it’s time to take a look at the differences between scipy.fft and scipy.fftpack.

When looking at the SciPy documentation, you may come across two modules that look very similar:

The scipy.fft module is newer and should be preferred over scipy.fftpack. You can read more about the change in the release notes for SciPy 1.4.0, but here’s a quick summary:

  • scipy.fft has an improved API.
  • scipy.fft enables using multiple workers, which can provide a speed boost in some situations.
  • scipy.fftpack is considered legacy, and SciPy recommends using scipy.fft instead.

Unless you have a good reason to use scipy.fftpack, you should stick with scipy.fft.

SciPy’s fast Fourier transform (FFT) implementation contains more features and is more likely to get bug fixes than NumPy’s implementation. If given a choice, you should use the SciPy implementation.

NumPy maintains an FFT implementation for backward compatibility even though the authors believe that functionality like Fourier transforms is best placed in SciPy. See the SciPy FAQ for more details.

Fourier analysis is a field that studies how a mathematical function can be decomposed into a series of simpler trigonometric functions. The Fourier transform is a tool from this field for decomposing a function into its component frequencies.

Okay, that definition is pretty dense. For the purposes of this tutorial, the Fourier transform is a tool that allows you to take a signal and see the power of each frequency in it. Take a look at the important terms in that sentence:

  • A signal is information that changes over time. For example, audio, video, and voltage traces are all examples of signals.
  • A frequency is the speed at which something repeats. For example, clocks tick at a frequency of one hertz (Hz), or one repetition per second.
  • Power, in this case, just means the strength of each frequency.

The following image is a visual demonstration of frequency and power on some sine waves:

Low frequency, high frequency and low power sine waves

The peaks of the high-frequency sine wave are closer together than those of the low-frequency sine wave since they repeat more frequently. The low-power sine wave has smaller peaks than the other two sine waves.

To make this more concrete, imagine you used the Fourier transform on a recording of someone playing three notes on the piano at the same time. The resulting frequency spectrum would show three peaks, one for each of the notes. If the person played one note more softly than the others, then the power of that note’s frequency would be lower than the other two.

Here’s what that piano example would look like visually:

Piano keys corresponding to peaks in a frequency spectrum

The highest note on the piano was played quieter than the other two notes, so the resulting frequency spectrum for that note has a lower peak.

The Fourier transform is useful in many applications. For example, Shazam and other music identification services use the Fourier transform to identify songs. JPEG compression uses a variant of the Fourier transform to remove the high-frequency components of images. Speech recognition uses the Fourier transform and related transforms to recover the spoken words from raw audio.

In general, you need the Fourier transform if you need to look at the frequencies in a signal. If working with a signal in the time domain is difficult, then using the Fourier transform to move it into the frequency domain is worth trying. In the next section, you’ll look at the differences between the time and frequency domains.

Throughout the rest of the tutorial, you’ll see the terms time domain and frequency domain. These two terms refer to two different ways of looking at a signal, either as its component frequencies or as information that varies over time.

In the time domain, a signal is a wave that varies in amplitude (y-axis) over time (x-axis). You’re most likely used to seeing graphs in the time domain, such as this one:

An audio signal in the time domain

This is an image of some audio, which is a time-domain signal. The horizontal axis represents time, and the vertical axis represents amplitude.

In the frequency domain, a signal is represented as a series of frequencies (x-axis) that each have an associated power (y-axis). The following image is the above audio signal after being Fourier transformed:

An audio signal in the frequency domain

Here, the audio signal from before is represented by its constituent frequencies. Each frequency along the bottom has an associated power, producing the spectrum that you see.

For more information on the frequency domain, check out the DeepAI glossary entry.

The Fourier transform can be subdivided into different types of transform. The most basic subdivision is based on the kind of data the transform operates on: continuous functions or discrete functions. This tutorial will deal with only the discrete Fourier transform (DFT).

You’ll often see the terms DFT and FFT used interchangeably, even in this tutorial. However, they aren’t quite the same thing. The fast Fourier transform (FFT) is an algorithm for computing the discrete Fourier transform (DFT), whereas the DFT is the transform itself.

Another distinction that you’ll see made in the scipy.fft library is between different types of input. fft() accepts complex-valued input, and rfft() accepts real-valued input. Skip ahead to the section Using the Fast Fourier Transform (FFT) for an explanation of complex and real numbers.

Two other transforms are closely related to the DFT: the discrete cosine transform (DCT) and the discrete sine transform (DST). You’ll learn about those in the section The Discrete Cosine and Sine Transforms.

A tutorial on the scipy.fft module wouldn’t be complete without looking at the discrete cosine transform (DCT) and the discrete sine transform (DST). These two transforms are closely related to the Fourier transform but operate entirely on real numbers. That means they take a real-valued function as an input and produce another real-valued function as an output.

SciPy implements these transforms as dct() and dst(). The i* and *n variants are the inverse and n-dimensional versions of the functions, respectively.

The DCT and DST are a bit like two halves that together make up the Fourier transform. This isn’t quite true since the math is a lot more complicated, but it’s a useful mental model.

So if the DCT and DST are like halves of a Fourier transform, then why are they useful?

For one thing, they’re faster than a full Fourier transform since they effectively do half the work. They can be even faster than rfft(). On top of this, they work entirely in real numbers, so you never have to worry about complex numbers.

Before you can learn how to choose between them, you need to understand even and odd functions. Even functions are symmetrical about the y-axis, whereas odd functions are symmetrical about the origin. To imagine this visually, take a look at the following diagrams:

An example of even and odd functions

You can see that the even function is symmetrical about the y-axis. The odd function is symmetrical about y = -x, which is described as being symmetrical about the origin.

When you calculate a Fourier transform, you pretend that the function you’re calculating it on is infinite. The full Fourier transform (DFT) assumes the input function repeats itself infinitely. However, the DCT and DST assume the function is extended through symmetry. The DCT assumes the function is extended with even symmetry, and the DST assumes it’s extended with odd symmetry.

The following image illustrates how each transform imagines the function extends to infinity:

Diagram of how different discrete transforms extend functions

In the above image, the DFT repeats the function as is. The DCT mirrors the function vertically to extend it, and the DST mirrors it horizontally.

Note that the symmetry implied by the DST leads to big jumps in the function. These are called discontinuities and produce more high-frequency components in the resulting frequency spectrum. So unless you know your data has odd symmetry, you should use the DCT instead of the DST.

The DCT is very commonly used. There are many more examples, but the JPEG, MP3, and WebM standards all use the DCT.

The Fourier transform is a powerful concept that’s used in a variety of fields, from pure math to audio engineering and even finance. You’re now familiar with the discrete Fourier transform and are well equipped to apply it to filtering problems using the scipy.fft module.

In this tutorial, you learned:

  • How and when to use the Fourier transform
  • How to select the correct function from scipy.fft for your use case
  • What the differences are between the time domain and the frequency domain
  • How to view and modify the frequency spectrum of a signal
  • How using rfft() can speed up your code

In the last section, you also learned about the discrete cosine transform and the discrete sine transform. You saw what functions to call to use them, and you learned when to use one over the other.

If you’d like a summary of this tutorial, then you can download the cheat sheet below. It has explanations of all the functions in the scipy.fft module as well as a breakdown of the different types of transform that are available:

Keep exploring this fascinating topic and experimenting with transforms, and be sure to share your discoveries in the comments below!