
clear;
close all;

namebase = ['../notes/' mfilename()];

% SAMPLING INFORMATION
dt = 0.05;          % Sampling period
dd  = 4;            % 
di  = 12;           %
t = (-10:dt:10).';  % Time axis
N = length(t);      % Time length

% DEFINE THE FUNCTIONS TO CONVOLVE
k = 1;
x1c{k} = 1/dt.*(t == 0);
x2c{k} = exp(-t).*(t >= 0);
k = k + 1;

x1c{k} = (t >= 0);
x2c{k} = exp(-t).*(t >= 0);
k = k + 1;

x1c{k} = (t >= 0);
x2c{k} = (t >= 0) - (t >= 3);
k = k + 1;

x1c{k} = (t >= 0) - (t >= 3);
x2c{k} = (t >= 0) - (t >= 3);
k = k + 1;

x1c{k} = (t >= 0) - (t >= 3);
x2c{k} = exp(-t).*(t >= 0);
k = k + 1;

x1c{k} = exp(-1.*t).*(t >= 0);
x2c{k} = exp(-1.*t).*(t >= 0);
k = k + 1;

x1c{k} = t.*(t >= 0) - t.*(t >= 3);
x2c{k} = (t >= 0) - (t >= 3);
k = k + 1;

x1c{k} = randn(N,1);
x2c{k} = (t >= 0) - (t >= 3);
k = k + 1;

x1c{k} = (t >= 0);
x2c{k} = exp(-1.*t).*(t >= 0);
k = k + 1;

x1c{k} = (t >= 0);
x2c{k} = (t >= 0) - (t >= 2);
k = k + 1;


for mm = 1:length(x1c)

    close all;

    % RE-CAST SIGNALS AS DOUBLES
    x1 = double(x1c{mm});
    x2 = double(x2c{mm});

    % CONVOLVE THE SIGNALS TOGETHER
    y = conv(x1, x2, 'same')*dt;    % Conovlution

    % INTIALIZE OUTPUT
    x3 = nan*ones(N,1);

    % CREATE FIGURE
    figure(3);
    set(gcf, 'Units', 'inches', 'Position', [1 1 6 6], 'color', [1 1 1])
    pos = get(gca,'position');       % Read position [x y z]
    set(gca,'position',[.05 pos(2:4)-[0 -.05 0.1]]) % Move label to right
    
    % ILLUSTRATE CONVOLUTION
    for n = (dd + di*dd):dd:N

       % SHIFT DATA
       x2s = circshift(flipud(x2), round(t(n)/dt));

       subplot(421)
       plot(t, x1, 'linewidth', 1.4, 'color', [228,26 ,28]/255)  
       axis([3*t(1)/4 3*t(end)/4 min(y)-1 max(max(y)+1,1)])
       oa = oaxes('origin', [0 0 0], 'xlabel', {'','t'}, 'ylabel', {'','x(t)'}, 'YLabelHorizontalAlignment', 'left');
       set(oa, 'linewidth', 0.5)

       subplot(423)
       plot(t, x2, 'linewidth', 1.4, 'color', [55 ,126,184]/255)  
       axis([3*t(1)/4 3*t(end)/4 min(y)-1 max(max(y)+1,1)])
       oa = oaxes('origin', [0 0 0], 'xlabel', {'','t'}, 'ylabel', {'','h(t)'}, 'YLabelHorizontalAlignment', 'left');
       set(oa, 'linewidth', 0.5)

       subplot(422)
       plot(t, x1, 'linewidth', 1.4, 'color', [228,26 ,28]/255)
       axis([3*t(1)/4 3*t(end)/4 min(y)-1 max(max(y)+1,1)])
       oa = oaxes('origin', [0 0 0], 'xlabel', {'','\tau'}, 'ylabel', {'','x(\tau)'}, 'YLabelHorizontalAlignment', 'left');
       set(oa, 'linewidth', 0.5)

       subplot(424)
       plot(t, x2s, 'linewidth', 1.4, 'color', [55 ,126,184]/255)
       text(t(n), 0, '\bf|', 'HorizontalAlignment', 'Center');   
       axis([3*t(1)/4 3*t(end)/4 min(y)-1 max(max(y)+1,1)])
       oa = oaxes('origin', [0 0 0], 'xlabel', {'','\tau'}, 'ylabel', {'',['h(t-\tau), t = ' num2str(t(n), '%.2f')]}, 'YLabelHorizontalAlignment', 'left');
       set(oa, 'linewidth', 0.5)

       subplot(426)
       plot(t.',(x1.*x2s).', 'color', [152,78 ,163]/255, 'linewidth', 1.4)
       axis([3*t(1)/4 3*t(end)/4 min(y)-1 max(max(y)+1,1)])
       oa = oaxes('origin', [0 0 0], 'xlabel', {'','\tau'}, 'ylabel', {'',['x(\tau) h(t-\tau), t = ' num2str(t(n), '%.2f')]}, 'YLabelHorizontalAlignment', 'left');
       set(oa, 'linewidth', 0.5)

       subplot(428)
       plot(t, [y(1:n); nan*ones(N-n,1)],  'g-', 'linewidth', 2, 'color', [77 ,175,74]/255)
       text(t(n), 0, '\bf|', 'HorizontalAlignment', 'Center');  
       axis([3*t(1)/4 3*t(end)/4 min(y)-1 max(max(y)+1,1)])
       oa = oaxes('origin', [0 0 0], 'xlabel', {'','t'}, 'ylabel', {'','y(t)'}, 'YLabelHorizontalAlignment', 'left');
       set(oa, 'linewidth', 0.5)

       
       %if exist('export_fig', 'file'), 
       %    if n == dd + di*dd, [im, map] = rgb2ind(export_fig, 65536); im(1,1,1,(N+dd-1-di*dd)/dd-1) = 0; end
       %    im(:,:,:,(n - di*dd)/dd) = rgb2ind(export_fig, map);
       %else 
       if exist('export_fig', 'file'), 
           h = myaa('tmp.png');
           f = getframe(h);
           close(h)
       else
           f = getframe(figure(3));
       end
       if n == dd + di*dd, [im,map] = rgb2ind(f.cdata,256,'nodither'); im(1,1,1,(N+dd-1-di*dd)/dd-1) = 0; end
       im(:,:,1,(n - di*dd)/dd) = rgb2ind(f.cdata,map,'nodither');
       %end

    end

    imwrite(im,map,[namebase '_' num2str(mm, '%03u') '.gif'],'DelayTime',.1,'LoopCount',inf); %g443800
end
