clear all
close all

fontsize=14;
linewidth=1;
markersize=10;
speed=1;

fin = [50 100 500];  %signal frequencies
ftest = [50 100 200];  %comparison frequencies
A = [1 2 1];   %signal amplitudes
N = 10000;
t = linspace(0,1,N)';

g = zeros(length(t),1);
for ii=length(fin):-1:1
    g = g + A(ii)*cos(2*pi*fin(ii)*t);
    plot(t,A(ii)*cos(2*pi*fin(ii)*t),'LineWidth',linewidth)
    hold all
end
xlabel('$t$ [sec]','FontSize',fontsize,'Interpreter','latex')
ylabel('$g(t)$','FontSize',fontsize,'Interpreter','latex')
set(gca,'FontSize',fontsize,'LineWidth',linewidth)
legend({'50 Hz','100 Hz','200 Hz'},'FontSize',fontsize,'Interpreter','latex')

figure
plot(t,g,'LineWidth',2*linewidth)
xlabel('$t$ [sec]','FontSize',fontsize,'Interpreter','latex')
ylabel('$g(t)$','FontSize',fontsize,'Interpreter','latex')
set(gca,'FontSize',fontsize,'LineWidth',linewidth)
legend({'total'},'FontSize',fontsize,'Interpreter','latex')

[fout,gtilde] = fourier(t,g);
figure
semilogx(fout,abs(gtilde))
xlim([1 1000])
xlabel('$f$ [Hz]','FontSize',fontsize,'Interpreter','latex')
ylabel('$\tilde{g}(f)$','FontSize',fontsize,'Interpreter','latex')
set(gca,'FontSize',fontsize,'LineWidth',linewidth)

test = zeros(length(t),1);
figure
for ii=1:length(ftest)
    test = cumsum(g.*cos(2*pi*ftest(ii)*t));
    plot(t,test,'LineWidth',2*linewidth)
    hold all
end
xlabel('$t$ [sec]','FontSize',fontsize,'Interpreter','latex')
ylabel('$\int_0^t g \times \cos(2\pi f t)$','FontSize',fontsize,'Interpreter','latex')
set(gca,'FontSize',fontsize,'LineWidth',linewidth)
legend({'50 Hz','100 Hz','200 Hz'},'FontSize',fontsize,'Interpreter','latex')

figure
plot(t,g,'LineWidth',2*linewidth)
hold all
for ii=1:length(ftest)
    plot(t,4*cos(2*pi*ftest(ii)*t),'LineWidth',2*linewidth)
end
xlim([0 0.01])
xlabel('$t$ [sec]','FontSize',fontsize,'Interpreter','latex')
ylabel('$\cos(2\pi f t)$','FontSize',fontsize,'Interpreter','latex')
set(gca,'FontSize',fontsize,'LineWidth',linewidth)
legend({'signal','50 Hz','100 Hz','200 Hz'},'FontSize',fontsize,'Interpreter','latex')