dtft in matlab

To compute the Discrete-Time Fourier Transform (DTFT) of a signal in Matlab, you can use the fft function. However, since the DTFT is a continuous function of frequency, it cannot be represented by a finite number of samples. Therefore, the fft function in Matlab computes a sampled version of the DTFT.

The basic syntax for computing the DTFT in Matlab is:

main.m
X = fft(x);
12 chars
2 lines

where x is the input signal and X is the output DTFT. By default, fft computes the DTFT for frequencies ranging from 0 to 2*pi in steps of 2*pi/N, where N is the length of the input signal x. To shift the frequency range to be centered at zero, you can use the fftshift function as follows:

main.m
X_shifted = fftshift(X);
25 chars
2 lines

This will move the zero-frequency component of the DTFT to the center of the output vector X_shifted.

Here is an example of computing the DTFT of a simple sine wave:

main.m
% generate a sine wave
f = 0.1;
t = 0:0.01:10;
x = sin(2*pi*f*t);

% compute the DTFT and shift the frequency range
X = fft(x);
X_shifted = fftshift(X);

% plot the magnitude and phase of the DTFT
frequencies = linspace(-pi,pi,length(x));
subplot(2,1,1), plot(frequencies, abs(X_shifted))
xlabel('Frequency (rad/sample)'), ylabel('Magnitude')
subplot(2,1,2), plot(frequencies, angle(X_shifted))
xlabel('Frequency (rad/sample)'), ylabel('Phase')
445 chars
16 lines

This will generate a plot showing the magnitude and phase of the DTFT of the sine wave.

gistlibby LogSnag