
- 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 - dft() Function
The scipy.linalg.dft() function generates a square matrix that represents the DFT for a given n. This matrix breaks down signals into frequency components using complex exponentials. It is also possible to scale the matrix members optionally to normalize them.
This method is important in signal processing, spectrum analysis, and data transformation between time and frequency domains.
Inconsistent dimensions can result in errors when combining matrices or vectors. Poor handling of complex numbers may yield incorrect answers. Large n values can cause memory and performance issues.
The scaling parameter in the DFT matrix affects the transformation of the signal. The value 1/n ensures that the matrix reflects the average contribution of frequency components, while the value 1/sqrt(n) makes the matrix unitary and preserves energy in transformations without altering the power of the signal.
The DFT matrix is very efficient with numpy.matmul() for manual Fourier transforms and with packages like scipy.fft() for advanced frequency analysis.
Syntax
The syntax for the SciPy dft() method is as follows −
.dft(n, scale=None)
Parameters
This method accepts the following parameters −
n (int) − The size of the square DFT matrix (number of rows and columns).
scale (str, optional) − The scale parameter in scipy.linalg.dft normalizes the DFT matrix. With None, no scaling is applied. 'sqrtn' scales by 1/sqrt(n) for a unitary matrix, while 'n' scales by 1/n for amplitude normalization.
Return Value
DFT_matrix − An nn complex matrix representing the DFT transformation.
Example 1: Generating a 3x3 Unitary DFT Matrix
This code produces a 3x3 DFT matrix with unitary scaling for energy preservation. In the below code, scale='sqrtn' argument ensures that each member in the matrix is scaled by 1/sqrt(n), where n equals 3.
This scaling makes the matrix unitary, preserving the signal's energy during transformations. The output matrix's values are complex numbers with normalized magnitudes to ensure that energy is not amplified or reduced during the procedure.
from scipy.linalg import dft # Generate a 4x4 DFT matrix with unitary scaling matrix = dft(3, scale='sqrtn') print(matrix)
When we run above program, it produces following result
[[ 0.57735027+0.j 0.57735027+0.j 0.57735027+0.j ] [ 0.57735027+0.j -0.28867513-0.5j -0.28867513+0.5j] [ 0.57735027+0.j -0.28867513+0.5j -0.28867513-0.5j]]
Example 2: Generating a 3x3 Normalized DFT Matrix
This code generates a 3x3 DFT matrix with each element normalized by 1/n (where n=3) and scale='n'.
The dft(3, scale='n') function creates a normalized DFT matrix with each member scaled by 1/3. The matrix is complex, comprising real and imaginary portions that represent normalized frequency components.
import numpy as np from scipy.linalg import dft matrix = dft(3, scale='n') print(matrix)
Following is an output of the above code −
[[ 0.33333333+0.j 0.33333333+0.j 0.33333333+0.j ] [ 0.33333333+0.j -0.16666667-0.28867513j -0.16666667+0.28867513j] [ 0.33333333+0.j -0.16666667+0.28867513j -0.16666667-0.28867513j]]
Example 3: Transform a Signal Using a 3x3 DFT Matrix
The dft() function of SciPy.Linalg returns the 3x3 DFT transformation matrix, which is utilized to convert the signal from time domain into a frequency domain. The DFT technique breaks a signal into its components of frequency and finds the proportion of each frequency component contributing to the signal.
To transform the signal [1, 2, 3], multiply the input signal by the DFT matrix. This produces an array of complex numbers that represents the signal in the frequency domain. The first value displays the overall energy of the signal, and the other values represent the contributions of different frequencies.
import numpy as np from scipy.linalg import dft matrix = dft(3) # Signal to transform signal = np.array([1, 2, 3]) transformed_signal = matrix @ signal print(transformed_signal)
Output of the above code is as follows
[ 6. +0.j -1.5+0.8660254j -1.5-0.8660254j]