Lab6 NguyenHoaiNam EEACIU20104
Lab6 NguyenHoaiNam EEACIU20104
INTERNATIONAL UNIVERSITY
- Header/Footer Yes No
- Spelling Yes No
Total Score
Signature:
Date:
International University School of Electrical Engineering
Table of Contents
List of Figures
Figure 1: Result of problem 1 ............................................................................................................... 21
Figure 2: Result of problem 2 ............................................................................................................... 22
Figure 3: Result of problem 3 ............................................................................................................... 23
Figure 4: Result of problem 4a and 4b ............................................................................................. 24
Figure 5: Result of problem 4c ............................................................................................................. 25
Figure 6: Result of problem 4d ............................................................................................................. 25
Figure 7: Graph of problem 5a ............................................................................................................. 27
Figure 8: Energy calculations in matlab of problem 5b .............................................................. 27
List of Tables
Table 1: Code of problem 1 ..................................................................................................................... 13
Table 2: Code of problem 2 .................................................................................................................... 15
Table 3: Code of problem 3 .................................................................................................................... 16
Table 4: Code of problem 4 .................................................................................................................... 18
Table 5: Code of problem 5 .................................................................................................................... 20
1 Theoretical Background
𝜔. A sufficient condition for the existence of the DTFT X(ω) of a signal x[n] is
Moreover, the DTFT is always a periodic function with period 2π. In order to return
from the frequency domain ω back to the discrete-time domain n, we apply inverse DTFT.
The mathematical expression of the inverse DTFT is
The computation of the DTFT X(ω) of a signal x[n] is easily obtained directly from (1)
Example 1
Compute the DTFT X(ω) of the signal x[n] = 0. 8𝑛 , 0 ≤ n ≤ 20 and plot X(ω) over the
frequency intervals −π ≤ ω ≤ π and −5π ≤ ω ≤ 5π.
Example 2
Compute and plot the DTFT of the signal x[n] = 0.6𝑛 u[n].
samples ωk from the frequency ω. This leads to a second type of Fourier transform
appropriate for discrete-time signals, the discrete Fourier transform (DFT). The N -point DFT
of a discrete-time signal x[n] is denoted by Xk or X(k) or X(ωk). It is defined in the discrete-
time interval 0 ≤ n ≤ N − 1 and is computed according to
The DFT Xk of a sequence x[n] is a function of k and is completely specified by its values for
k = 0,1, ... , N − 1 that is, from the N values X1, X2, ... , XN−1. Typically, Xk are complex
Where
Alternatively, Xk is expressed as
where the second equality is derived from the fact that x(0)cos(0) = x(0). The imaginary
Example 3:
Compute the DFT Xk of the sequence x[n] = [1,2,2,1], 0 ≤ n ≤ 3. Plot the magnitude,
the phase, the real part, and the imaginary part of Xk.
Example 4:
Write a function that computes the DFT Xk of a sequence x[n]. Compute through your
Suppose that the DFT Xk of a discrete-time signal x[n] is known. The signal x[n] can be
derived from the N DFT points Xk, k = 0,1, ... , N − 1 by applying the inverse discrete Fourier
transform (IDFT). The IDFT of a sequence Xk is given by
Example 5:
Computing an N-point DFT or IDFT directly from its definition can be a computationally
expensive process. More specifically, looking into Equation (4) we notice that for every value
that k takes, i.e., for every Xk, k = 0, ... , N − 1 multiplications must be performed. Thus, in
order to compute the entire sequence Xk, k = 0, ... , N − 1 we must perform 𝑁 2 multiplications.
If the discrete-time signal x[n] is complex valued, things get difficult, since one multiplication
of two complex numbers requires four multiplications between real numbers. Hence,
computing the DFT (or IDFT) directly from the definition is usually too slow for real-time
applications. In order to reduce the computational effort needed, an efficient algorithm (with
many variants) is available for the DFT computation. This algorithm is called fast Fourier
transform algorithm or FFT algorithm. FFT is based on a ‘‘divide and conquer’’ technique;
that is, the original problem of N points is divided in two symmetric subproblems of N/2
points. If N/2 is even number, the problem of N/2 points is divided in two subproblems of
N/4 points.
If N is a power of 2, i.e, N = 2𝑃 then only a 2-point DFT has to be computed. The DFT Xk
of a sequence x[n] is computed in MATLAB through a FFT algorithm, with the command fft.
The syntax is X = fft(x), where x is the sequence x[n] and X is the DFT Xk.
Remark
FFT must not be confused with DFT. FFT is an algorithm that computes the DFT of a
sequence. An alternative syntax of the fft command is X = fft(x,N). Using this syntax we derive
the N-point DFT of an M-point sequence. If M > N, the sequence x[n] is truncated; while if M
< N, the sequence x[n] is zero-padded.
The IDFT of a sequence Xk is computed by the MATLAB command ifft. The syntax is x
= ifft(X) or x = ifft(X, N) if an N-point IDFT is required.
In this section, we establish the relationship between the two types of Fourier
transform that are applicable to discrete-time signals, namely, the DFT and the DTFT. It
states that the DFT Xk is actually a sampling in the frequency of the DTFT X(ω), or in other
words the DFT is a sampling of the continuous spectrum of a discrete-time signal. More
precisely, we can state that the DFT sequence Xk is equal to the DTFT X(ω) when X(ω) is
2𝜋𝑘
evaluated at the points ωk = , k = 0,1, ... , N − 1. To verify the relationship between DTFT
𝑛
and DFT, consider the truncated discrete-time signal x[n] = 0.9n, 0 ≤ n ≤ 7. The DTFT is
2𝜋𝑘
evaluated at the frequencies ωk = , k = 0,1, ... , N − 1 and is compared to the DFT of x[n].
𝑛
Example 6:
Let x[n] be a random sequence of 21 elements. Plot in the same figure the DTFT X(ω)
2𝜋𝑘
of x[n] for 0 ≤ n ≤ 2π and the DFT of x[n] versus the frequencies 𝜔𝑘 = , , k = 0,1, ... , N
𝑛
− 1.
In this section, we discuss how the CTFT is approximated by the DFT. Suppose that
X(ω) denotes the Fourier transform of a continuous-time signal x(t). The procedure followed
in order to approximate X(ω) through samples obtained from the FFT algorithm is
- The signal x(t) is sampled with sampling time T; that is, we obtain the discrete-time
signal x[nT], n = 0,1, ... , N − 1.
- The DFT Xk, k = 0,1, ... , N − 1 of the discrete-time signal x[nT] is computed.
Example 7:
Compute and plot the magnitude of the Fourier transform X(Ω) of the continuous-time
signal x(t) = 2 − 3t, 0 ≤ t ≤ 2. Also compute and plot in the same graph the magnitude of the
approximate sequence X(Ωk) for N = 128 and T = 0. 1.
2 Experimental Procedure
2.1 Problem 1:
Plot the magnitude and the phase of the DTFT X(ω) of the signal x[n] = cos(πn⁄3), 0 ≤
subplot(2, 2, 2);
plot(omega1, angle(X_omega1));
title('Phase of DTFT (?X(?)) for -3? ? ? ? 3?');
xlabel('?');
ylabel('?X(?)');
grid on;
subplot(2, 2, 4);
plot(omega2, angle(X_omega2));
title('Phase of DTFT (?X(?)) for -7? ? ? ? 7?');
xlabel('?');
ylabel('?X(?)');
grid on;
Plot the magnitude, the angle, the real part and the imaginary of the DFT Xk of the signal
clc
clear all;
close all;
% Plotting
figure;
subplot(2, 2, 1);
stem(k, magnitude, 'filled');
title('Magnitude of DFT');
xlabel('Frequency Bin');
ylabel('Magnitude');
subplot(2, 2, 2);
stem(k, angleX, 'filled');
title('Angle of DFT');
xlabel('Frequency Bin');
ylabel('Angle (radians)');
subplot(2, 2, 3);
stem(k, real_part, 'filled');
title('Real Part of DFT');
xlabel('Frequency Bin');
ylabel('Real Part');
subplot(2, 2, 4);
stem(k, imaginary_part, 'filled');
title('Imaginary Part of DFT');
xlabel('Frequency Bin');
ylabel('Imaginary Part');
% Adjust layout
sgtitle('DFT Analysis of x[n] = 3cos(2?n/3)');
Consider the sequence x[n] = 5cos(2πn⁄3), 0 ≤ n ≤ 19. Plot in the same graph the
DTFT of x[n] over the frequency interval 0 ≤ ω ≤ 2ω and the DFT of x[n] versus the
frequency points ωk = 2πk/N, k = 0,1, ... , N − 1.
clc
clear all
% Define the sequence
n = 0:19;
x_n = 5 * cos(2 * pi * n / 3);
% Plot DTFT
plot(omega, abs(X_omega), 'b', 'DisplayName', 'DTFT');
hold on;
% Plot DFT
stem(omega_k, abs(X_k), 'r', 'DisplayName', 'DFT');
hold off;
clc
clear all
close all
R = 1; % Ohms
C = 0.1; % Farads
figure;
subplot(2,1,1);
semilogx(f, 20*log10(abs(H_low)), 'b', f,
20*log10(abs(H_high)), 'r');
title('Magnitude Response of Low-Pass and High-Pass Filters');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
legend('Low-Pass Filter', 'High-Pass Filter');
grid on;
subplot(2,1,2);
semilogx(f, angle(H_low), 'b', f, angle(H_high), 'r');
title('Phase Response of Low-Pass and High-Pass Filters');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
legend('Low-Pass Filter', 'High-Pass Filter');
grid on;
figure;
subplot(2,1,1);
plot(t, Vi, 'k', t, Vo_low, 'b');
subplot(2,1,2);
plot(t, Vi, 'k', t, Vo_high, 'r');
title('Response of High-Pass Filter to Square Wave');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Input (Vi)', 'High-Pass Output (Vo)');
grid on;
2.5 Problem 5
clc
clear all
close all
% Given sequence parameters
N = 50;
n = 0:N-1;
x = 0.7 .^ n;
% Compute DTFT
omega = linspace(0, 2 * pi, 1000);
X_omega = arrayfun(@(w) sum(x .* exp(-1j * w * n)), omega);
% Compute DFT
X_k = fft(x);
3 Experimental Results
3.1 Problem 1
3.2 Problem 2
Comment: In this problem, I creates a vector n containing integers from 0 to 64 for 0< n < 64 then
I define the signal x_n. Function ‘fft’ computes the DFT of the signal x_n using the Fast Fourier
Transform (FFT) algorithm. Respectively,
magnitude = abs(X_k) for computes the magnitude of each frequency component.
angleX = angle(X_k) for computes the phase angle
real_part = real(X_k) to extracts the real part of frequency
imaginary_part = imag(X_k) to extracts the imaginary part
Next, I creates a vector k representing the frequency, ranging from 0 to the length of (X_k-1).
Finally, I plot all the result I get by using stem and subplot combine as I show on the figure 2
3.3 Problem 3
omega = linspace(0, 2 * pi, 1000) to generates 1000 equally spaced points between
0 and 2π for the DTFT frequency range.
Finnaly I plot the graph using stem(omega_k, abs(X_k), 'r', 'DisplayName', 'DFT')
By plotting both the DTFT and DFT, we can observe how the continuous frequency
spectrum (DTFT) relates to the discrete frequency spectrum (DFT) obtained by sampling the
sequence.
3.4 Problem 4
Comment: In problem 4, first I define the value of C and R then I create a logarithmically
spaced vector of 1000 points ranging from 100 (1 Hz) to 104 (10 kHz).
As the figure from the problem 4, we can see that is a low Pass Filter and the transfer
function H(f) is given by
When we interchange the position of R to C and C to R the circuit become High Pass
Filter, now the transfer function is given by
H_low = 1 ./ (1 + 1i * 2 * pi * f * R * C);
For question c, I Sets the sampling frequency to 2000 Hz and frequency of the square wave
1
to 7 Hz then create a time vector t from 0 to 1 second with a sampling interval of . Next, I
𝑓𝑠
generate a square wave signal with a frequency of 7 Hz using the time vector t ‘Vi = square(2
* pi * f_square * t)’
3.5 Problem 5
Comment: First, I define the squence x_n and create a vector n from 0 to 49. I generate
1000 equally spaced points between 0 and 2π for the DTFT frequency range by function
linspace. Next I compute the DTFT of x at each frequency in omega X_omega =
arrayfun(@(w) sum(x .* exp(-1j * w * n)), omega) based on formula ∑𝑁−1
𝑛=0 𝑥[𝑛]𝑒
−𝑗𝑤𝑛
After that, I use plot(omega, abs(X_omega), 'LineWidth', 1.5) to plot the magnitude
of the DTFT∣X(ω)∣ versus omega with a line width of 1.5 and stem(2 * pi * (0:N-1) / N,
abs(X_k), 'r', 'filled') to plots the magnitude of the DFT∣X k ∣
energy_dft = (1/N) * sum(abs(X_k).^2): Computes the energy using the DFT. The
energy is normalized by 1/N to account for the length of the DFT. The formula used here is
1
∑𝑁−1
𝑛=0 |𝑥[𝑘]|
2
𝑁
By plotting both the DTFT and DFT, we can observe how the continuous frequency
spectrum (DTFT) relates to the discrete frequency spectrum (DFT) obtained by sampling the
sequence. The energy calculations further validate the consistency between time and
frequency domain representations
4 Discussion of Results
In lab section 5, we had practice the problem about the Discrete-Time Fourier Transform. After
this lab student gained practical and theoretical knowledge about discrete-time signals and their
frequency representations. We learned how to compute and visualize the DTFT and DFT, validate
theoretical concepts such as Parseval's theorem, and apply these techniques using MATLAB.
These skills are essential for anyone working in digital signal processing, communications, or
related fields.