fftshift
Shift zero-frequency component to center of spectrum
Description
Y = fftshift(
rearranges
a Fourier transform X
)X
by shifting the zero-frequency
component to the center of the array.
If
X
is a vector, thenfftshift
swaps the left and right halves ofX
.If
X
is a matrix, thenfftshift
swaps the first quadrant ofX
with the third, and the second quadrant with the fourth.If
X
is a multidimensional array, thenfftshift
swaps half-spaces ofX
along each dimension.
Examples
Shift Vector Elements
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×6
4 5 6 1 2 3
Xodd = [1 2 3 4 5 6 7]; fftshift(Xodd)
ans = 1×7
5 6 7 1 2 3 4
Shift 1-D Signal
When analyzing the frequency components of signals, it can be helpful to shift the zero-frequency components to the center.
Create a signal S
, compute its Fourier transform, and plot the power.
fs = 100; % sampling frequency t = 0:(1/fs):(10-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)
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 range powershift = abs(Y).^2/n; % zero-centered power plot(fshift,powershift)
Shift Signals in Matrix
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 matrix A
whose rows represent two 1-D signals, and compute the Fourier transform of each signal. Plot the power for each signal.
fs = 100; % sampling frequency t = 0:(1/fs):(10-1/fs); % time vector S1 = 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 range power = abs(X).^2/n; % power plot(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 range powershift = abs(Y).^2/n; % zero-centered power plot(fshift,powershift(1,:),fshift,powershift(2,:))
Input Arguments
X
— Input array
vector | matrix | multidimensional array
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
dim
— Dimension to operate along
positive integer scalar
Dimension to operate along, specified as a positive integer
scalar. If no value is specified, then fftshift
swaps
along all dimensions.
Consider an input matrix
Xc
. The operationfftshift(Xc,1)
swaps halves of each column ofXc
.Consider a matrix
Xr
. 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™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Beispiel öffnen
Sie haben eine geänderte Version dieses Beispiels. Möchten Sie dieses Beispiel mit Ihren Änderungen öffnen?
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)