
- 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 - fft() Function
The scipy.fft.fft() function calculates the frequency parts of a signal. It does this by changing a signal from the time domain, which shows how a signal changes over time, to the frequency domain, which shows how much of each frequency is in the signal. The FFT is a quick way to look at and understand the many frequencies in a signal.
This method is useful to study and work with frequency data in signal processing audio analysis, and image processing. In science and engineering, it's also useful for convolutions solving complex math problems, and looking at patterns that repeat.
This fft() method is very efficient since it drastically lowers computational complexity as compared to naive DFT. It is flexible to use for many applications as it supports configurable lengths, axes, and multi-dimensional arrays.
Syntax
The syntax for the Scipy fft() method is as follows −
.fft(x, n=None, axis=-1, norm=None, overwrite_x=False, plan=None)
Parameters
This method accepts the following parameters −
x (array-like) − Input array (signal data).
n (int, optional) − Length of the output transform. Pads with zeros or truncates if needed.
axis (int, optional) − Axis along which to compute the FFT (default is the last axis).
norm (str, optional) − Normalization mode ("backward", "forward" or "ortho").
overwrite_x (bool, optional) − If True, allows modifying input x to save memory.
plan (plan, optional) − Reserved for pre-computed FFT plans for performance optimization.
Return Value
out(complex ndarray) − The DFT of the input array x, containing complex values that represent the frequency components of the signal.
Example 1: Basic FFT Calculation
The FFT gives the frequency components of a signal, showing the proportion of each frequency in the input array.
In the below code we calculates the FFT of a basic signal, shows the real and imaginary parts of the frequency components.
import numpy as np from scipy.fft import fft # Input signal x = np.array([1, 2, 3, 4]) # Compute FFT result = fft(x) print("FFT Result:", result)
When we run above program, it produces following result −
FFT Result: [10.-0.j -2.+2.j -2.-0.j -2.-2.j]
Example 2: Zero-Padding for Higher Resolution
Zero-padding allows the FFT output to have finer frequency resolution by lengthening the input signal.
In this example, Zero-padding is used to construct a signal's FFT, which will produce more precise frequency data in the output array.
import numpy as np from scipy.fft import fft x = np.array([1, 2, 3, 4]) fft_x_padded = fft(x, n=8) print("FFT with zero-padding:\n", fft_x_padded)
Following is an output of the above code −
FFT with zero-padding: [10. -0.j -0.41421356-7.24264069j -2. +2.j 2.41421356-1.24264069j -2. -0.j 2.41421356+1.24264069j -2. -2.j -0.41421356+7.24264069j]
Example 3: FFT on Multi-Dimensional Arrays
In multi-dimensional arrays, FFT can be used along particular axes to alter signals one dimension at a time.
This example shows how to calculate the FFT along a 2D array's rows axis=1, individually converting each row into the frequency domain.
import numpy as np from scipy.fft import fft # Define a 2D signal x = np.array([[1, 2, 3], [4, 5, 6]]) # Compute FFT along rows fft_x_rows = fft(x, axis=1) print("FFT along rows:\n", fft_x_rows)
Output of the above code is as follows
FFT along rows: [[ 6. -0.j -1.5+0.8660254j -1.5-0.8660254j] [15. -0.j -1.5+0.8660254j -1.5-0.8660254j]]
Example 4: Overwriting Input for Performance
Enabling overwrite_x can improve performance by reusing memory making it suitable for large datasets.
This example optimizes memory usage and processing speed for a large input array by computing the FFT with overwrite_x=True.
import numpy as np from scipy.fft import fft x = np.random.rand(1000) fft_x = fft(x, overwrite_x=True) print("FFT computed with overwrite_x=True.")
Output of the above code is as follows −
FFT computed with overwrite_x=True.