how to do dtft in matlab

In MATLAB, we can calculate the Discrete Time Fourier Transform (DTFT) of a discrete sequence x(n) using the fft function. The DTFT of x(n) can be obtained as follows:

main.m
N = length(x);           % length of input sequence
n = 0:N-1;               % time vector
X = fft(x);              % DTFT of x(n)
w = 2*pi*(0:N-1)/N;      % frequency vector
175 chars
5 lines

Here, N is the length of the input sequence x. The fft function calculates the FFT of the sequence x, which is the DTFT of x sampled at N frequencies. The w vector contains the frequencies at which the DTFT is sampled.

Note that the DTFT is a continuous function of frequency and hence, it is represented by a continuous curve in the frequency domain. To plot the magnitude and phase of the DTFT, we can use the plot function as follows:

main.m
subplot(2,1,1);
plot(w, abs(X));
xlabel('Frequency (\omega)');
ylabel('Magnitude |X(e^{j\omega})|');
title('Magnitude Response of DTFT');
grid on;

subplot(2,1,2);
plot(w, angle(X));
xlabel('Frequency (\omega)');
ylabel('Phase \angle X(e^{j\omega})');
title('Phase Response of DTFT');
grid on;
294 chars
14 lines

This will plot the magnitude and phase of the DTFT on separate subplots.

Alternatively, we can use the freqz function to plot the magnitude and phase of the DTFT as follows:

main.m
[b, a] = butter(5, 0.4);     % design a lowpass filter
[h, w] = freqz(b, a, 'whole'); % DTFT of filter
subplot(2,1,1);
plot(w/pi, abs(h));
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Magnitude Response |H(e^{j\omega})|');
title('Magnitude Response of DTFT');
grid on;

subplot(2,1,2);
plot(w/pi, angle(h));
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Phase Response \angle H(e^{j\omega})');
title('Phase Response of DTFT');
grid on;
471 chars
16 lines

This will plot the magnitude and phase of the DTFT of a filter designed using the butter function. Note that freqz plots the normalized frequency in the range [-pi, pi] (or [-0.5, 0.5] cycles/sample).

related categories

gistlibby LogSnag