Time for action – calculating the Fourier transform

First, we will create a signal to transform. In order to calculate the Fourier transform, perform 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.
    print np.all(np.abs(np.fft.ifft(transformed) - wave) < 10 ** -9)

    The result is shown as follows:

    True
    
  4. Plot the transformed signal with Matplotlib.
    plot(transformed)
    show()

    The resulting screenshot shows the fast Fourier transform:

    Time for action – calculating the Fourier transform

What just happened?

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

import numpy as np
from matplotlib.pyplot import plot, show

x =  np.linspace(0, 2 * np.pi, 30)
wave = np.cos(x)
transformed = np.fft.fft(wave)
print np.all(np.abs(np.fft.ifft(transformed) - wave) < 10 ** -9)

plot(transformed)
show()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset