Reproducing those plots is fairly easy. However, note several things:
- In your class notes, the time domain plots are from
t = 0
tot = 4
(nott = 2
like you did). - The frequency domain plots show only the positive frequencies, i.e. half the frequency spectrum. The frequency range is from 0 to 20Hz, meaning that the sampling frequency is 40Hz.
- The amount of energy must remain the same, so if only half the spectrum is displayed, the plotted frequency components should be multiplied by a factor of 2.
That said, here's the complete code (including fixes to your time domain plots):
% # Time domain plots
fs = 40;
t = 0:(1 / fs):4;
y1 = sin(2 * pi * 5 * t)+ sin(2 * pi * 10 * t);
y2 = [sin(2 * pi * 5 * t(t <= 2)), sin(2 * pi * 10 * t(t > 2))];
figure
subplot(2, 1, 1), plot(t, y1)
subplot(2, 1, 2), plot(t, y2)
% # Frequency domain plots
Fy1 = abs(ifft(y1));
Fy2 = abs(ifft(y2));
N = numel(t);
idx = 1:numel(Fy1) / 2; % # Indices of half the spectrum
f = fs * (0:(N - 1)) / N; % # Actual frequencies
figure
subplot(2, 1, 1), plot(f(idx), 2 * Fy1(idx))
subplot(2, 1, 2), plot(f(idx), 2 * Fy2(idx))
The time domain plots are:
and the corresponding frequency domain plots are: