%% Answer to Seminar 6 Exercise1
clear
clc

Fs = 100; % set the sampling rate (Hz)
T = 2; % set the total sampling time (second)
f1 = 20; % the frequency of the first term in the signal (Hz)
f2 = 30; % the frequency of the second term in the signal (Hz)
N = T*Fs; % the total num of sampling points
for i = 1:N
    % create a vector t to trace the time of the sampling points
    t(i)=i/Fs;
    % create a signal in the time domain
    % You can try to change some of the parameters here or discard some terms to create another new signal.
    x(i) = cos(2*pi*f1*i/Fs) + cos(2*pi*f2*i/Fs) + 2*rand(1,1);
    % create a vector f to trace the frequency of the sampling points
    fre_x(i) = i/T;
end

% plot the signal in the time domain
figure(1)
plot(t,x)
xlabel('t (second)')
ylabel('x(t)')
title('Signal in the time domain')
% do DFT on the signal and plot the signal in the frequency domain
figure(2)
% Here, we use abs() because we only pay attention to the amplitude of the transformed signal.
plot(fre_x,abs(fft(x))) 
xlabel('f (Hz)')
ylabel('FFT(x)')
title('Signal in the frequency domain')

