You are on page 1of 21

fft - Fast Fourier transform

Syntax
Y Y Y Y = = = = fft(x) fft(X,n) fft(X,[],dim) fft(X,n,dim)

Definitions
The functions Y = fft(x) and y = ifft(X) implement the transform and inverse transform pair given for vectors of length N by:

where

is an Nth root of unity.

Description
returns the discrete Fourier transform (DFT) of vector x, computed with a fast Fourier transform (FFT) algorithm.
Y = fft(x)

If the input X is a matrix, Y = fft(X) returns the Fourier transform of each column of the matrix. If the input X is a multidimensional array, fft operates on the first nonsingleton dimension.
Y = fft(X,n) returns the n-point DFT. fft(X) is equivalent to fft(X, n) where n is the size of X in the first nonsingleton dimension. If the length of X is less than n, X is padded with trailing zeros to length n. If the length of X is greater than n, the sequence X is truncated. When X is a

matrix, the length of the columns are adjusted in the same manner.
Y = fft(X,[],dim) dim.

and Y = fft(X,n,dim) applies the FFT operation across the dimension

Examples
A common use of Fourier transforms is to find the frequency components of a signal buried in a noisy time domain signal. Consider data sampled at 1000 Hz. Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and 120 Hz sinusoid of amplitude 1 and corrupt it with some zeromean random noise:
Fs = 1000; % Sampling frequency T = 1/Fs; % Sample time L = 1000; % Length of signal t = (0:L-1)*T; % Time vector % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); y = x + 2*randn(size(t)); % Sinusoids plus noise plot(Fs*t(1:50),y(1:50)) title('Signal Corrupted with Zero-Mean Random Noise') xlabel('time (milliseconds)')

It is difficult to identify the frequency components by looking at the original signal. Converting to the frequency domain, the discrete Fourier transform of the noisy signal y is found by taking the fast Fourier transform (FFT):
NFFT = 2^nextpow2(L); % Next power of 2 from length of y Y = fft(y,NFFT)/L;

f = Fs/2*linspace(0,1,NFFT/2+1); % Plot single-sided amplitude spectrum. plot(f,2*abs(Y(1:NFFT/2+1))) title('Single-Sided Amplitude Spectrum of y(t)') xlabel('Frequency (Hz)') ylabel('|Y(f)|')

The main reason the amplitudes are not exactly at 0.7 and 1 is because of the noise. Several executions of this code (including recomputation of y) will produce different approximations to 0.7 and 1. The other reason is that you have a finite length signal. Increasing L from 1000 to 10000 in the example above will produce much better approximations on average.

Algorithms
The FFT functions (fft, fft2, fftn, ifft, ifft2, ifftn) are based on a library called FFTW [3],[4]. To compute an N-point DFT when N is composite (that is, when N = N1N2), the FFTW library decomposes the problem using the Cooley-Tukey algorithm [1], which first computes N1 transforms of size N2, and then computes N2 transforms of size N1. The decomposition is applied recursively to both the N1- and N2-point DFTs until the problem can be solved using one of several machine-generated fixed-size "codelets." The codelets in turn use several algorithms in combination, including a variation of Cooley-Tukey [5], a prime factor algorithm [6], and a splitradix algorithm [2]. The particular factorization of N is chosen heuristically.

When N is a prime number, the FFTW library first decomposes an N-point problem into three (N 1)-point problems using Rader's algorithm [7]. It then uses the Cooley-Tukey decomposition described above to compute the (N 1)-point DFTs. For most N, real-input DFTs require roughly half the computation time of complex-input DFTs. However, when N has large prime factors, there is little or no speed difference. The execution time for fft depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors. Note You might be able to increase the speed of fft using the utility function fftw, which controls the optimization of the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support


fft supports inputs of data types double and single. If you call fft fft(X, ...), the output y has the same data type as the input X.

with the syntax y =

References
[1] Cooley, J. W. and J. W. Tukey, "An Algorithm for the Machine Computation of the Complex Fourier Series,"Mathematics of Computation, Vol. 19, April 1965, pp. 297-301. [2] Duhamel, P. and M. Vetterli, "Fast Fourier Transforms: A Tutorial Review and a State of the Art," Signal Processing, Vol. 19, April 1990, pp. 259-299. [3] FFTW (http://www.fftw.org) [4] Frigo, M. and S. G. Johnson, "FFTW: An Adaptive Software Architecture for the FFT,"Proceedings of the International Conference on Acoustics, Speech, and Signal Processing, Vol. 3, 1998, pp. 1381-1384. [5] Oppenheim, A. V. and R. W. Schafer, Discrete-Time Signal Processing, Prentice-Hall, 1989, p. 611. [6] Oppenheim, A. V. and R. W. Schafer, Discrete-Time Signal Processing, Prentice-Hall, 1989, p. 619. [7] Rader, C. M., "Discrete Fourier Transforms when the Number of Data Samples Is Prime," Proceedings of the IEEE, Vol. 56, June 1968, pp. 1107-1108.

fft2 - 2-D fast Fourier transform


Syntax
Y = fft2(X) Y = fft2(X,m,n)

Description
returns the two-dimensional discrete Fourier transform (DFT) of X, computed with a fast Fourier transform (FFT) algorithm. The result Y is the same size as X.
Y = fft2(X)

truncates X, or pads X with zeros to create an m-by-n array before doing the transform. The result is m-by-n.
Y = fft2(X,m,n)

Algorithms
fft2(X)

can be simply computed as

fft(fft(X).').'

This computes the one-dimensional DFT of each column X, then of each row of the result. The execution time for fft depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors. Note You might be able to increase the speed of fft2 using the utility function fftw, which controls how MATLAB software optimizes the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support


fft2 supports inputs of data types double and single. If you call fft2 fft2(X, ...), the output y has the same data type as the input X.

with the syntax y =

fftn - N-D fast Fourier transform


Syntax
Y = fftn(X) Y = fftn(X,siz)

Description
returns the discrete Fourier transform (DFT) of X, computed with a multidimensional fast Fourier transform (FFT) algorithm. The result Y is the same size as X.
Y = fftn(X) Y = fftn(X,siz) pads X with zeros, or truncates X, to create a multidimensional siz before performing the transform. The size of the result Y is siz.

array of size

Algorithms
fftn(X)

is equivalent to

Y = X; for p = 1:length(size(X)) Y = fft(Y,[],p); end

This computes in-place the one-dimensional fast Fourier transform along each dimension of X. The execution time for fft depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors. Note You might be able to increase the speed of fftn using the utility function fftw, which controls the optimization of the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support


fftn supports inputs of data types double and single. If you call fftn fftn(X, ...), the output y has the same data type as the input X.

with the syntax y =

fftshift - Shift zero-frequency component to center of spectrum


Syntax
Y = fftshift(X) Y = fftshift(X,dim)

Description
rearranges the outputs of fft, fft2, and fftn by moving the zero-frequency component to the center of the array. It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum.
Y = fftshift(X)

For vectors, fftshift(X) swaps the left and right halves of X. For matrices, fftshift(X) swaps the first quadrant with the third and the second quadrant with the fourth.

For higher-dimensional arrays, fftshift(X) swaps "half-spaces" of X along each dimension.


Y = fftshift(X,dim)

applies the fftshift operation along the dimension dim.

Note ifftshift will undo the results of fftshift. If the matrix X contains an odd number of elements, ifftshift(fftshift(X)) must be done to obtain the original X. Simply performing fftshift(X) twice will not produce X.

Examples
For any matrix X
Y = fft2(X)

has Y(1,1) = sum(sum(X)); the zero-frequency component of the signal is in the upper-left corner of the two-dimensional FFT. For
Z = fftshift(Y)

this zero-frequency component is near the center of the matrix. The difference between fftshift and ifftshift is important for input sequences of oddlength.
N X Y Z = = = = 5; 0:N-1; fftshift(fftshift(X)); ifftshift(fftshift(X));

Notice that Z is a correct replica of X, but Y is not.


isequal(X,Y),isequal(X,Z) ans = 0 ans = 1

fftw - Interface to FFTW library run-time algorithm tuning control


Syntax
fftw('planner', method) method = fftw('planner') str = fftw('dwisdom') str = fftw('swisdom') fftw('dwisdom', str) fftw('swisdom', str)

Description
fftw enables you to optimize the speed of the MATLAB FFT functions fft, ifft, fft2, ifft2, fftn, and ifftn. You can use fftw to set options for a tuning algorithm that experimentally

determines the fastest algorithm for computing an FFT of a particular size and dimension at run time. MATLAB software records the optimal algorithm in an internal data base and uses it to compute FFTs of the same size throughout the current session. The tuning algorithm is part of the FFTW library that MATLAB software uses to compute FFTs. sets the method by which the tuning algorithm searches for a good FFT algorithm when the dimension of the FFT is not a power of 2. You can specify method to be one of the following. The default method is estimate:
fftw('planner', method) 'estimate' 'measure' 'patient' 'exhaustive' 'hybrid'

When you call fftw('planner', method), the next time you call one of the FFT functions, such as fft, the tuning algorithm uses the specified method to optimize the FFT computation. Because the tuning involves trying different algorithms, the first time you call an FFT function, it might run more slowly than if you did not call fftw. However, subsequent calls to any of the FFT functions, for a problem of the same size, often run more quickly than they would without using fftw.

Note The FFT functions only use the optimal FFT algorithm during the current

MATLAB session. Reusing Optimal FFT Algorithms explains how to reuse the optimal algorithm in a future MATLAB session. If you set the method to 'estimate', the FFTW library does not use run-time tuning to select the algorithms. The resulting algorithms might not be optimal. If you set the method to 'measure', the FFTW library experiments with many different algorithms to compute an FFT of a given size and chooses the fastest. Setting the method to 'patient' or 'exhaustive' has a similar result, but the library experiments with even more algorithms so that the tuning takes longer the first time you call an FFT function. However, subsequent calls to FFT functions are faster than with 'measure'. If you set 'planner' to 'hybrid', MATLAB software

Sets method to 'measure' method for FFT dimensions 8192 or smaller. Sets method to 'estimate' for FFT dimensions greater than 8192. returns the current planner method.

method = fftw('planner') str = fftw('dwisdom')

returns the information in the FFTW library's internal doubleprecision database as a string. The string can be saved and then later reused in a subsequent MATLAB session using the next syntax.
str = fftw('swisdom')

returns the information in the FFTW library's internal single-precision

database as a string. loads fftw wisdom represented by the string str into the FFTW library's internal double-precision wisdom database. fftw('dwisdom','') or fftw('dwisdom', []) clears the internal wisdom database.
fftw('dwisdom', str)

loads fftw wisdom represented by the string str into the FFTW library's internal single-precision wisdom database. fftw('swisdom','') or fftw('swisdom', []) clears the internal wisdom database.
fftw('swisdom', str)

Note on large powers of 2 For FFT dimensions that are powers of 2, between 214 and 222, MATLAB software uses special preloaded information in its internal database to optimize the FFT computation. No tuning is performed when the dimension of the FTT is a power of 2, unless you clear the database using the command fftw('wisdom', []). For more information about the FFTW library, see http://www.fftw.org.

Examples
Comparison of Speed for Different Planner Methods

The following example illustrates the run times for different settings of planner. The example first creates some data and applies fft to it using the default method, estimate.
t=0:.001:5; x = sin(2*pi*50*t)+sin(2*pi*120*t); y = x + 2*randn(size(t)); tic; Y = fft(y,1458); toc Elapsed time is 0.000521 seconds.

If you execute the commands


tic; Y = fft(y,1458); toc Elapsed time is 0.000151 seconds.

a second time, MATLAB software reports the elapsed time as essentially 0. To measure the elapsed time more accurately, you can execute the command Y = fft(y,1458) 1000 times in a loop.
tic; for k=1:1000 Y = fft(y,1458); end; toc Elapsed time is 0.056532 seconds.

This tells you that it takes on order of 1/10000 of a second to execute fft(y, 1458) a single time. For comparison, set planner to patient. Since this planner explores possible algorithms more thoroughly than hybrid, the first time you run fft, it takes longer to compute the results.
fftw('planner','patient') tic;Y = fft(y,1458);toc Elapsed time is 0.100637 seconds.

However, the next time you call fft, it runs at approximately the same speed as before you ran the method patient.
tic;for k=1:1000 Y=fft(y,1458); end;toc Elapsed time is 0.057209 seconds.

Reusing Optimal FFT Algorithms

In order to use the optimized FFT algorithm in a future MATLAB session, first save the "wisdom" using the command
str = fftw('wisdom')

You can save str for a future session using the command
save str

The next time you open a MATLAB session, load str using the command
load str

and then reload the "wisdom" into the FFTW database using the command
fftw('wisdom', str)

ifft - Inverse fast Fourier transform


Syntax
y y y y y y = = = = = = ifft(X) ifft(X,n) ifft(X,[],dim) ifft(X,n,dim) ifft(..., 'symmetric') ifft(..., 'nonsymmetric')

Description
returns the inverse discrete Fourier transform (DFT) of vector X, computed with a fast Fourier transform (FFT) algorithm. If X is a matrix, ifft returns the inverse DFT of each column of the matrix.
y = ifft(X)

tests X to see whether vectors in X along the active dimension are conjugate symmetric. If so, the computation is faster and the output is real. An N-element vector x is conjugate symmetric if x(i) = conj(x(mod(N-i+1,N)+1)) for each element of x.
ifft

If X is a multidimensional array, ifft operates on the first non-singleton dimension.


y = ifft(X,n)

returns the n-point inverse DFT of vector X. and y = ifft(X,n,dim) return the inverse DFT of X across the

y = ifft(X,[],dim) dimension dim.

causes ifft to treat X as conjugate symmetric along the active dimension. This option is useful when X is not exactly conjugate symmetric, merely because of round-off error.
y = ifft(..., 'symmetric') y = ifft(..., 'nonsymmetric') 'nonsymmetric'.

is the same as calling ifft(...) without the argument

For any X, ifft(fft(X)) equals X to within roundoff error.

Algorithms
The algorithm for ifft(X) is the same as the algorithm for fft(X), except for a sign change and a scale factor of n = length(X). As for fft, the execution time for ifft depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors. Note You might be able to increase the speed of ifft using the utility function fftw, which controls how MATLAB software optimizes the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support


ifft supports inputs of data types double and single. If you call ifft ifft(X, ...), the output y has the same data type as the input X.

with the syntax y =

ifft2 - 2-D inverse fast Fourier transform


Syntax
Y Y y y = = = = ifft2(X) ifft2(X,m,n) ifft2(..., 'symmetric') ifft2(..., 'nonsymmetric')

Description
returns the two-dimensional inverse discrete Fourier transform (DFT) of X, computed with a fast Fourier transform (FFT) algorithm. The result Y is the same size as X.
Y = ifft2(X)

tests X to see whether it is conjugate symmetric. If so, the computation is faster and the output is real. An M-by-N matrix X is conjugate symmetric if X(i,j) = conj(X(mod(M-i+1, M) + 1, mod(N-j+1, N) + 1)) for each element of X.
ifft2 Y = ifft2(X,m,n)

returns the m-by-n inverse fast Fourier transform of matrix X. option is

y = ifft2(..., 'symmetric') causes ifft2 to treat X as conjugate symmetric. This useful when X is not exactly conjugate symmetric, merely because of round-off error. y = ifft2(..., 'nonsymmetric') 'nonsymmetric'.

is the same as calling ifft2(...) without the argument

For any X, ifft2(fft2(X)) equals X to within roundoff error.

Algorithms
The algorithm for ifft2(X) is the same as the algorithm for fft2(X), except for a sign change and scale factors of [m,n] = size(X). The execution time for ifft2 depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors. Note You might be able to increase the speed of ifft2 using the utility function fftw, which controls how MATLAB software optimizes the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support


ifft2 supports inputs of data types double and single. If you call ifft2 ifft2(X, ...), the output y has the same data type as the input X.

with the syntax y =

ifftn - N-D inverse fast Fourier transform


Syntax
Y Y y y = = = = ifftn(X) ifftn(X,siz) ifftn(..., 'symmetric') ifftn(..., 'nonsymmetric')

Description
returns the n-dimensional inverse discrete Fourier transform (DFT) of X, computed with a multidimensional fast Fourier transform (FFT) algorithm. The result Y is the same size as X.
Y = ifftn(X)

tests X to see whether it is conjugate symmetric. If so, the computation is faster and the output is real. An N1-by-N2-by- ... Nk array X is conjugate symmetric if
ifftn X(i1,i2, ...,ik) = conj(X(mod(N1-i1+1,N1)+1, mod(N2-i2+1,N2)+1, ... mod(Nk-ik+1,Nk)+1))

for each element of X.


Y = ifftn(X,siz) pads X with zeros, or truncates X, to create a multidimensional siz before performing the inverse transform. The size of the result Y is siz.

array of size option is

y = ifftn(..., 'symmetric') causes ifftn to treat X as conjugate symmetric. This useful when X is not exactly conjugate symmetric, merely because of round-off error.

y = ifftn(..., 'nonsymmetric') 'nonsymmetric'.

is the same as calling ifftn(...) without the argument

Tips
For any X, ifftn(fftn(X)) equals X within roundoff error.

Algorithms
ifftn(X)

is equivalent to

Y = X; for p = 1:length(size(X)) Y = ifft(Y,[],p); end

This computes in-place the one-dimensional inverse DFT along each dimension of X. The execution time for ifftn depends on the length of the transform. It is fastest for powers of two. It is almost as fast for lengths that have only small prime factors. It is typically several times slower for lengths that are prime or which have large prime factors. Note You might be able to increase the speed of ifftn using the utility function fftw, which controls how MATLAB software optimizes the algorithm used to compute an FFT of a particular size and dimension.

Data Type Support


ifftn supports inputs of data types double and single. If you call ifftn ifftn(X, ...), the output y has the same data type as the input X.

with the syntax y =

ifftshift - Inverse FFT shift


Syntax
ifftshift(X) ifftshift(X,dim)

Description
swaps the left and right halves of the vector X. For matrices, ifftshift(X) swaps the first quadrant with the third and the second quadrant with the fourth. If X is a multidimensional array, ifftshift(X) swaps "half-spaces" of X along each dimension.
ifftshift(X) ifftshift(X,dim)

applies the ifftshift operation along the dimension dim.

Note ifftshift undoes the results of fftshift. If the matrix X contains an odd number of elements, ifftshift(fftshift(X)) must be done to obtain the original X. Simply performing fftshift(X) twice will not produce X.

nextpow2 - Next higher power of 2


Syntax
p = nextpow2(A)

Description
p = nextpow2(A) returns the smallest power of two that value of A. (That is, p that satisfies 2^p >= abs(A)).

is greater than or equal to the absolute

This function is useful for optimizing FFT operations, which are most efficient when sequence length is an exact power of two.

Examples
For any integer n in the range from 513 to 1024, nextpow2(n) is 10. For vector input, nextpow2(n) returns an element-by-element result:
A = [1 2 3 4 5 9 519] nextpow2(A) ans = 0 1 2 2 3 4 10

You might also like