clear; close all
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
vid = VideoWriter('squareWave001.mp4','MPEG-4');
vid.FrameRate = 30;    % Default 30
vid.Quality = 100;    % Default 75
open(vid);
set(0,'defaultfigurecolor',[1 1 1]);
set(gcf,'Color','k');

rgb = customcolormap([0 0.17 0.33 0.5 0.67 0.83 1],...
    {'#ff0000', '#ffa500', '#ffff00', '#00ff00',...
    '#00ffff', '#0000ff', '#7f00ff'},500);
rgb = flip(rgb);

pos1 = [0 0 1 1];
n = 5;
c = 0.25;
circOffset = 2; % Vertical axis position of circles on left

x = 0:0.00125:10;
thc = 0:pi/180:2*pi;

subplot('position',pos1);
for t = 0.0:1/200:4.995
    hold off
    offset = 0;
    sum = 0.0;
    for jj = 1:2:2*n+1 % n+1 components; odd values only: 1, 3, 5, 7, ...
        % x- and y-coordinates of circles on left
        xc = (1/jj)*cos(thc) - circOffset;
        yc = (1/jj)*sin(thc);
        % y-coordinates of sinusoidal components
        y = (1/jj)*sin(2*jj*pi*(x/4 - t));
        % Sum these up to get approximation to square wave
        sum = sum + y;
        % Shift each sine wave for presentational purposes
        y2 = y - offset;
        % Assign colour to each component
        colindex = 1 + floor(jj*499/(2*n+1));
        rgbcol = rgb(colindex,:);
        % Plot sine wave
        plot(x, y2, 'color', rgbcol, 'LineWidth', 2.0);
        hold on
        % Plot circle
        plot(xc, yc - offset, 'color', rgbcol, 'LineWidth', 2.0);
        % Plot line from centre of circle to its perimeter at the relevant
        % position for the present value of t
        xarrow = [0 (1/jj)*cos(2*pi*jj*t)] - circOffset;
        yarrow = [0 y(1)] - offset;
        plot(xarrow, yarrow, 'color', rgbcol, 'LineWidth', 2.0);
        % Plot connecting line from circle to start of sine wave
        xspan = [xarrow(2) 0];
        yspan = [yarrow(2) yarrow(2)];
        plot(xspan, yspan, 'color', [0.7 0.7 0.7], 'LineWidth', 2.0);
        % Increase the offset value for the next component
        offsetj = (1/jj) + c + (1/(jj+2));
        offset = offset + offsetj;
    end
    % Plot square-wave approximation
    plot(x, sum - offset - 1.6, 'r','LineWidth', 2.0);
    % Plot circles on top of one another
    jjOffsetx = -circOffset;
    jjOffsety = -offset - 1.6;

    % Plot all circles connected
    for jj = 1:2:2*n+1
        colindex = 1 + floor(jj*499/(2*n+1));
        rgbcol = rgb(colindex,:);
        xc = (1/jj)*cos(thc) + jjOffsetx;
        yc = (1/jj)*sin(thc) + jjOffsety;
        plot(xc, yc, 'color', rgbcol, 'LineWidth', 2.0);
        y = (1/jj)*sin(2*jj*pi*(x/4 - t));
        xarrow = [0 (1/jj)*cos(2*pi*jj*t)] + jjOffsetx;
        yarrow = [0 y(1)] + jjOffsety;
        plot(xarrow, yarrow, 'color', rgbcol, 'LineWidth', 2.0);
        jjOffsetx = xarrow(2);
        jjOffsety = yarrow(2);
    end
    % Plot connecting line from circle to start of sine wave
    xspan = [xarrow(2) 0];
    yspan = [yarrow(2) yarrow(2)];
    plot(xspan, yspan, 'color', [0.7 0.7 0.7], 'LineWidth', 2.0);
    
    axis off
    axis equal
    xlim([-4 10])
    ylim([-7.4 1.1])
    frame = getframe(gcf);
    writeVideo(vid,frame);
    hold off
end
% Output the movie as an mp4 file and close it
close(vid);