sns_lab_8
sns_lab_8
sns_lab_8
Computer Science
Group Members
Syeda Fatima
334379
Zahra
The goal of this laboratory is to be able to calculate the Fourier series of discrete time signals and plot
the real part of the spectrum / Fourier series coefficients.
2.2 Equipment
Software
• MATLAB
All questions should be answered precisely to get maximum credit. Lab report must ensure following
items:
• Lab objectives
• MATLAB codes
• Results (Graphs/Tables) duly commented and discussed
• Conclusion
Write a function that will generate a single Discrete Time (DT) sinusoid 𝑥[𝑛] = 𝐴 sin[𝑤𝑛] =
𝑓
𝐴 sin [2𝜋 𝐹 𝑛]. 𝐴 = 3, 𝑓 = 500Hz and sampling frequency 𝐹𝑠 = 8000Hz.
𝑠
n_definition = 0:1:N;
x = A * sin(2 * pi * (f / Fs) * n_definition);
subplot(2, 2, 1)
stem(n_definition, x, 'filled')
title('Original Signal x[n]')
xlabel('n')
grid
n_obv = 0:1:15;
x_n = A * sin(2 * pi * (f / Fs) * n_obv);
length(n_obv);
ak = zeros(1, 41);
length(ak);
for k = -20:1:20
for n = 0:1:15
ak(k + 21) = ak(k + 21) + (x_n(n + 1) ...
* (exp(-1i * k * (2 * pi * (1 / N)) * n))) / N;
end
end
k_axis = -20:20;
length(k_axis);
mag_ak = abs(ak);
tol = 1e-6;
for k = -20:1:20
if abs(ak(k + 21)) < tol
ak(k + 21) = 0;
end
end
phase_ak = angle(ak);
% subplot(2, 2, 2)
% stem(k_axis, mag_ak);
for k = -20:20
frequency_radians(k + 21) = 2 * pi * (k / N);
end
for k = -20:20
frequency_Hertz(k + 21) = (k / N);
end
subplot(2,2,2)
stem(frequency_radians, mag_ak)
title('Magnitude of a_{k}')
xlabel('Frequency (in Radians)')
grid
subplot(2,2,3)
stem(frequency_Hertz, mag_ak)
title('Magnitude of a_{k}')
xlabel('Frequency (in Hertz)')
grid
for n = 0:N
for k = -7:1:8
x_recon(n + 1) = x_recon(n + 1) + (ak(k + 21) ...
.* (exp(1i * k * (2 * pi * (1 / N)) * n)));
end
end
n_axis = 0:1:N;
subplot(2, 2, 4)
stem(n_axis, x_recon, 'filled')
title('Reconstructed Signal x[n]')
xlabel('n')
grid
2𝜋𝑚 𝐹𝑠
𝑃𝑒𝑟𝑖𝑜𝑑 𝑁 = = = 16
𝜔 𝑓
2𝜋
1
2. For calculating the DT Fourier series coefficients use 𝑎𝑘 = 𝑁 ∑𝑛=<𝑁> 𝑥[𝑛]𝑒 −𝑗𝑘( 𝑁 )𝑛 . It will return
the value of the 𝑘 th DTFS coefficient. For remaining coefficients, you may use a loop to iterate
over values of 𝑘. What will be the range of 𝑘?
𝑅𝑎𝑛𝑔𝑒 𝑘 = −20: 20
3. You must make two plots of the coefficients against frequency axis instead of 𝑘. One plot should
be against frequency in radians. Other plot should be against frequency in Hz.
Assume a rectangular wave as shown below. Using a similar approach outlined in the previous task,
obtain the DTFS representation of the rectangular wave. Plot the magnitude and phase of the Fourier
series coefficients with appropriate axes, labels, and titles.
function [] = discreteSquare()
N = 10; % Period N
n_definition = 1:1:N;
x = [0 0 0 0 0 1 1 1 1 1]; % 1 - 10
subplot(2, 2, 1)
stem(n_definition, x, 'filled');
title('Original Function x[n]')
xlabel('n');
axis([1 N -0.25 1.25]);
ak = zeros(1, 35);
length(ak);
for k = -17:1:17
for n = 1:1:N
ak(k + 18) = ak(k + 18) + (x(n) ...
* (exp(-1i * k * (2 * pi * (1 / N)) * n))) / N;
k_axis = -17:1:17;
length(k_axis);
mag_ak = abs(ak);
tol = 1e-6;
for k = -17:1:17
if abs(ak(k + 18)) < tol
ak(k + 18) = 0;
end
end
phase_ak = phase(ak);
subplot(2, 2, 2)
stem(k_axis, mag_ak);
title('Magnitude Spectrum');
xlabel('k')
grid
subplot(2, 2, 3)
stem(k_axis, phase_ak);
title('Phase Spectrum');
xlabel('k')
grid
for n = 1:N
for k = 1:1:N
x_recon(n + 1) = x_recon(n + 1) + (ak(k + 18) ...
.* (exp(1i * k * (2 * pi * (1 / N)) * n)));
end
end
n_axis = 0:1:N;
subplot(2, 2, 4)
stem(n_axis, x_recon, 'filled')
title('Reconstructed Signal x[n]')
xlabel('n')
grid
1. Determine the period, 𝑁, of this sinosoid? Determine the DT Fourier series coefficients and plot
the magnitude and phase of the DT Fourier series coefficients.
𝑃𝑒𝑟𝑖𝑜𝑑 𝑁 = 10
𝑅𝑎𝑛𝑔𝑒 𝑘 = −17: 17
3. You must make two plots of the coefficients against frequency axis instead of 𝑘.
2𝜋
4. 𝑘-th coefficient corresponds to frequency 𝑘. Determine the range in radians of the distinct
𝑁
frequency components?
Assume a rectangular wave as shown below. Using a similar approach outlined in the previous task,
obtain the DTFS representation of the rectangular wave. Plot the magnitude and phase of the DT
Fourier series coefficients with appropriate axes, labels, and titles. What differences can you note from
the frequency representation of previous waveform.
Keeping all the previous code the same, except now, original function is changed to:
x = [0 0 0 0 0 0 0 0 1 1]; % 1 - 10
4 Conclusion
After the conduct of this lab, we have determined that MATLAB can be effectively used for
determining Fourier series of Discrete Time signals and any number of coefficients can be obtained.
However, a given DTFS only has a finite number of coefficients equal to the period of the signal. Also,
we proved the 2π relation between the frequency in Hertz and radians by studying and observing
MATLAB output plots.