American University of Sharjah
College of Engineering
Department of Computer Science and Engineering
HW1
Aley Amr
[email protected]
Dr Hasan Mir
Submission Date
October 7th, 2024
Question 1
t = linspace(0, 3, 1000);
NValues = [5, 15, 25, 35];
figure;
for idx = 1:length(NValues)
N = NValues(idx);
x1 = 0.5; %initializing x with the first term in the fourier series
%computing the n term forier series
for k = 1:N
x1 = x1 + (1/(k*pi)) * cos(2*pi*k*t+pi/2);
end
%plotting x1(t)
subplot(2, 2, idx);
plot(t, x1);
xlabel('time t');
ylabel('x1(t)');
grid on;
end
%spectrum plot
figure;
for idx = 1:length(NValues)
N = NValues(idx);
x1 = 0.5;
for k = 1:N
x1 = x1+(1/(k*pi))*cos(2*pi*k*t+pi/2);
end
%fourier transform and spectrum
X1 = fft(x1);
f = linspace(0,1/2,length(X1)/2); %frequency vector
subplot(2, 2, idx);
plot(f, abs(X1(1:length(X1)/2)));
xlabel('frequency Hz');
ylabel('|X1(f)|');
grid on;
end
Figure 1 Time-domain signal
As the value of N increases, the Time Domain signal changes significantly. For lower values
of N, the signal is smooth and resembles smooth approximations of the sawtooth signal, and
has rounded features. As N increases, the signal gets consistently sharper, rougher, and
steeper. High frequency elements are captured, leading to a more detailed and original
waveform. This detailing progressions is consistent with the fourier series expectations, as
more terms are added, the approximation closes in and becomes more accurate, capturing
finer details of the sawtooth waveform.
Figure 2 Frequency Spectrum of x(t) for different values of N
The Frequency spectrum also changes as N increases. For lower values of N, the spectrum
contains less harmonic components. A sparse spectrum and fewer peaks, and higher
frequencies have less effect. As N increases, the spectrum becomes more dense, with more
peaks at higher frequencies, as opposed to lower N values. More fine details of the fourier
series are captured, worth noting that higher frequencies have fast decaying magnitudes. In
the end, more harmonics are added as N increases, with progressively smaller amplitudes
and effects.
Question 2
t = linspace(0, 3, 1000);
%definenig N values
NValues = [5, 15, 25, 35];
figure;
for idx = 1:length(NValues)
N = NValues(idx);
x2 = 1/2; %first term, dc component
%deducing the n term fourier series for the square wave
for k = 1:2:(2*N-1) %odd
x2 = x2+(2/(k*pi))*cos(2*pi*k*t-pi/2);
end
%plotting x2(t)
subplot(2, 2, idx);
plot(t, x2);
xlabel('time sec');
ylabel('amplitude');
grid on;
end
%spectrum plot for x2(t)
figure;
for idx = 1:length(NValues)
N = NValues(idx);
x2 = 1/2;
for k = 1:2:(2*N-1)
x2 = x2+(2/(k*pi))*cos(2*pi*k*t-pi/2);
end
%fourier transform and spectrum
X2 = fft(x2);
f = linspace(0, 1/2, length(X2)/2);
subplot(2, 2, idx);
plot(f, abs(X2(1:length(X2)/2)));
xlabel('frequency Hz');
ylabel('|X2(f)|');
grid on;
end
Figure 3 Time Domain plot of the square wave for different values of N
As N increases, we start with lower values of N. At lower values the signal is a rough
approximation of the square wave, with peaks and smoother curves above and below,
because of fewer harmonics. For higher values of N, more harmonics and terms come into
play, so we are left with more accurate approximations and sharper transitions. So, as N
increases, the overall shape better resembles the square wave with sharper transitions
between high and low states.
Figure 4 Frequency Spectrum of the square wave for different values of N
As for the frequency domain, as N increases, more odd harmonics get included in the signal,
leaving us with a denser spectrum. Newer increased frequencies have smaller amplitudes
and peaks and contributions to the overall shape.