Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

fftshift

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

Signal Processing

v0.3-1
GPL-3
Authors
Geert van Boxtel [aut, cre] (Maintainer), Tom Short [aut] (Author of 'signal' package), Paul Kienzle [aut] (Majority of the original sources), Ben Abbott [ctb], Juan Aguado [ctb], Muthiah Annamalai [ctb], Leonardo Araujo [ctb], William Asquith [ctb], David Bateman [ctb], David Billinghurst [ctb], Juan Pablo Carbajal [ctb], André Carezia [ctb], Vincent Cautaerts [ctb], Eric Chassande-Mottin [ctb], Luca Citi [ctb], Dave Cogdell [ctb], Carlo de Falco [ctb], Carne Draug [ctb], Pascal Dupuis [ctb], John W. Eaton [ctb], R.G.H Eschauzier [ctb], Andrew Fitting [ctb], Alan J. Greenberger [ctb], Mike Gross [ctb], Daniel Gunyan [ctb], Kai Habel [ctb], Kurt Hornik [ctb], Jake Janovetz [ctb], Alexander Klein [ctb], Peter V. Lanspeary [ctb], Bill Lash [ctb], Friedrich Leissh [ctb], Laurent S. Mazet [ctb], Mike Miller [ctb], Petr Mikulik [ctb], Paolo Neis [ctb], Georgios Ouzounis [ctb], Sylvain Pelissier [ctb], Francesco Potortì [ctb], Charles Praplan [ctb], Lukas F. Reichlin [ctb], Tony Richardson [ctb], Asbjorn Sabo [ctb], Thomas Sailer [ctb], Rolf Schirmacher [ctb], Rolf Schirmacher [ctb], Ivan Selesnick [ctb], Julius O. Smith III [ctb], Peter L. Soendergaard [ctb], Quentin Spencer [ctb], Doug Stewart [ctb], P. Sudeepam [ctb], Stefan van der Walt [ctb], Andreas Weber [ctb], P. Sudeepam [ctb], Andreas Weingessel [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.