clear; close all
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
vid = VideoWriter('FTs_explained4.mp4','MPEG-4');
vid.FrameRate = 60;    % 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];

periodicity = 1; 
xmax = 9.9975; 
x = 0:0.0025:xmax; % Step size 1/400 
y = zeros(1,size(x,2));
% Create square function with periodicity 1 and 50% (200 points) off, 50% (200 points) on 
for ii = 1:size(x,2)
    if round(x(1,ii)) == floor(x(1,ii)) 
        y(1,ii) = 1; 
    end 
end
y = y-0.5;
jj = 0; 

freqFacMax = 9.1; 
for freqFac = 0.0025:0.0025:freqFacMax % Change Euler circle rotation frequency 
    jj = jj+1; 
    hold off
    subplot('position',pos1); % Plot of g(x) 
    plot(x,y,'b','LineWidth', 2.0)
    hold on
    H=area(x,y);
    set(H(1),'FaceColor','b','FaceAlpha',0.25);

    xlim([0 xmax]);
    ylim([-0.55 0.55]);
    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.7],'HorizontalAlignment','center'); 
    ylabel('Amplitude'); 
    hold off
    
    subplot('position',pos2); % Plot of g(x) on Euler circle 
    theta = 0:pi/200:2*pi*xmax/periodicity; 
    theta2 = freqFac*theta; 
    rho = y;
    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.5 0.55])
    thetaticks(0:45:315);
    thetaticklabels({'1',' ','i',' ','-1',' ','-i',' '});
    rticks(-0.5:0.25:0.5);
    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 freqFacMax]);
    ylim([-1000 2000]);
    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',[freqFacMax/2 -275],'HorizontalAlignment','center'); 
    ylabel('Integral'); 

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