FFT in Matlab
FFT in Matlab
FFT in Matlab
The window length is the length of the input data vector. It is determined by, for example, the size of an external buffer. The transform length is the length of the output, the computed DFT. An FFT algorithm pads or chops the input to achieve the desired transform length. The following figure illustrates the two lengths.
The execution time of an FFT algorithm depends on the transform length. It is fastest when the transform length is a power of two, and almost as fast when the transform length has only small prime factors. It is typically slower for transform lengths that are prime or have large prime factors. Time differences, however, are reduced to insignificance by modern FFT algorithms such as those used in MATLAB. Adjusting the transform length for efficiency is usually unnecessary in practice. Back to Top
Introduction Example: Basic Spectral Analysis Example: Spectral Analysis of a Whale Call Example: Data Interpolation
Introduction The MATLAB fft function returns the DFT y of an input vector x using a fast Fourier transform algorithm:
y = fft(x);
In this call to fft, the window length m = length(x) and the transform length n = length(y) are the same. The transform length is specified by an optional second argument:
y = fft(x,n);
In this call to fft, the transform length is n. If the length of x is less than n, x is padded with trailing zeros to increase its length to n before computing the DFT. If the length of x is greater than n, only the first n elements of x are used to compute the transform. Example: Basic Spectral Analysis The FFT allows you to efficiently estimate component frequencies in data from a discrete set of values sampled at a fixed rate. Relevant quantities in a spectral analysis are listed in the following table. For space-based data, replace references to time with references to space. Quantity
x m = length(x) fs dt = 1/fs t = (0:m-1)/fs y = fft(x,n) abs(y) (abs(y).^2)/n fs/n f = (0:n-1)*(fs/n) fs/2
Description Sampled data Window length (number of samples) Samples/unit time Time increment per sample Time range for data Discrete Fourier transform (DFT) Amplitude of the DFT Power of the DFT Frequency increment Frequency range Nyquist frequency
For example, consider the following data x with two component frequencies of differing amplitude and phase buried in noise:
fs = 100; t = 0:1/fs:10-1/fs; x = (1.3)*sin(2*pi*15*t) ... + (1.7)*sin(2*pi*40*(t-2)) ... + (2.5)*randn(size(t)); % % % % % Sample frequency (Hz) 10 sec sample 15 Hz component 40 Hz component Gaussian noise;
finds the exponent of the next power of two greater than or equal to the window length (ceil(log2(m))), and pow2 computes the power. Using a power of two for the transform length optimizes the FFT algorithm, though in practice there is usually little difference in execution time from using n = m.
nextpow2
To visualize the DFT, plots of abs(y), abs(y).^2, and log(abs(y)) are all common. A plot of power versus frequency is called a periodogram:
plot(f,power) xlabel('Frequency (Hz)') ylabel('Power') title('{\bf Periodogram}')
The first half of the frequency range (from 0 to the Nyquist frequency fs/2) is sufficient to identify the component frequencies in the data, since the second half is just a reflection of the first half. In many applications it is traditional to center the periodogram at 0. The fftshift function rearranges the output from fft with a circular shift to produce a 0-centered periodogram:
y0 = fftshift(y); f0 = (-n/2:n/2-1)*(fs/n); power0 = y0.*conj(y0)/n; plot(f0,power0) xlabel('Frequency (Hz)') % Rearrange y values % 0-centered frequency range % 0-centered power
The rearrangement makes use of the periodicity in the definition of the DFT (see Discrete Fourier Transform (DFT)). Use the MATLAB angle and unwrap functions to create a phase plot of the DFT:
phase = unwrap(angle(y0)); plot(f0,phase*180/pi) xlabel('Frequency (Hz)') ylabel('Phase (Degrees)') grid on
Component frequencies are mostly hidden by the randomness in phase at adjacent values. The upward trend in the plot is due to the unwrap function, which in this case adds 2 to the phase more often than it subtracts it.