
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Fast Fourier Transform
NumPy Fast Fourier Transform
The Fast Fourier Transform (FFT) is a quick way to compute the Discrete Fourier Transform (DFT) and its inverse. It speeds up the process by reducing the time it takes from O(n2) to O(nlogn), making it much faster, especially when working with large datasets.
In NumPy, you can calculate the FFT using the fft module, which has functions for both one-dimensional and multi-dimensional FFT calculations.
Computing the FFT Using fft() Function
The numpy.fft.fft() function computes the one-dimensional n-point FFT of an array. It transforms a signal from the time domain into the frequency domain.
fft(x) = Fast Fourier Transform of the input array x
Example: Computing the FFT
In the following example, we compute the Fast Fourier Transform of a one-dimensional array (signal) using the fft() function −
import numpy as np # Define a simple signal signal = np.array([1, 2, 3, 4]) # Compute the Fast Fourier Transform of the signal fft_signal = np.fft.fft(signal) print("FFT of the signal:", fft_signal)
We get the output as shown below −
FFT of the signal: [10.+0.j -2.+2.j -2.+0.j -2.-2.j]
Computing the Inverse FFT
The numpy.fft.ifft() function computes the inverse of the Fast Fourier Transform, converting frequency components back into the original time-domain signal. This is useful when you want to reconstruct the signal from its frequency representation.
ifft(x) = Inverse Fast Fourier Transform of the input array x
Example: Inverse FFT
In the following example, we compute the inverse FFT of the FFT computed earlier using NumPy's ifft() function −
import numpy as np # Define the FFT of a signal (computed previously) fft_signal = np.array([10+0j, -2+2j, -2+0j, -2-2j]) # Compute the Inverse Fast Fourier Transform reconstructed_signal = np.fft.ifft(fft_signal) print("Reconstructed signal:", reconstructed_signal)
The result produced is as follows −
Reconstructed signal: [1.+0.j 2.+0.j 3.+0.j 4.+0.j]
Computing the FFT for a Real Signal
The numpy.fft.rfft() function is optimized for computing the FFT of real input signals. This function returns only the non-negative frequency terms, as the negative frequency terms are redundant for real-valued signals.
rfft(x) = FFT of a real-valued input array x
Example: FFT of a Real Signal
In the following example, we compute the Fast Fourier Transform of a real-valued signal using NumPy's rfft() function −
import numpy as np # Define a real-valued signal signal = np.array([1, 2, 3, 4]) # Compute the Fast Fourier Transform of the real-valued signal fft_real_signal = np.fft.rfft(signal) print("FFT of the real-valued signal:", fft_real_signal)
After executing the above code, we get the following output −
FFT of the real-valued signal: [10.+0.j -2.+2.j -2.+0.j]
Computing Inverse FFT for a Real Signal
The numpy.fft.irfft() function computes the inverse of the FFT for real-valued signals, reconstructing the original time-domain signal from its non-negative frequency components.
irfft(x) = Inverse FFT of a real-valued input array x
Example: Inverse FFT of a Real Signal
In the following example, we compute the inverse FFT of a real-valued signal −
import numpy as np # Define the FFT of a real-valued signal (computed previously) fft_real_signal = np.array([10+0j, -2+2j]) # Compute the Inverse FFT of the real-valued FFT reconstructed_real_signal = np.fft.irfft(fft_real_signal) print("Reconstructed real-valued signal:", reconstructed_real_signal)
The output obtained is as shown below −
Reconstructed real-valued signal: [4. 6.]
Frequency Binning Using fftfreq() Function
The numpy.fft.fftfreq() function is used to generate an array of sample frequencies corresponding to the components of the FFT. This is useful for plotting the frequency components of a signal or analyzing its spectral content.
fftfreq(n, d=1) = frequencies corresponding to the FFT of an array with n points, spaced by d
Example: Frequency Binning
In the following example, we generate the frequencies corresponding to the FFT of a signal −
import numpy as np # Define the number of points in the FFT and the sample spacing n_points = 4 spacing = 1 # Generate the frequencies corresponding to the FFT frequencies = np.fft.fftfreq(n_points, spacing) print("Frequencies corresponding to the FFT:", frequencies)
Following is the output of the above code −
Frequencies corresponding to the FFT: [ 0. 0.25 -0.5 -0.25]
Using FFT in Multi-dimensional Arrays
NumPy's fft module also supports multi-dimensional arrays, allowing you to compute the FFT along specific axes of the array. The numpy.fft.fftn() function computes the n-dimensional FFT, while numpy.fft.ifftn() function computes the inverse of the n-dimensional FFT.
fftn(x) = N-dimensional Fast Fourier Transform of an array x
Example: 2D FFT
In the following example, we compute the 2-dimensional FFT of a 2D array −
import numpy as np # Define a 2D signal signal_2d = np.array([[1, 2], [3, 4]]) # Compute the 2D Fast Fourier Transform fft_2d_signal = np.fft.fftn(signal_2d) print("2D FFT of the signal:", fft_2d_signal)
This will produce the following result −
2D FFT of the signal: [[10.+0.j -2.+0.j] [-4.+0.j 0.+0.j]]