Time for action – shifting frequencies

We will create a signal, transform it, and then shift the signal. In order to shift the frequencies, perform the following steps:

  1. Create a cosine wave with 30 points.
    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. Shift the signal with the fftshift function.
    shifted = np.fft.fftshift(transformed)
  4. Reverse the shift with the ifftshift function. This should undo the shift.
    print np.all((np.fft.ifftshift(shifted) - transformed) < 10 ** -9)

    The result is shown as follows:

    True
    
  5. Plot the signal and transform it with Matplotlib.
    plot(transformed, lw=2)
    plot(shifted, lw=3)
    show()

    The following screenshot shows the shift in the fast Fourier transform:

    Time for action – shifting frequencies

What just happened?

We applied the fftshift function to a cosine wave. After applying the ifftshift function, we got our signal back (see fouriershift.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)
shifted = np.fft.fftshift(transformed)
print np.all(np.abs(np.fft.ifftshift(shifted) - transformed) < 10 ** -9)

plot(transformed, lw=2)
plot(shifted, lw=3)
show()
..................Content has been hidden....................

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