Monday, November 2, 2020

Fourier Transforms With scipy.fft: Python Signal Processing

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 scipy.fft Module#

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.

Install SciPy and Matplotlib#

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__)
/usr/local/lib/python3.6/dist-packages/scipy/__init__.py
>>> print(matplotlib.__file__)
/usr/local/lib/python3.6/dist-packages/matplotlib/__init__.py

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.

scipy.fft vs scipy.fftpack#

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

  1. scipy.fft
  2. scipy.fftpack

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.fft vs numpy.fft#

Read the full article at https://realpython.com/python-scipy-fft/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...