clear all
close all

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

Tc = 0.01;
tend = abs(1/(f2-f1));
df = 0.01;

fsmax = 20;
t = -tend:Tc:tend;

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

noPause = 4;
figure(1),pause
dfs = 0.1;

fs = [6:dfs:18 6:-dfs:3];

for nn = 1:length(fs)
    T = 1/fs(nn);
    N = floor(tend/T);
    tT = (-N:N)*T;
    
    yT = 2 + cos(2*pi*f1*tT) + cos(2*pi*f2*tT);
    yT_FT = [0 kron([1 1 1 1 1 1 1],kron([1 1 4 1 1]/2,[0 1 0])) 0];
    k = -3:3;
    fT = [-fsmax kron(sort([k*fs(nn) k*fs(nn)-f2 k*fs(nn)-f1 k*fs(nn)+f1 k*fs(nn)+f2]),[1 1 1]) fsmax];
    if fs(nn) == 5
        yT_FT = [0 kron([1 1 1 1 1 1 1],kron([1 2 1],[0 1 0])) 0];
        k = -3:3;
        fT = [-fsmax kron(sort([k*fs(nn) k*fs(nn)-f1 k*fs(nn)+f1]),[1 1 1]) fsmax];
    end
    

    figure(1)
    subplot(211)
    h = bar(tT,yT,1,'y');
    hold on
    plot(t,y,'b--','LineWidth',2)
    hold off
    set(gca,'FontSize',16)
    axis([-4 4 0 4])
    xlabel('t [s]','FontSize',16)
    ylabel('y(t)','FontSize',16)
    title(['f_s = ',num2str(1/T),' Hz'],'FontSize',16)
    
    subplot(212)
    fill([-fs(nn)/2 -fs(nn)/2 fs(nn)/2 fs(nn)/2],[0 2.2 2.2 0],0.9*[1 1 1])
    hold on
    plot(fT,yT_FT,'k',f_true,y_FT_true,'r',[-fs(nn)/2 -fs(nn)/2],[0 3],'b:',[fs(nn)/2 fs(nn)/2],[0 3],'b:',[-3*fs(nn)/2 -3*fs(nn)/2],[0 3],'b:',[3*fs(nn)/2 3*fs(nn)/2],[0 3],'b:',[-5*fs(nn)/2 -5*fs(nn)/2],[0 3],'b:',[5*fs(nn)/2 5*fs(nn)/2],[0 3],'b:','LineWidth',2)
    hold off
    if ((nn > 1) & (fs(nn) <= 6))
        axis([-7 7 0 2.2])
    else
        axis([-fsmax fsmax 0 2.2])
    end
    set(gca,'FontSize',16)
    legend('Approx.','Exakt')
    xlabel('f [Hz]','FontSize',16)
    ylabel('Y(f)','FontSize',16)
    
    drawnow
    if nn == 1
        pause
        noPause = noPause - 1;
    elseif ((noPause == 3) & (fs(nn) >= 10))
        pause
        noPause = noPause - 1;
    elseif ((noPause == 2) & (fs(nn) == 18))
        pause
        noPause = noPause - 1;
    elseif ((noPause == 1) & (fs(nn) == 6))
        pause
        noPause = noPause - 1;
    elseif ((noPause == 0) & (fs(nn) == 5))
        pause
        x = 2+2*cos(2*pi*f1*t);
        subplot(211)
        h = bar(tT,yT,1,'y');
        hold on
        plot(t,y,'r--',tT,yT,'ko','LineWidth',2,'MarkerSize',8)
        hold off
        axis([-4 4 0 4])
        xlabel('t [s]')
        ylabel('y(t)')
        title(['f_s = ',num2str(1/T),' Hz'])
        
        pause
        plot(t,y,'r--',tT,yT,'ko','LineWidth',2,'MarkerSize',8)
        axis([-4 4 0 4])
        xlabel('t [s]')
        ylabel('y(t)')
        title(['f_s = ',num2str(1/T),' Hz'])
        
        pause
        plot(t,y,'r--',t,x,'b-',tT,yT,'ko','LineWidth',2,'MarkerSize',8)
        axis([-4 4 0 4])
        xlabel('t [s]')
        ylabel('y(t)')
        title(['f_s = ',num2str(1/T),' Hz'])
        
        pause
        noPause = noPause - 1;
    elseif fs(nn) == 3
        pause
        x = 2+cos(2*pi*3/8*t)+cos(2*pi*5/8*t);
        subplot(211)
        h = bar(tT,yT,1,'y');
        hold on
        plot(t,y,'r--',tT,yT,'ko','LineWidth',2,'MarkerSize',8)
        hold off
        axis([-4 4 0 4])
        xlabel('t [s]')
        ylabel('y(t)')
        title(['f_s = ',num2str(1/T),' Hz'])
        
        pause
        plot(t,y,'r--',tT,yT,'ko','LineWidth',2,'MarkerSize',8)
        axis([-4 4 0 4])
        xlabel('t [s]')
        ylabel('y(t)')
        title(['f_s = ',num2str(1/T),' Hz'])
        
        pause
        plot(t,y,'r--',t,x,'b-',tT,yT,'ko','LineWidth',2,'MarkerSize',8)
        axis([-4 4 0 4])
        xlabel('t [s]')
        ylabel('y(t)')
        title(['f_s = ',num2str(1/T),' Hz'])
        
    end
end

noPause = 4;
pause,figure(2),pause
dfs = 0.05;
df = 0.01;

fs = [2:dfs:5 2:-dfs:0.5];
f = -5:df:5;

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

for nn = 1:length(fs)
    T = 1/fs(nn);
    N = floor(tend/T);
    tT = (-N:N)*T;
    
    yT = exp(-abs(tT));
    
    yT_FT = T*(1-exp(-2*T))./(1-2*exp(-T)*cos(2*pi*f*T)+exp(-2*T));
    
    figure(2)
    subplot(211)
    h = bar(tT,yT,1,'y');
    hold on
    plot(t,y,'b--','LineWidth',2)
    hold off
    set(gca,'FontSize',16)
    axis([-4 4 0 1])
    xlabel('t [s]','FontSize',16)
    ylabel('y(t)','FontSize',16)
    title(['f_s = ',num2str(1/T),' Hz'],'FontSize',16)
    
    subplot(212)
    fill([-fs(nn)/2 -fs(nn)/2 fs(nn)/2 fs(nn)/2],[0 3 3 0],0.9*[1 1 1])
    hold on
    plot(f,yT_FT,'k',f,y_FT_true,'r',[-fs(nn)/2 -fs(nn)/2],[0 3],'b-.',[fs(nn)/2 fs(nn)/2],[0 3],'b-.',[-3*fs(nn)/2 -3*fs(nn)/2],[0 3],'b-.',[3*fs(nn)/2 3*fs(nn)/2],[0 3],'b-.',[-5*fs(nn)/2 -5*fs(nn)/2],[0 3],'b-.',[5*fs(nn)/2 5*fs(nn)/2],[0 3],'b-.','LineWidth',2)
    hold off
    set(gca,'FontSize',16)
    axis([-5 5 0 3])
    legend('Approx.','Exakt')
    xlabel('f [Hz]','FontSize',16)
    ylabel('Y(f)','FontSize',16)
    
    drawnow
    if nn == 1
        pause
        noPause = noPause - 1;
    elseif ((noPause == 3) & (fs(nn) >= 4))
        pause
        noPause = noPause - 1;
    elseif ((noPause == 2) & (fs(nn) == 5))
        pause
        noPause = noPause - 1;
    elseif ((noPause == 1) & (fs(nn) == 2))
        pause
        noPause = noPause - 1;
    elseif ((noPause == 0) & (fs(nn) == 1))
        pause
        noPause = noPause - 1;
    elseif fs(nn) == 0.5
        pause
    end
end
