Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
About the Author
About the Reviewers
NumPy Functions' References

Time for action – calculating the Fourier transform

First, we will create a signal to transform. Calculate the Fourier transform with the following steps:

  1. Create a cosine wave with 30 points as follows:

    x =  np.linspace(0, 2 * np.pi, 30)
    wave = np.cos(x)
  2. Transform the cosine wave with the fft() function:

    transformed = np.fft.fft(wave)
  3. Apply the inverse transform with the ifft() function. It should approximately return the original signal. Check with the following line:

    print(np.all(np.abs(np.fft.ifft(transformed) - wave) < 10 ** -9))

    The result appears as follows:

  4. Plot the transformed signal with matplotlib:

    plt.title('Transformed cosine')

    The following resulting diagram shows the FFT result:

What just happened?

We applied the fft() function to a cosine wave. After applying the ifft() function, we got our signal back (see

from __future__ import print_function
import numpy as np
import matplotlib...