python-scipyHow do I use the fftshift function in Python SciPy?
The fftshift function in Python SciPy allows you to shift the zero-frequency component of a Fourier Transform to the center of the array. This is useful for plotting the FFT in a more intuitive manner.
Example code
import numpy as np
from scipy.fftpack import fftshift
# Generate a test signal
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Perform FFT
f = np.fft.fft(y)
# Shift the FFT
fshift = fftshift(f)
The output of the above code is an array of complex numbers, fshift, which is the shifted Fourier Transform of the input signal.
Code explanation
import numpy as np: This imports the NumPy library, which is used for numerical computing.from scipy.fftpack import fftshift: This imports thefftshiftfunction from the SciPy library.x = np.linspace(0, 10, 100): This generates a test signal of 100 equally spaced points from 0 to 10.y = np.sin(x): This creates a sine wave from the test signal.f = np.fft.fft(y): This performs a Fourier Transform on the sine wave.fshift = fftshift(f): This shifts the zero-frequency component of the Fourier Transform to the center of the array.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python and SciPy to find the zeros of a function?
- How do I create a zero matrix using Python and Numpy?
- How do I use Scipy zeros in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python and SciPy to visualize data?
- How can I use Python and NumPy to find unique values in an array?
- How do I check the version of Python Numpy I am using?
- How do I uninstall Python Scipy?
- How can I check if a certain version of Python is compatible with SciPy?
See more codes...