%  Udfoer foldning af to signal
%
%  Version 1.1, 14/2-1996, JAJ
%  Version 1.2, 4/9-2017, JAJ
%      Modified for support in Matlab 2017

function [signal1,signal2] = foldning (sig_data1,sig_data2,hp1,hp2,hastighed)

%  Find signallaengder

N1=max(size(sig_data1));
N2=max(size(sig_data2));
N=max([N1 N2]);

%  Set up the different axis

axes(hp1)
cla
plot(sig_data1)
title('Indgangssignal')
axis([0 N-1 1.1*min([sig_data1 sig_data2]) 1.1*max([sig_data1 sig_data2])])
hold on
signal1 = line('color','r','linestyle','--','xdata',[],'ydata',[]);

max_value=max(conv(sig_data1,sig_data2))*1.1;
min_value=min(conv(sig_data1,sig_data2))*1.1;

axes(hp2)
cla
plot(conv(sig_data1,sig_data2),'w') % sort = k
title('Resultat')
axis([0 N-1 min_value max_value])
hold on
%signal2 = line('color','b','linestyle','-','erase','none','xdata',[],'ydata',[]);
signal2 = animatedline('color','b','linestyle','-');
set(hp1,'Xticklabel','','Yticklabel','')
set(hp2,'Xticklabel','','Yticklabel','')

%  Plot the data one sample at a time

prev_val=0;
for i=1:N1+N2-1

  if (i<N2)
    N1val=(1:i);
    N2val=(i:-1:1);
  elseif (i<N1)
    N1val=(1:N2)+i-N2;
    N2val=(N2:-1:1);
  else
    N1val=(1+i-N2:N1);
    N2val=(N2:-1:i-N1+1);
  end

  % Update the plot

  set(signal1,'xdata',N1val,'ydata',sig_data2(N2val))
  current_val=sig_data2(N2val)*sig_data1(N1val)' ;
  if (i<=N1)
    addpoints(signal2,[i-1 i],[prev_val current_val])
  end
  prev_val=current_val;
  drawnow;

  %  Make a slight delay

  if (hastighed==0)
    pause
  else
    for i=1:hastighed
      a=sin(23.0);
      end
    end
  end

