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 thefftshift
function 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 use Python Scipy to fit a curve?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I integrate Scipy with Python?
- How can I use Python and Numpy to parse XML data?
- How do I create a numpy array of zeros using Python?
- How can I use RK45 with Python and SciPy?
- How do I use Scipy zeros in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
See more codes...