Computation of the DTFT Using MATLAB

10.2.3 Computation of the DTFT Using MATLAB

According to the definitions of the direct and the inverse DTFT, their computation needs to be done for a continuous frequency ω ∈ [−π, π) and requires integration. In MATLAB the DTFT is approxi- mated in a discrete set of frequency values, and summation instead of integration is used. As we will see later, this can be done by sampling in frequency the DTFT to obtain the discrete Fourier transform (DFT), which in turn is efficiently implemented by an algorithm called the Fast Fourier Transform (FFT). We will introduce the FFT in Chapter 12, and so for now consider the FFT as a black box capable of giving a discrete approximation of the DTFT.

To understand the use of the MATLAB function fft in the script below consider the following issues:

The command

X = fft(x) , where x is a vector with the entries the sample values x[n], n = 0, . . . , L − 1, computes the FFT values X[k], k = 0, . . . , L − 1, or the DTFT X(e jω ) at discrete frequencies {2πk/L}.

The {k} values correspond to the discretized frequencies {ω k = 2πk/L}, which go from 0 to 2π(L − 1)/L (close to 2π for large L). This is a discretization of the frequency ω ∈ [0, 2π).

To find an equivalent representation of the frequency ω ∈ [−π, π), we simply subtract π from ω k = 2πk/L to get a band of frequencies,

2k −L

˜ω k =ω k −π=π

,k = 0, . . . , L − 1 or − π ≤ ˜ω k <π

The frequency ˜ω can be normalized to [−1, 1) with no units by dividing by π. This change in the frequency scale requires a corresponding shift of the magnitude and the phase spectra. This is done by means of the MATLAB function fftshift .

When plotting the signal, which is discrete in time, the function stem is more appropriate than plot . However, the plot function is more appropriate for plotting the magnitude and the phase frequency response functions, which are supposed to be continuously varying with respect to

frequency.

C H A P T E R 10: Fourier Analysis of Discrete-Time Signals and Systems

The function abs computes the magnitude and the function angle computes the phase of the frequency response. The magnitude and the phase are even and odd symmetric when plotted in ω ∈ [−π, π) or in the normalized frequency ω/π ∈ [−1, 1).

The three signals in the following script are a rectangular pulse, a windowed sinusoid, and a chirp. In the script, to process one of the signals you delete the corresponding comment % and keep it for the other two. The length of the FFT is set to L = 256, which is larger or equal to the length of either of the three signals.

%%%%%%%%%%%%%%%%%%%%%% % DTFT of aperiodic signals %%%%%%%%%%%%%%%%%%%%%% % signals L = 256; % length of FFT with added zeros % N = 21; x = [ones(1,N) zeros(1,L - N)];

% pulse

% N = 200; n = 0:N - 1; x = [cos(4 ∗ pi ∗ n/N) zeros(1,L - N)]; % windowed sinusoid n = 0:L - 1; x = cos(pi ∗ n. ˆ 2/(4 ∗ L));

% chirp

X = fft(x); w = 0:2 ∗ pi/L:2 ∗ pi - 2 ∗ pi/L;w1 = (w - pi)/pi; % normalized frequency n = 0:length(x) - 1; subplot(311) stem(n,x); axis([0 length(n) - 1 1.1 ∗ min(x) 1.1 ∗ max(x)]); grid; xlabel(‘n’); ylabel(‘x(n)’) subplot(312) plot(w1,fftshift(abs(X))); axis([min(w1) max(w1) 0 1.1 ∗ max(abs(X))]); ylabel(‘ |X|’); grid subplot(313) plot(w1,fftshift(angle(X))); ylabel(‘<X’); xlabel(‘ω/π ’); grid axis([min(w1) max(w1) 1.1 ∗ min(angle(X)) 1.1 ∗ max(angle(X))])

As expected, the magnitude spectrum for the rectangular pulse is like a sinc. The windowed sinusoid has a spectrum that resembles that of the sinusoid but the rectangular window makes it broader. Finally, a chirp is a sinusoid with time-varying frequency; thus its magnitude spectrum displays com- ponents over a range of frequencies. We will comment on the phase spectra later. The results are shown in Figure 10.1.