clear; close all
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
vid = VideoWriter('FTs_explained2.mp4','MPEG-4');
vid.FrameRate = 30;    % Default 30
vid.Quality = 100;    % Default 75
open(vid);
set(0,'defaultfigurecolor',[1 1 1]);
myBlue = [0.5 0.55 0.88];
myBlue2 = [0.61 0.67 0.92];
pos1 = [0.1 0.7 0.8 0.28];
pos2 = [0.1 0.1 0.4 0.5];
pos3 = [0.55 0.1 0.4 0.5];

xmax = 4.5;
periodicity = 1/3;
x = 0:0.0025:xmax;
y = cos(2*pi*x/periodicity)+1;
jj = 0;

for freqFac = 0.005:0.00125:1.25 % Change Euler circle rotation frequency 
    jj = jj+1;
    linePos = periodicity/freqFac;
    hold off
    subplot('position',pos1); % Plot of g(x) 
    plot(x,y,'b','LineWidth', 2.0)
    hold on
    ll = round(xmax/linePos);
    for ii = 1:ll % Draw vertical lines separated by Euler-circle periodicity 
        plot([ii*linePos ii*linePos],[0 2],'r','LineWidth', 2.0,'LineStyle','--');
    end
    xlim([0 xmax]);
    ylim([-0.1 2.1]);
    set(gca,'fontsize',18);
    set(gca,'linewidth',2);
    set(gca,'box','off')
    ax = gca;
    ax.XAxisLocation = 'origin';
    ax.YAxisLocation = 'origin';
    ax.YTickLabel = ' ';
    xlabel('Time [s]','Position',[2.25 -0.4],'HorizontalAlignment','center');
    ylabel('Amplitude');
    hold off

    subplot('position',pos2); % Plot of g(x) on Euler circle 
    theta = 0:pi/180:2*pi*4.5/periodicity;
    theta2 = freqFac*theta;
    rho = cos(theta)+1;
    realIntegral(jj) = sum(cos(theta2).*rho);
    imagIntegral(jj) = sum(sin(theta2).*rho);
    absIntegral(jj) = ((sum(cos(theta2).*rho)).^2 + (sum(sin(theta2).*rho)).^2).^0.5;
    freqRatio(jj) = freqFac;
    polarplot(-theta2,rho,'b','LineWidth', 2.0)
    hold on
    rlim([0 2])
    thetaticks(0:45:315);
    thetaticklabels({'1',' ','i',' ','-1',' ','-i',' '});
    rticks(0:1:2);
    rticklabels({' ',' ',' '});

    set(gca,'fontsize',18);
    set(gca,'linewidth',2);
    set(gca,'box','off')
    hold off

    subplot('position',pos3); % Plot of integrals as a function of frequency ratios 
    plot(freqRatio,realIntegral,'b','LineWidth', 2.0)
    hold on
    plot(freqRatio,imagIntegral,'r','LineWidth', 2.0)
    plot(freqRatio,absIntegral,'g','LineWidth', 2.0)
    xlim([0 1.28]);
    ylim([-1250 5000]);
    set(gca,'fontsize',18);
    set(gca,'linewidth',2);
    set(gca,'box','off')
    ax = gca;
    ax.XAxisLocation = 'origin';
    ax.YAxisLocation = 'origin';
    ax.YTickLabel = ' ';
    xlabel('Frequency ratio','Position',[0.625 -625],'HorizontalAlignment','center');
    ylabel('Integral');

    frame = getframe(gcf);
    writeVideo(vid,frame);
end
% Output the movie as an mp4 file and close it
close(vid);