Documentation

fftshift

Shift zero-frequency component to center of spectrum

Description

example

Y = fftshift(X)rearranges a Fourier transformXby shifting the zero-frequency component to the center of the array.

  • IfXis a vector, thenfftshiftswaps the left and right halves ofX.

  • IfXis a matrix, thenfftshiftswaps the first quadrant ofXwith the third, and the second quadrant with the fourth.

  • IfXis a multidimensional array, thenfftshiftswaps half-spaces ofXalong each dimension.

example

Y = fftshift(X,dim)operates along the dimensiondimofX. For example, ifXis a matrix whose rows represent multiple 1-D transforms, thenfftshift(X,2)swaps the halves of each row ofX.

Examples

collapse all

Swap the left and right halves of a row vector. If a vector has an odd number of elements, then the middle element is considered part of the left half of the vector.

Xeven = [1 2 3 4 5 6]; fftshift(Xeven)
ans =1×64 5 6 1 2 3
Xodd = [1 2 3 4 5 6 7]; fftshift(Xodd)
ans =1×75 6 7 1 2 3 4

When analyzing the frequency components of signals, it can be helpful to shift the zero-frequency components to the center.

Create a signalS, compute its Fourier transform, and plot the power.

fs = 100;% sampling frequencyt = 0:(1/fs):(10-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)

Shift the zero-frequency components and plot the zero-centered power.

Y = fftshift(X); fshift = (-n/2:n/2-1)*(fs/n);% zero-centered frequency rangepowershift = abs(Y).^2/n;% zero-centered powerplot(fshift,powershift)

You can process multiple 1-D signals by representing them as rows in a matrix. Then use the dimension argument to compute the Fourier transform and shift the zero-frequency components for each row.

Create a matrixAwhose rows represent two 1-D signals, and compute the Fourier transform of each signal. Plot the power for each signal.

fs = 100;% sampling frequencyt = 0:(1/fs):(10-1/fs);% time vectorS1 = cos(2*pi*15*t); S2 = cos(2*pi*30*t); n = length(S1); A = [S1; S2]; X = fft(A,[],2); f = (0:n-1)*(fs/n);% frequency rangepower = abs(X).^2/n;% powerplot(f,power(1,:),f,power(2,:))

Shift the zero-frequency components, and plot the zero-centered power of each signal.

Y = fftshift(X,2); fshift = (-n/2:n/2-1)*(fs/n);% zero-centered frequency rangepowershift = abs(Y).^2/n;% zero-centered powerplot(fshift,powershift(1,:),fshift,powershift(2,:))

Input Arguments

collapse all

Input array, specified as a vector, a matrix, or a multidimensional array.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Dimension to operate along, specified as a positive integer scalar. If no value is specified, thenfftshiftswaps along all dimensions.

  • Consider an input matrixXc. The operationfftshift(Xc,1)swaps halves of each column ofXc.

  • Consider a matrixXr. The operationfftshift(Xr,2)swaps halves of each row ofXr.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a