% faltsimul.m Animierte Simulation des Ablaufs einer Faltung
% ACONVVEC und BCONVVEC koennen vordefiniert werden
if exist('ACONVVEC') == 0 
  a = [ 1 2 2 2 3 4 5 4 3 2 2 2 1];
else
  a = ACONVVEC;
end
if exist('BCONVVEC') == 0 
  b = [ 1 2 3 0 0 0 2 5 9 5 2 0 0 1 2 3 4];
else
  b = BCONVVEC;
end
n = length(a) ; m = length(b) ;
af = [a zeros(1,m-1) ]; bf = [b zeros(1,n-1) ];
ntot = m+n-1;
%
clf;
cc = conv(a,b); cmx = max(cc);
axis([0 ntot+1 0 max(cc)]); hold on
xv = 1:ntot;
c = zeros(1,ntot); fmat = zeros(ntot,ntot);
for k=1:m
  d = bf(k)*shrg(af,k-1);
  hdb = plot([0 ntot+1] , [0 0],'k');
  fmat(1:ntot,k) = d';
  if k>1
    delete(hd); delete(hdb); 
    delete(hdr); delete(hdp);
  end
  hd = area(fmat);
  hdb = plot([0 ntot+1] , [cmx/4 cmx/4],'k');
  hdr = plot(d+cmx/4,'r','LineWidth',1.8);
  hdp = plot(k-1,cmx/4,'or','LineWidth',3);
  pause(0.5)
end


