fftshift: Zero-frequency shift

View source: R/fftshift.R

fftshiftR Documentation

Zero-frequency shift

Description

Perform a shift in order to move the frequency 0 to the center of the input.

Usage

fftshift(x, MARGIN = 2)

Arguments

x

input data, specified as a vector or matrix.

MARGIN

dimension to operate along, 1 = row, 2 = columns (default). Specifying MARGIN = c(1, 2) centers along both rows and columns. Ignored when x is a vector.

Details

If x is a vector of N elements corresponding to N time samples spaced by dt, then fftshift(x) corresponds to frequencies f = c(-seq(ceiling((N-1)/2), 1, -1), 0, (1:floor((N-1)/2))) * df, where df = 1 / (N * dt). In other words, the left and right halves of x are swapped.

If x is a matrix, then fftshift operates on the rows or columns of x, according to the MARGIN argument, i.e. it swaps the the upper and lower halves of the matrix (MARGIN = 1), or the left and right halves of the matrix (MARGIN = 2). Specifying MARGIN = c(1, 2) swaps along both dimensions, i.e., swaps the first quadrant with the fourth, and the second with the third.

Value

vector or matrix with centered frequency.

Author(s)

Vincent Cautaerts, vincent@comf5.comm.eng.osaka-u.ac.jp,
adapted by John W. Eaton.
Conversion to R by Geert van Boxtel, G.J.M.vanBoxtel@gmail.com.

See Also

ifftshift

Examples

Xeven <- 1:6
ev <- fftshift(Xeven)   # returns 4 5 6 1 2 3

Xodd <- 1:7
odd <- fftshift(Xodd)   # returns 5 6 7 1 2 3 4

fs <- 100                      # sampling frequency
t <- seq(0, 10 - 1/fs, 1/fs)   # time vector
S <- cos(2 * pi * 15 * t)
n <- length(S)
X <- fft(S)
f <- (0:(n - 1)) * (fs / n);   # frequency range
power <- abs(X)^2 / n          # power
plot(f, power, type="l")
Y <- fftshift(X)
fsh <- ((-n/2):(n/2-1)) * (fs / n)  # zero-centered frequency range
powersh <- abs(Y)^2 / n             # zero-centered power
plot(fsh, powersh, type = "l")


gsignal documentation built on Sept. 12, 2024, 6:27 a.m.