Fft help 2

Embed Size (px)

Citation preview

  • 7/28/2019 Fft help 2

    1/21

    fft - Fast Fourier transform

    Syntax

    Y = fft(x)Y = fft(X,n)

    Y = fft(X,[],dim)

    Y = 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 lengthNby:

    where

    is anNth root of unity.

    Description

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

    If the input X is a matrix, Y = fft(X) returns the Fourier transform of each column of thematrix.

    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 sizeofX in the first nonsingleton dimension. If the length ofX is less than n, X is padded with trailing

    zeros to length n. If the length ofX is greater than n, the sequence X is truncated. When X is amatrix, the length of the columns are adjusted in the same manner.

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

    dim.

  • 7/28/2019 Fft help 2

    2/21

    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 zero-

    mean 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 takingthe fast Fourier transform (FFT):

    NFFT = 2^nextpow2(L); % Next power of 2 from length of y

    Y = fft(y,NFFT)/L;

  • 7/28/2019 Fft help 2

    3/21

    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 ofy) will produce different approximations to

    0.7 and 1. The other reason is that you have a finite length signal. Increasing L from 1000 to10000 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 anN-point DFT whenNis composite (that is, whenN=N1N2), the FFTW

    library decomposes the problem using the Cooley-Tukey algorithm [1], which first computesN1transforms of sizeN2, and then computesN2 transforms of sizeN1. The decomposition is applied

    recursively to both theN1- andN2-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 split-radix algorithm [2]. The particular factorization ofNis chosen heuristically.

  • 7/28/2019 Fft help 2

    4/21

    WhenNis 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 decompositiondescribed above to compute the (N 1)-point DFTs.

    For mostN, real-input DFTs require roughly half the computation time of complex-input DFTs.

    However, whenNhas 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 timesslower for lengths that are prime or which have large prime factors.

    Note You might be able to increase the speed offft 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 Supportfft supports inputs of data types double and single. If you call fft with the syntax y =

    fft(X, ...), the output y has the same data type as the input X.

    References

    [1] Cooley, J. W. and J. W. Tukey, "An Algorithm for the Machine Computation of the ComplexFourier 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 theArt," 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.

  • 7/28/2019 Fft help 2

    5/21

    fft2 - 2-D fast Fourier transform

    Syntax

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

    Description

    Y = fft2(X) returns the two-dimensional discrete Fourier transform (DFT) ofX, computed with

    a fast Fourier transform (FFT) algorithm. The result Y is the same size as X.

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

    transform. The result is m-by-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. Itis 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 offft2 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 with the syntax y =

    fft2(X, ...), the output y has the same data type as the input X.

  • 7/28/2019 Fft help 2

    6/21

    fftn - N-D fast Fourier transform

    Syntax

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

    Description

    Y = fftn(X) returns the discrete Fourier transform (DFT) ofX, computed with a

    multidimensional fast Fourier transform (FFT) algorithm. The result Y is the same size as X.

    Y = fftn(X,siz) pads X with zeros, or truncates X, to create a multidimensional array of size

    siz before performing the transform. The size of the result Y is siz.

    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 ofX.

    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 timesslower for lengths that are prime or which have large prime factors.

    Note You might be able to increase the speed offftn using the utility function fftw,which controls the optimization of the algorithm used to compute an FFT of a particularsize and dimension.

    Data Type Support

    fftnsupports inputs of data types

    doubleand

    single. If you call

    fftnwith the syntax

    y =

    fftn(X, ...), the output y has the same data type as the input X.

  • 7/28/2019 Fft help 2

    7/21

    fftshift - Shift zero-frequency component to

    center of spectrum

    SyntaxY = fftshift(X)

    Y = fftshift(X,dim)

    Description

    Y = fftshift(X) rearranges the outputs offft, 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.

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

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

    Y = fftshift(X,dim) applies the fftshift operation along the dimension dim.

    Note ifftshift will undo the results offftshift. 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.

  • 7/28/2019 Fft help 2

    8/21

    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-leftcorner 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 odd-length.

    N = 5;X = 0:N-1;

    Y = fftshift(fftshift(X));

    Z = ifftshift(fftshift(X));

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

    isequal(X,Y),isequal(X,Z)

    ans =

    0

    ans =

    1

  • 7/28/2019 Fft help 2

    9/21

    fftw - Interface to FFTW library run-time

    algorithm tuning control

    Syntaxfftw('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 runtime. 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.

    fftw('planner', method) 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:

    '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 theFFT 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 selectthe algorithms. The resulting algorithms might not be optimal.

  • 7/28/2019 Fft help 2

    10/21

    If you set the method to 'measure', the FFTW library experiments with many differentalgorithms 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.

    method = fftw('planner') returns the current planner method.

    str = fftw('dwisdom') returns the information in the FFTW library's internal double-

    precision database as a string. The string can be saved and then later reused in a subsequentMATLAB session using the next syntax.

    str = fftw('swisdom') returns the information in the FFTW library's internal single-precision

    database as a string.

    fftw('dwisdom', str) 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('swisdom', 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.

    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 tooptimize 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 ofplanner. 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));

  • 7/28/2019 Fft help 2

    11/21

    tic; Y = fft(y,1458); toc

    Elapsed time is 0.000521 seconds.

    If you execute the commands

    tic; Y = fft(y,1458); tocElapsed 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 aloop.

    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 singletime.

    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

  • 7/28/2019 Fft help 2

    12/21

    and then reload the "wisdom" into the FFTW database using the command

    fftw('wisdom', str)

  • 7/28/2019 Fft help 2

    13/21

    ifft - Inverse fast Fourier transform

    Syntax

    y = ifft(X)y = ifft(X,n)

    y = ifft(X,[],dim)

    y = ifft(X,n,dim)

    y = ifft(..., 'symmetric')

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

    Description

    y = ifft(X) returns the inverse discrete Fourier transform (DFT) of vector X, computed with a

    fast Fourier transform (FFT) algorithm. IfX is a matrix, ifft returns the inverse DFT of each

    column of the matrix.

    ifft 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

    ifx(i) = conj(x(mod(N-i+1,N)+1)) for each element ofx.

    IfX 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.

    y = ifft(X,[],dim) and y = ifft(X,n,dim) return the inverse DFT ofX across the

    dimension dim.

    y = ifft(..., 'symmetric') 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 ofround-off error.

    y = ifft(..., 'nonsymmetric') is the same as calling ifft(...) without the argument

    'nonsymmetric'.

    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 ofn = 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 onlysmall prime factors. It is typically several times slower for lengths that are prime or which have

    large prime factors.

  • 7/28/2019 Fft help 2

    14/21

    Note You might be able to increase the speed ofifft 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 Supportifft supports inputs of data types double and single. If you call ifft with the syntax y =

    ifft(X, ...), the output y has the same data type as the input X.

  • 7/28/2019 Fft help 2

    15/21

    ifft2 - 2-D inverse fast Fourier transform

    Syntax

    Y = ifft2(X)Y = ifft2(X,m,n)

    y = ifft2(..., 'symmetric')

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

    Description

    Y = ifft2(X) returns the two-dimensional inverse discrete Fourier transform (DFT) ofX,

    computed with a fast Fourier transform (FFT) algorithm. The result Y is the same size as X.

    ifft2 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 ifX(i,j) = conj(X(mod(M-i+1, M)+ 1, mod(N-j+1, N) + 1)) for each element ofX.

    Y = ifft2(X,m,n) returns the m-by-n inverse fast Fourier transform of matrix X.

    y = ifft2(..., 'symmetric') causes ifft2 to treat X as conjugate symmetric. This option is

    useful when X is not exactly conjugate symmetric, merely because of round-off error.

    y = ifft2(..., 'nonsymmetric') is the same as calling ifft2(...) without the argument

    'nonsymmetric'.

    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 ofthe 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 ofifft2 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

  • 7/28/2019 Fft help 2

    16/21

    ifft2 supports inputs of data types double and single. If you call ifft2 with the syntax y =

    ifft2(X, ...), the output y has the same data type as the input X.

  • 7/28/2019 Fft help 2

    17/21

    ifftn - N-D inverse fast Fourier transform

    Syntax

    Y = ifftn(X)Y = ifftn(X,siz)

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

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

    Description

    Y = ifftn(X) returns the n-dimensional inverse discrete Fourier transform (DFT) ofX,

    computed with a multidimensional fast Fourier transform (FFT) algorithm. The result Y is the

    same size as X.

    ifftn tests X to see whether it is conjugate symmetric. If so, the computation is faster and theoutput is real. An N1-by-N2-by- ... Nk array X is conjugate symmetric if

    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 ofX.

    Y = ifftn(X,siz) pads X with zeros, or truncates X, to create a multidimensional array of size

    siz before performing the inverse transform. The size of the result Y is siz.

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

    y = ifftn(..., 'nonsymmetric') is the same as calling ifftn(...) without the argument

    'nonsymmetric'.

    Tips

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

    Algorithmsifftn(X) is equivalent to

    Y = X;

    for p = 1:length(size(X))

    Y = ifft(Y,[],p);

    end

  • 7/28/2019 Fft help 2

    18/21

    This computes in-place the one-dimensional inverse DFT along each dimension ofX.

    The execution time for ifftn depends on the length of the transform. It is fastest for powers oftwo. 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 ofifftn 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 with the syntax y =

    ifftn(X, ...), the output y has the same data type as the input X.

  • 7/28/2019 Fft help 2

    19/21

    ifftshift - Inverse FFT shift

    Syntax

    ifftshift(X)ifftshift(X,dim)

    Description

    ifftshift(X) 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" ofX along each dimension.

    ifftshift(X,dim) applies the ifftshift operation along the dimension dim.

    Note ifftshift undoes the results offftshift. 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.

  • 7/28/2019 Fft help 2

    20/21

    nextpow2 - Next higher power of 2

    Syntax

    p = nextpow2(A)

    Description

    p = nextpow2(A) returns the smallest power of two that is greater than or equal to the absolute

    value ofA. (That is, p that satisfies 2^p >= abs(A)).

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

    ExamplesFor 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

  • 7/28/2019 Fft help 2

    21/21