fftshift: Fourier Transform Shift

View source: R/fftshift.R

fftshiftR Documentation

Fourier Transform Shift

Description

This function serves to shift the zero-frequency component of the Fourier transform to the center of the matrix.

Usage

fftshift(x, dim = -1)

Arguments

x

An n x n Fourier transform matrix.

dim

Which dimension to shift the matrix. -1 swaps up/down and left/right. 1 swaps up/down. 2 swaps left/right.

Value

An n x n matrix with the zero-frequency component of the Fourier transform in the center.

References

#' This function was created from code posted by rayryeng at: https://stackoverflow.com/questions/38230794/how-to-write-fftshift-and-ifftshift-in-r.

Examples

# import raster image
data(normforest)
normforest <- terra::unwrap(normforest)

# convert to matrix form
M <- ncol(normforest)
N <- nrow(normforest)
zmat <- matrix(terra::values(normforest), ncol = M, nrow = N, byrow = TRUE)

# calculate fourier transform and shift
ftmat <- fft(zmat)
ftshift <- fftshift(ftmat)

# plot real component
r <- terra::setValues(normforest, Re(ftshift))
terra::plot(r)

geodiv documentation built on Oct. 6, 2023, 1:07 a.m.