The Matlab demonstration program Discrete Fourier Transform is also helpful. It may be accessed by typing demo at the Matlab prompt.
The dft function computes magnitudes and phases for a signal’s spectrum or a system’s frequency response using the DFT formula directly. It is used as follows:
[mag,phase,k] = dft(x)  where x is a signal, or [mag,phase,k] = dft(h)
where h is an impulse response. The dft function is functionally identical to the dfs function from the Matlab section for chapter 8. The results of dft can be plotted using the function plotdft, which is functionally identical to plotdfs. For example,
x = [1 –2 3 0 4 –1 5 0]; [mag,phase,k] = dft(x); plotdft(mag,phase,k,1) Magnitude Spectrum
Phase Spectrum
The function fft computes the same outputs as the function dft, but does so more efficiently. The fft is calculated and plotted as follows: xx = fft(x); mag = abs(xx);
phase = angle(xx); k = 0:(length(xx)-1); plotfft(mag,phase,k,1);
Note that, like plotdft, the function plotfft is functionally identical to plotdfs. The FFT spectrum is shown below for the signal produced by the command
x = rand(1,32) – 0.5; Signal
Magnitude Spectrum
Phase Spectrum
When the FFT for a signal is known, the signal can be found from the complex FFT values using the ifft function. From the example above,  check_x = ifft(xx); and check_x equals x.
The function winfft permits windowing options for the FFT to be explored. For each window type, the window function multiplies the signal samples before the FFT is taken. For the rectangular, Hanning, Hamming and Blackman windows, the form is:
[mag,phase,k] = winfft(x,'rect'); [mag,phase,k] = winfft(x,'hanning'); [mag,phase,k] = winfft(x,'hamming');
[mag,phase,k] = winfft(x,'blackman'); For the Kaiser window, the form is:
[mag,phase,k] = winfft(x,'kaiser',beta);
Spectra for digital images may be obtained from a matrix of gray scale levels using Matlab’s fft2 function. Images are obtained from spectra using ifft2. Examples of these functions are provided in the Matlab section for chapter 15.
[Home] [Using the CD] [Chapters] [Matlab] [Software]