# DFT Example with Matlab

The Discrete Fourier Transform (DFT) is a powerful mathematical tool used to analyze the frequency content of discrete-time signals. The DFT allows us to transform a discrete-time signal from the time domain to the frequency domain. It decomposes a signal into its constituent frequency components, revealing the amplitude and phase information associated with each frequency. This transformation is widely used in various fields such as signal processing, communications, audio processing, digital filter design, and image analysis. Here, we will explore the concept of DFT and provide a step-by-step example using MATLAB to compute and visualize the frequency response of a given signal.

### MATLAB DFT Implementation

Computing the DFT: To demonstrate the DFT computation in MATLAB, we will consider a simple example. Let's assume we have an impulse response h[n] given by [1, 1, -1, 1, 2, 3]. We will compute the DFT using the built-in fft function in MATLAB. To calculate DFT without built-in matlab function see DFT in Matlab without built-in function.

### Visualizing the Frequency Response

Once we have computed the DFT, we can visualize the frequency response using MATLAB's plotting capabilities. We will plot both the magnitude response and the phase response.

### DFT Example Code

Below is Matlab code for calculating the DFT of a impulse response sequence data, displaying the magnitude and phase values and for plotting the magnitude and phase response.

h = [1, 1, -1, 1, 2, 3]; % Impulse response

N = length(h); % Length of the impulse response
H = fft(h); % Compute the Discrete Fourier Transform (DFT) of the impulse response

magnitudeResponse = abs(H); % Calculate the magnitude response values
phaseResponse = angle(H); % Calculate the phase response values

% Plotting the magnitude response
frequencyRange = (0:N-1) * (1/N); % Frequency range
subplot(2, 1, 1);
stem(frequencyRange, magnitudeResponse);
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Response');

% Plotting the phase response
subplot(2, 1, 2);
stem(frequencyRange, phaseResponse);
xlabel('Frequency');
ylabel('Phase');
title('Phase Response');

sgtitle('Frequency Response');
disp(magnitudeResponse);
disp(phaseResponse);


In MATLAB, you can calculate the magnitude response values using the built-in fft function. Here's how you can calculate the magnitude response values for the given impulse response [1, 1, -1, 1, 2, 3]:

h = [1, 1, -1, 1, 2, 3]; % Impulse response

N = length(h); % Length of the impulse response
H = fft(h); % Compute the Discrete Fourier Transform (DFT) of the impulse response
magnitudeResponse = abs(H); % Calculate the magnitude response values

disp(magnitudeResponse); 

When you run this code, you will get the magnitude response values as follows:

7.0000    4.5826    1.0000    3.0000    1.0000    4.5826

These values correspond to the magnitude response at each frequency component in the given impulse response.

Note that MATLAB's fft function returns a complex-valued spectrum, so we use the abs function to calculate the magnitude (absolute value) of each complex number.

Similarly we can calculate the phase response.

When you run this code, it will plot both the magnitude response and the phase response in two subplots. The x-axis represents the frequency components, and the y-axis represents the magnitude and phase values, respectively.

The magnitude response is plotted using stem function, and the phase response is also plotted using stem. The sgtitle function is used to add a common title for the two subplots.

Note that the phase values returned by the angle function are in radians.

Conclusion

Here, we explored the concept of the Discrete Fourier Transform (DFT) and its significance in analyzing the frequency content of discrete-time signals. We provided a step-by-step example using MATLAB to compute and visualize the frequency response of a given signal. The DFT is a fundamental tool for understanding signal properties and is widely used in various applications.

We hope this tutorial has enhanced your understanding of the DFT and its practical application in signal analysis. Experiment with different signals and explore further applications of the DFT to deepen your knowledge in this important area of signal processing.

References