
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - fftshift() Function
The fftshift() function in SciPy moves the zero-frequency part of a Fourier transform to the middle of the spectrum. FFT puts the zero-frequency part at the beginning of the output, but to analyze and visualize, it makes more sense to have it in the center. fftshift() method reorganizes the data to do this.
In signal or image processing, after performing an FFT, it's common to use fftshift for visualization. For example in spectogram using fftshift puts the spectrum in the middle, which helps you tell high and low frequencies by centering the spectrum.
It's also useful in physics when you're looking at diffraction patterns. By moving the zero frequency to the center, you can examine patterns around the origin more. This makes the whole analysis process smoother.
Syntax
Following is the syntax of the SciPy fftshift() method −
.fftshift(x, axes=None)
Parameters
Following are the parameters of this method −
x (array_like) − The input array (e.g., a Fourier-transformed signal or image).
axes(optional) − The axes along which the shift is performed. By default, it shifts along all axes. when axes = 0, shifts the zero frequency component along the rows (vertical axis) and when, axes = 1, shifts the zero frequency component along the columns (horizontal axis).
Return Value
The fftshift() method returns a new array where the zero-frequency component is centered.
Example 1: Shiffting 1D Frequencies
This is the basic example of how fftshift() transforms a 1D frequency array, it shifts the zero-frequency component from the start of the spectrum to its center. Following is the code −
import numpy as np data = np.fft.fftfreq(8, 0.2) print("Original Frequencies:", data) shifted_freq = np.fft.fftshift(data) print("Shifted Frequencies:", shifted_freq)
Output of the above code is as follows −
Original Frequencies: [ 0. 0.625 1.25 1.875 -2.5 -1.875 -1.25 -0.625] Shifted Frequencies: [-2.5 -1.875 -1.25 -0.625 0. 0.625 1.25 1.875]
Example 2: Shifting 2D FFT Frequencies
This example applies fftshift() to a 2D FFT result, shifting the zero-frequency component in order to better present frequency data in a 2D array. Following is the code −
import numpy as np from scipy.fft import fft2, fftshift arr_2d = np.arange(16).reshape(4, 4) print("image is :\n", arr_2d) fft = fft2(arr_2d) shifted_result = fftshift(fft) print("\nFFT Result (Before Shift):\n", np.round(fft.real, 2)) print("\nFFT Result (After Shift):\n", np.round(shifted_result.real, 2))
Output of the above code is as follows −
image is : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] FFT Result (Before Shift): [[120. -8. -8. -8.] [-32. 0. 0. 0.] [-32. 0. 0. 0.] [-32. 0. 0. 0.]] FFT Result (After Shift): [[ 0. 0. -32. 0.] [ 0. 0. -32. 0.] [ -8. -8. 120. -8.] [ 0. 0. -32. 0.]]
Example 3: fftshift() using axes parameter
in the below code let us create a 4 * 4 matrix using np.fft.fftfreq with 16 points. Then by using fftshift(freqs, axes=1) we shift the frequency components along the rows, centering the zero-frequency components.
import numpy as np freqs = np.fft.fftfreq(16, d=1./16).reshape(4, 4) # Apply fftshift along rows (axes=0) shifted_freqs = np.fft.fftshift(freqs, axes=1) print("Original Frequency Matrix:") print(freqs) print("\nShifted Frequency Matrix:") print(shifted_freqs)
Output of the above code is as follows −
Original Frequency Matrix: [[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [-8. -7. -6. -5.] [-4. -3. -2. -1.]] Shifted Frequency Matrix: [[ 2. 3. 0. 1.] [ 6. 7. 4. 5.] [-6. -5. -8. -7.] [-2. -1. -4. -3.]]