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]
scipy_special_matrices_functions.htm
Advertisements