
- 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 - Discrete Fourier Transform
NumPy Discrete Fourier Transform
The Discrete Fourier Transform (DFT) is a mathematical technique used to convert a sequence of values into components of different frequencies. It is widely used in signal processing, image analysis, and audio processing.
In NumPy, the DFT can be computed using the fft (Fast Fourier Transform) module, which provides implementations for computing the DFT and its inverse.
The DFT helps analyze the frequency content of a signal, making it useful in many applications, including filtering, signal compression, and spectral analysis.
Computing DFT Using fft() Function
The numpy.fft.fft() function computes the one-dimensional n-point Discrete Fourier Transform (DFT) of an array. The function returns the frequency components of the input signal, with the zero frequency component at the beginning of the output array.
fft(x) = Discrete Fourier Transform of the input array x
Example: Computing the DFT
In the following example, we compute the Discrete Fourier Transform of a one-dimensional array using NumPy's fft() function −
import numpy as np # Define an array of sample data (signal) signal = np.array([1, 2, 3, 4]) # Compute the Discrete Fourier Transform of the signal dft_signal = np.fft.fft(signal) print("DFT of the signal:", dft_signal)
The result of the above code is −
DFT of the signal: [10.+0.j -2.+2.j -2.+0.j -2.-2.j]
Computing the Inverse DFt
The numpy.fft.ifft() function computes the inverse of the Discrete 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 Discrete Fourier Transform of the input array x
Example: Inverse DFT
In the following example, we compute the inverse Discrete Fourier Transform of the DFT computed earlier using NumPy's ifft() function −
import numpy as np # Define the DFT of a signal (computed previously) dft_signal = np.array([10+0j, -2+2j, -2+0j, -2-2j]) # Compute the Inverse Discrete Fourier Transform of the DFT reconstructed_signal = np.fft.ifft(dft_signal) print("Reconstructed signal:", reconstructed_signal)
Following is the output obtained −
Reconstructed signal: [1.+0.j 2.+0.j 3.+0.j 4.+0.j]
Computing the DFT for a Real Signal
The numpy.fft.rfft() function is optimized for computing the DFT 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) = DFT of a real-valued input array x
Example: DFT of a Real Signal
In the following example, we compute the Discrete 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 Discrete Fourier Transform of the real-valued signal dft_real_signal = np.fft.rfft(signal) print("DFT of the real-valued signal:", dft_real_signal)
This will produce the following result −
DFT of the real-valued signal: [10.+0.j -2.+2.j -2.+0.j]
Computing the Inverse DFT for Real Signal
The numpy.fft.irfft() function computes the inverse of the DFT for real-valued signals, reconstructing the original time-domain signal from its non-negative frequency components.
irfft(x) = Inverse DFT of a real-valued input array x
Example: Inverse DFT of a Real Signal
In the following example, we compute the inverse DFT of a real-valued signal −
import numpy as np # Define the DFT of a real-valued signal (computed previously) dft_real_signal = np.array([10+0j, -2+2j]) # Compute the Inverse Discrete Fourier Transform of the real-valued DFT reconstructed_real_signal = np.fft.irfft(dft_real_signal) print("Reconstructed real-valued signal:", reconstructed_real_signal)
Following is the output of the above code −
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 DFT. This is useful for plotting the frequency components of a signal or analyzing its spectral content.
fftfreq(n, d=1) = frequencies corresponding to the DFT of an array with n points, spaced by d
Example: Frequency Binning
In the following example, we generate the frequencies corresponding to the DFT of a signal −
import numpy as np # Define the number of points in the DFT and the sample spacing n_points = 4 spacing = 1 # Generate the frequencies corresponding to the DFT frequencies = np.fft.fftfreq(n_points, spacing) print("Frequencies corresponding to the DFT:", frequencies)
The output obtained is as shown below −
Frequencies corresponding to the DFT: [ 0. 0.25 -0.5 -0.25]