Location>code7788 >text

Python implementation of multi-dimensional Fourier transforms

Popularity:444 ℃/2024-09-26 11:14:04

Technical background

in the previous postwritingsIn , we introduced the basic principles and simple code implementation of one-dimensional discrete Fourier transform and fast Fourier transform. This article adds a multidimensional Fourier transform scenario and a simple Python implementation.

two-dimensional Fourier transform (math.)

First recall the form of the one-dimensional Fourier transform and the inverse Fourier transform introduced in the previous article:

\[y_k=\sum_{n=0}^{N-1}x_ne^{-j\frac{2\pi nk}{N}},0\leq k\leq N-1\\ x_n=\frac{1}{N}\sum_{k=0}^{N-1}y_ke^{j\frac{2\pi nk}{N}},0\leq n\leq N-1 \]

So first let's understand the physical image of the one-dimensional Fourier transform by using the simple DFT implementation from an earlier post:

import numpy as np

def dft(x):
    y = np.zeros_like(x, dtype=np.complex64)
    N = [0]
    for k in range(N):
        y[k] = (x * (-1j*2**k*(N)/N))
    return y

Let's not discuss the concepts of time and frequency domains; here there are only input x and output y. The data for each point of y, then, is an inner product of the x vector through a series of parameter vectors. In other words, every data point on x contributes to every data point on y, and the magnitude of this contribution is given by the parameters of the Fourier transform:

The high-dimensional Fourier transform, then, is really an inner product in each dimension in order:

The corresponding algebraic form is:

\[y_{k_1,k_2}=\sum_{n_2=0}^{D-1}e^{-j\frac{2\pi n_2k_2}{D}}\sum_{n_1=0}^{N-1}x_{n_1,n_2}e^{-j\frac{2\pi n_1k_1}{N}},0\leq k_1,k_2\leq N-1\\ x_{n_1,n_2}=\frac{1}{D}\sum_{k_2=0}^{D-1}e^{j\frac{2\pi n_2k_2}{D}}\frac{1}{N}\sum_{k_1=0}^{N-1}y_{k_1,k_2}e^{j\frac{2\pi n_1k_1}{N}},0\leq n_1,n_2\leq N-1 \]

As for higher dimensional Fourier transforms, this is to continue to increase the dimension of the summation. There is also a common way to write this in the form of a normalized vector inner product:

\[y_{\vec{k}}=\sum_{\vec{n}}e^{-2j\pi\vec{k}\cdot\vec{n}}x_{\vec{n}} \]

As for the form of the FFT, it is just a decomposition of specific dimensions of it, and instead of analyzing it more here, you can just take a look at a simple implementation of the multidimensional DFT.

Python code implementation

Here is the simplest implementation of a 2D Fourier Transform and Inverse Fourier Transform using Python, without any optimization:

import numpy as np

def dftn(x):
    y = np.zeros_like(x, dtype=np.complex64)
    N = [0]
    D = [1]
    for k1 in range(N):
        for k2 in range(D):
            for n1 in range(N):
                for n2 in range(D):
                    y[k1][k2] += (-2j**(k2*n2)/D)* (-2j**(k1*n1)/N) * x[n1][n2]
    return y

def idftn(y):
    x = np.zeros_like(y, dtype=np.complex64)
    N = [0]
    D = [1]
    for n1 in range(N):
        for n2 in range(D):
            for k1 in range(N):
                for k2 in range(D):
                    x[n1][n2] += (2j**(k2*n2)/D) * (2j**(k1*n1)/N) * y[k1][k2] / N / D
    return x

N = 16
x = ((N, 3)).astype(np.float32)
y0 = dftn(x)
y1 = .fft2(x)
x0 = idftn(y1)
x1 = .ifft2(y0)
print ((y0, y1))
print ((x0, x1))
# True
# True

After comparison with the way it is implemented in numpy, the results are consistent on both sides.

Summary outline

Following the one-dimensional Fourier transform in the previous article, this article introduces the physical image and basic principles of the multidimensional Fourier transform, accompanied by a simple implementation in Python. The results of the Python calculations are also compared with the results of the two-dimensional Fourier transform already implemented in Numpy.

copyright statement

This article was first linked to:/dechinphy/p/

Author ID: DechinPhy

More original articles:/dechinphy/

Buy the blogger coffee:/dechinphy/gallery/image/