Perform a shift in order to move the frequency 0 to the center of the input.
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.
Returns
vector or matrix with centered frequency.
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.
Examples
Xeven <-1:6ev <- fftshift(Xeven)# returns 4 5 6 1 2 3Xodd <-1:7odd <- fftshift(Xodd)# returns 5 6 7 1 2 3 4fs <-100# sampling frequencyt <- seq(0,10-1/fs,1/fs)# time vectorS <- cos(2* pi *15* t)n <- length(S)X <- fft(S)f <-(0:(n -1))*(fs / n);# frequency rangepower <- abs(X)^2/ n # powerplot(f, power, type="l")Y <- fftshift(X)fsh <-((-n/2):(n/2-1))*(fs / n)# zero-centered frequency rangepowersh <- abs(Y)^2/ n # zero-centered powerplot(fsh, powersh, type ="l")