Engee documentation

fft2

Two-dimensional fast Fourier transform.

Library

EngeeDSP

Syntax

Function call

  • Y = fft2(X) — returns two-dimensional Fourier transform matrices X using the fast Fourier transform algorithm, which is equivalent to computing fft(fft(X).').'.

    If X — multidimensional array, fft2 calculates the two-dimensional Fourier transform for the first two dimensions of each subarray X, which can be considered as a two - dimensional matrix for dimensions greater than 2. For example, if X — an array of size m×n×1×2 Then Y(:,:,1,1) = fft2(X(:,:,1,1)) and Y(:,:,1,2) = fft2(X(:,:,1,2)). Output size Y matches the size of X.

  • Y = fft2(X,m,n) — truncates X or complements X with zeros to the end, forming a matrix of size m on n before calculating the transformation. If X — the matrix, then Y — matrix size m on n. If X — a multidimensional array, then fft2 forms the first two dimensions X according to m and n.

Arguments

Input arguments

# X — input array

+ the matrix | multidimensional array

Details

The input array, specified as a matrix or multidimensional array. If X has a type Float32, then the function fft2 it is initially calculated with single precision, and Y It also has a type Float32. Otherwise, Y returns as Float64.

Типы данных

Float64, Float32, Int8, Int16, Int32, UInt8, UInt16, UInt32, Bool

Support for complex numbers

Yes

# m — number of conversion lines

+ a positive integer scalar

Details

The number of conversion lines specified as a positive integer.

Типы данных

Float64, Float32, Int8, Int16, Int32, UInt8, UInt16, UInt32, Bool

# n is the number of conversion columns

+ a positive integer scalar

Details

The number of conversion columns, set as a positive integer.

Типы данных

Float64, Float32, Int8, Int16, Int32, UInt8, UInt16, UInt32, Bool

Output arguments

# Y — output array

+ the matrix | multidimensional array

Details

The output array returned as a matrix or multidimensional array.

Examples

Two-dimensional transformation

Details

The two-dimensional Fourier transform is useful for processing two-dimensional signals and other two-dimensional data such as images.

Let’s create and display two-dimensional data with repeating blocks on the graph.

function peaks(n)
    x = range(-3, 3, length=n)
    y = range(-3, 3, length=n)
    X = repeat(x', n, 1)
    Y = repeat(y, 1, n)
    Z = 3 * (1 .- X).^2 .* exp.(-X.^2 .- (Y .+ 1).^2) .-
        10 * (X/5 .- X.^3 .- Y.^5) .* exp.(-X.^2 .- Y.^2) .-
        1/3 * exp.(-(X .+ 1).^2 .- Y.^2)
    return Z
end

P = peaks(20)
X = repeat(P, 5, 10)

heatmap(X, aspect_ratio=1, c=:viridis, yflip=true)

fft2 1

Calculate the two-dimensional Fourier transform of the data. Shift the zero frequency component to the center of the output signal and plot the resulting matrix size 100 on 200, which corresponds to the size X.

import EngeeDSP.Functions: fft2, fftshift

Y = fft2(X)
Y_shifted = fftshift(Y)

heatmap(abs.(Y_shifted), aspect_ratio=1, c=:viridis, yflip=true)

fft2 2

Adding more X with zeros to calculate the transformation size 128 on 256.

n1 = nextpow(2, 100)  # 2^7 = 128
n2 = nextpow(2, 200)  # 2^8 = 256
Y_padded = fft2(X, n1, n2)
Y_padded_shifted = fftshift(Y_padded)

heatmap(abs.(Y_padded_shifted), aspect_ratio=1, c=:viridis, yflip=true)

fft2 3

Additional Info

Two-dimensional Fourier transform

Details

This formula defines the discrete Fourier transform. matrices size on :

where and — complex roots of unity:



where — imaginary unit. and — indexes ranging from before , and and — indexes ranging from before . This formula shifts the indexes and on 1 to reflect the indexes of the matrices in Engee.