clear all
close all

f1 = 19/8;
f2 = 21/8;

T = 0.01;
tend = abs(1/(f2-f1));
fs = 1/T;
df = 0.01;
fend = 5;
f = -fend:df:fend;

t = -tend:T:tend;

noPause = 3;
figure(2),pause
dP = 0.05;

y = exp(-abs(t));
y_FT_true = 2./(1+(2*pi*f).^2);

for P = 1:dP:tend*2
    tP = -P/2:T:P/2;
    TP = [-P/2 tP P/2];
    
    yP = exp(-abs(tP));
    YP = [0 yP 0];
    
    yP_FT = 2./(1+(2*pi*f).^2) - 2*exp(-P/2)./(1+(2*pi*f).^2).*(cos(pi*P*f) - 2*pi*f.*sin(pi*P*f));
    
    figure(2)
    subplot(211)
    plot(t,y,'k','LineWidth',2)
    hold on
    fill(TP,YP,'y','LineWidth',2)
    set(gca,'FontSize',16)
    hold off
    xlabel('t [s]','FontSize',16)
    ylabel('y(t)','FontSize',16)
    title(['P = ',num2str(P),' s'],'FontSize',16)
    
    subplot(212)
    plot(f,y_FT_true,'r',f,yP_FT,'k','LineWidth',2)
    set(gca,'FontSize',16)
    legend('Exakt','Approx.')
    xlabel('f [Hz]','FontSize',16)
    ylabel('Y(f)','FontSize',16)
    title(['Felet = ',num2str(exp(-P)*100),' %'],'FontSize',16)
    
    drawnow
    if (((noPause == 1) & (P >= log(1000))) | ((noPause == 2) & (P >= log(100))) | ((noPause == 3) & (P >= 1)))
        pause
        noPause = noPause - 1;
    end
end

noPause = 3;
pause,figure(3),pause
dP = 0.05;

y = exp(-2*abs(t-2)) + exp(-2*abs(t+2));
y_FT_true = 8./(4+(2*pi*f).^2).*cos(4*pi*f);

for P = 1:dP:tend*2
    tP = -P/2:T:P/2;
    TP = [-P/2 tP P/2];
    
    yP = exp(-2*abs(tP-2)) + exp(-2*abs(tP+2));
    YP = [0 yP 0];
    
    if P <= 4
        yP_FT = 8*exp(-4)*(sinh(P)*cos(pi*P*f) + cosh(P)*pi*f.*sin(pi*P*f))./(4+(2*pi*f).^2);
    else
        yP_FT = 8*(cos(4*pi*f) - exp(-P)*cosh(4)*(cos(pi*P*f) - pi*f.*sin(pi*P*f)))./(4+(2*pi*f).^2);
    end
    
    figure(3)
    subplot(211)
    plot(t,y,'k','LineWidth',2)
    hold on
    fill(TP,YP,'y','LineWidth',2)
    set(gca,'FontSize',16)
    hold off
    xlabel('t [s]','FontSize',16)
    ylabel('y(t)','FontSize',16)
    title(['P = ',num2str(P),' s'])
    
    subplot(212)
    plot(f,y_FT_true,'r',f,yP_FT,'k','LineWidth',2)
    set(gca,'FontSize',16)
    legend('Exakt','Approx.')
    xlabel('f [Hz]','FontSize',16)
    ylabel('Y(f)','FontSize',16)
    
    drawnow
    if (((noPause == 1) & (P >= tend*4/3)) | ((noPause == 2) & (P >= tend)) | ((noPause == 3) & (P >= 2)))
        pause
        noPause = noPause - 1;
    end
end

%%%%%%%%%%%%%%
% Illustrera 2 cosinuser = 1 cosinus
%%%%%%%%%%%%%%

P = 1;
t = -tend:T/4:tend;
tP = -P/2:T/4:P/2;
x = cos(2*pi*(10-1/16)*t)+cos(2*pi*(10+1/16)*t);
xP = cos(2*pi*(10-1/16)*tP)+cos(2*pi*(10+1/16)*tP);
pause,figure(4),pause
subplot(211)
plot(tP,xP,'b-','LineWidth',2)
set(gca,'FontSize',16)
xlabel('t [s]','FontSize',16)
ylabel('y(t)','FontSize',16)
title(['P = ',num2str(P),' s'],'FontSize',16)
    
pause
subplot(212)
plot(t,x,'b-','LineWidth',2)
set(gca,'FontSize',16)
xlabel('t [s]','FontSize',16)
ylabel('y(t)','FontSize',16)
title('P = 8 s','FontSize',16)

%%%%%%%%%%%%%%%
% Cosinusfallet
%%%%%%%%%%%%%%%

noPause = 3;
pause,figure(1),pause
dP = 0.05;

y = 2 + cos(2*pi*f1*t) + cos(2*pi*f2*t);
y_FT_true = [0 0 1/2/df 0 0 1/2/df 0 0 2/df 0 0 1/2/df 0 0 1/2/df 0 0]*df;
f_true = [-fend -f2 -f2 -f2 -f1 -f1 -f1 0 0 0 f1 f1 f1 f2 f2 f2 fend];

for P = 1:dP:tend*2
    tP = -P/2:T:P/2;
    TP = [-P/2 tP P/2];
    
    yP = 2 + cos(2*pi*f1*tP) + cos(2*pi*f2*tP);
    YP = [0 yP 0];
    
    yP_FT = P*2*sinc(P*f) + P/2*(sinc(P*(f-f1)) + sinc(P*(f+f1))) + P/2*(sinc(P*(f-f2)) + sinc(P*(f+f2)));
    
    figure(1)
    subplot(211)
    plot(t,y,'k','LineWidth',2)
    hold on
    fill(TP,YP,'y','LineWidth',2)
    set(gca,'FontSize',16)
    hold off
    xlabel('t [s]','FontSize',16)
    ylabel('y(t)','FontSize',16)
    title(['P = ',num2str(P),' s'],'FontSize',16)
    
    subplot(212)
    plot(f_true,y_FT_true*P,'r',f,yP_FT,'k','LineWidth',2)
    set(gca,'FontSize',16)
    legend('Exakt','Approx.')
    xlabel('f [Hz]','FontSize',16)
    ylabel('Y(f)','FontSize',16)
    title(['\Delta f = ',num2str(1/P),' Hz'],'FontSize',16)
    
    drawnow
    if P == 1
        pause
    end
    if (((noPause == 1) & (P >= tend*4/3)) | ((noPause == 2) & (P >= tend)) | ((noPause == 3) & (P >= 2)))
        pause
        noPause = noPause - 1;
    end
end
pause

N = 40;
fn = (-N:N)/8;
yP_FT = P*2*sinc(P*fn) + P/2*(sinc(P*(fn-f1)) + sinc(P*(fn+f1))) + P/2*(sinc(P*(fn-f2)) + sinc(P*(fn+f2)));
subplot(212)
hold on
plot(fn,yP_FT,'bo','MarkerSize',8,'LineWidth',2)
hold off
