Matlab Assignment 1
Matlab Assignment 1
College of Engineering
HW1
Aley Amr
Dr Hasan Mir
Submission Date
figure;
%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
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.
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
%plotting x2(t)
subplot(2, 2, idx);
plot(t, x2);
xlabel('time sec');
ylabel('amplitude');
grid on;
end
for k = 1:2:(2*N-1)
x2 = x2+(2/(k*pi))*cos(2*pi*k*t-pi/2);
end
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.
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.