function convanim(X,H,makemovie)
% convanim(X,H,makemovie)
%    Animate the convolution between two sequences 
%    by calculating it point-by-point, showing the overlaps.
%    X is the longer sequence.
% 2010-11-23 Dan Ellis dpwe@ee.columbia.edu

if nargin < 3
  makemovie = 0;
end

% Initialize the movie
if makemovie
  moviename = 'convanim.mov';
  disp(['writing movie ', moviename]);
  eval(['MakeQTMovie start ', moviename]);
  eval(['MakeQTMovie framerate 30']);
end

subplot(211)
plot(X)

lx = length(X);
lh = length(H);
ly = lx + lh - 1;

Y = NaN*ones(1,ly);

y0 = conv(X,H);

% Zero-pad X so that we always have valid points to calculate
% against
padX = [zeros(1,lh-1),X,zeros(1,lh-1)];

for i = 1:ly
  
  subplot(211)
  plot(1:lx,X,(i-lh)+[1:lh],fliplr(H),'r');
  ax = axis();
  axis([1-lh,ly,ax(3),ax(4)]);
  Y(i) = sum(fliplr(H).*padX((i-1)+[1:lh]));
  subplot(212)
  plot(1:ly,Y);
  axis([1-lh,ly,min(y0),max(y0)]);

  % Save this image
  if makemovie
    MakeQTMovie addfigure
  else
    drawnow
  end

end

if makemovie
  MakeQTMovie finish
end

% then compress down e.g.
% ffmpeg -i convanim.mov -vcodec mpeg4 -b 100kb -mbd 2 -flags +mv4 -trellis 2 -cmp 2 -subcmp 2 convanim.mp4