Skip to content
Snippets Groups Projects
crp2.m 55.5 KiB
Newer Older
      [X matext] = crp_plugin(x, y, m0, t, e, mflag, hCRP, plugin_path, 0);
marwan's avatar
marwan committed

marwan's avatar
marwan committed
  else
  % use builtin implementation
      if ~nogui
          set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Import Embedding Vectors'),drawnow
      end
      ex=ceil(find(~(ds+1))/m);
      x(:,ex)=[]; y(:,ex)=[]; 
      m=m-length(ex);
      if m==0, 
        set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','No embedding vectors!'),drawnow
        return
      end

      if m0>1
        x2=x(1:end-t*(m0-1),:);
        y2=y(1:end-t*(m0-1),:);
        for i=1:m0-1,
          if check_stop(hCRP,hCtrl,nogui,obj), break, end
          x2(:,m*i+1:m*(i+1))=x(1+t*i:end-t*(m0-i-1),:);
          y2(:,m*i+1:m*(i+1))=y(1+t*i:end-t*(m0-i-1),:);
        end
        x=x2; y=y2; Nx=size(x,1); Ny=size(y,1);
        m=m0*m; clear x2 y2
      end

      x1=repmat(x,1,Ny);
marwan's avatar
marwan committed
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
      for mi=1:m, 
          x2(:,mi)=reshape(rot90(x1(:,0+mi:m:Ny*m+mi-m)),Nx*Ny,1); 
      end
marwan's avatar
marwan committed
      y1=repmat(y,Nx,1); x1=x2; clear x2
marwan's avatar
marwan committed
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
marwan's avatar
marwan committed

      if ~nonorm, unit = ''; else unit = '\sigma'; end

marwan's avatar
marwan committed
      switch(mflag)


      %%%%%%%%%%%%%%%%% local CRP, fixed distance maximum norm

      case {1,2,3,5}
marwan's avatar
marwan committed

      errcode=111;
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      s=zeros(m,Nx*Ny);
marwan's avatar
marwan committed
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
marwan's avatar
marwan committed
      s1=zeros(Nx*Ny,m);
marwan's avatar
marwan committed
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
marwan's avatar
marwan committed
      X=uint8(zeros(Ny,Nx));
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      s1=x1-y1;
      switch mflag
        case 1
      %%%%%%%%%%%%%%%%% maximum norm
        if m>1
          if check_stop(hCRP,hCtrl,nogui,obj), return, end
          s=max(abs(s1'));
        else
          s=abs(s1);
        end
        matext=[num2str(round(100*e)/100) unit ' (fixed distance maximum norm)'];
        case 5
      %%%%%%%%%%%%%%%%% maximum norm, fixed RR
          errcode=115;
          if m>1
            if check_stop(hCRP,hCtrl,nogui,obj), return, end
            s=max(abs(s1'));
          else
            s=abs(s1);
          end
          ss = sort(s(:));
          idx = ceil(e * length(ss));
          e = ss(idx);
          matext=[num2str(round(100*e)/100) '\sigma (fixed distance maximum norm, fixed RR)'];
marwan's avatar
marwan committed
        case 2
      %%%%%%%%%%%%%%%%% euclidean norm
        errcode=112;
        if m>1
          if check_stop(hCRP,hCtrl,nogui,obj), return, end
          set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Transpose  Matrix'),drawnow
          s=s1.^2';
          if check_stop(hCRP,hCtrl,nogui,obj), return, end
          set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Sum Matrix'),drawnow
          s1=sum(s);
          if check_stop(hCRP,hCtrl,nogui,obj), return, end
          set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Squareroot Matrix'),drawnow
          s=sqrt(s1);
        else
          s=abs(s1);
        end
        matext=[num2str(round(100*e)/100) unit ' (fixed distance euclidean norm)'];
marwan's avatar
marwan committed
        case 3
      %%%%%%%%%%%%%%%%% minimum norm
        errcode=113;
        if m>1
          if check_stop(hCRP,hCtrl,nogui,obj), return, end
          s = sum(abs(s1),2);
        else
          s=abs(s1);
        end
        matext=[num2str(round(100*e)/100) unit ' (fixed distance minimum norm)'];
marwan's avatar
marwan committed
      end
marwan's avatar
marwan committed
      if check_stop(hCRP,hCtrl,nogui,obj), return, end

marwan's avatar
marwan committed
      %%%%%%%%%%%%%%%%% now apply threshold
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')'),'String','Building CRP Matrix'),drawnow
      X2 = s<e;
      X=reshape(uint8(X2),Ny,Nx); 
      clear s s1 x1 y1
marwan's avatar
marwan committed
      %%%%%%%%%%%%%%%%% local CRP, normalized distance euclidean norm
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      case 4
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      errcode=114;
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Normalize Embedding Vectors'),drawnow
      Dx=sqrt(sum(((x1(:,:)).^2)'))';
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
      Dy=sqrt(sum(((y1(:,:)).^2)'))';
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
      x1=x1./repmat(Dx,1,m);
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
      y1=y1./repmat(Dy,1,m); clear Dx Dy 
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Reshape Embedding Vectors'),drawnow
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      if m>1
         s=sqrt(sum(((x1(:,:)-y1(:,:)).^2)'));
      else
         s=abs(x1(:,1)-y1(:,1));
      end
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Building CRP Matrix'),drawnow
      X=uint8(reshape((s/max(s))<(e/max(s)),Ny,Nx)); clear s x1 y1 
      matext=[num2str(round(100*e)/100) unit ' (normalized distance euclidean norm)'];
marwan's avatar
marwan committed


marwan's avatar
marwan committed
      %%%%%%%%%%%%%%%%% local CRP, fixed neigbours amount
marwan's avatar
marwan committed

marwan's avatar
marwan committed

      errcode=116;
marwan's avatar
marwan committed
      if e>=1 
        e=round(e)/100;
        txt=['The value for fixed neigbours amount has to be smaller '...
             'than one. Continue the computation with a value of ' ...
	     num2str(e)];
        if nogui==0
          warndlg(txt,'Threshold value mismatch');
          drawnow
          waitforbuttonpress
          set(findobj('Tag','Size','Parent',gcf),'String',num2str(e))
        end
      end
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Reshape Embedding Vectors'),drawnow
marwan's avatar
marwan committed
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      if m>1
         s=sqrt(sum(((x1(:,:)-y1(:,:)).^2)'));
      else
         s=abs(x1(:,1)-y1(:,1));
      end
      if check_stop(hCRP,hCtrl,nogui,obj), return, end

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Sort Distance Matrix'),drawnow
      mine=round(Ny*e);
      [SS, JJ]=sort(reshape(s,Ny,Nx)); JJ=JJ';
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
marwan's avatar
marwan committed
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Building CRP Matrix'),drawnow
      X1(Nx*Ny)=uint8(0); X1(JJ(:,1:mine)+repmat([0:Ny:Nx*Ny-1]',1,mine))=uint8(1);
      if check_stop(hCRP,hCtrl,nogui,obj), return, end
      X=reshape(X1,Ny,Nx); clear X1 SS JJ s
marwan's avatar
marwan committed
      matext=[num2str(round(1000*mine/Ny)/10) '% (fixed neighbours amount)'];
marwan's avatar
marwan committed

marwan's avatar
marwan committed
      %%%%%%%%%%%%%%%%% local CRP, interdependent neigbours 

marwan's avatar
marwan committed

      errcode=117;
marwan's avatar
marwan committed
      warning('Method not available!')
      matext='';


      %%%%%%%%%%%%%%%%% order matrix

marwan's avatar
marwan committed

      errcode=118;
marwan's avatar
marwan committed
      warning('Method not available!')
      matext='';


      %%%%%%%%%%%%%%%%% order pattern recurrence plot

marwan's avatar
marwan committed

      errcode=119;
marwan's avatar
marwan committed

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Order Patterns'),drawnow

      % create a order pattern test
      cmdStr = '';
      for i=2:m;
          cmdStr = [cmdStr, ' permX(:,', num2str(i-1) ,') < permX(:,', num2str(i), ') + eps'];
          if i < m, cmdStr = [cmdStr, ' &']; end
      end
      if m==1
          cmdStr = '1'; 
          disp('Warning: No order patterns for dimension one; please use higher dimension!')
      end

      % order patterns by permutation of the set of values
      clear patt*
      for i=1:size(x,1)
          permX=perms(x(i,:));
          orderPattern = find(eval(cmdStr));
          if isempty(orderPattern) orderPattern = 0; end
          pattX(i) = orderPattern(1);
      end
      for i=1:size(y,1)
          permX=perms(y(i,:));
          orderPattern = find(eval(cmdStr));
          if isempty(orderPattern) orderPattern = 0; end
          pattY(i) = orderPattern(1);
      end

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Create Order Patterns Matrix'),drawnow

      px = permute(pattX', [ 1 3 2 ]);
      py = permute(pattY', [ 3 1 2 ]);
      X = uint8(px(:,ones(1,length(y)),:) == py(ones(1,length(x)),:,:));
marwan's avatar
marwan committed
      X = X';
      if check_stop(hCRP,hCtrl,nogui,obj), return, end

      matext='';

      %%%%%%%%%%%%%%%%% Levenshtein

      case 10

      errcode=120;

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      s1 = levenshtein(x1, y1);
      X = reshape(s1,Ny,Nx);
      matext='';


      %%%%%%%%%%%%%%%%% DTW

      case 11

      errcode=121;

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      s1 = dtw(x1, y1);
      X = reshape(s1,Ny,Nx);
      matext='';



marwan's avatar
marwan committed
      %%%%%%%%%%%%%%%%% global CRP

      case length(check_meth)

      errcode=110+length(check_meth);
      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Reshape Embedding Vectors'),drawnow

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Compute Distance Matrix'),drawnow
      if m>1
         s=sqrt(sum(((x1(:,:)-y1(:,:)).^2)'));
      else
         s=abs(x1(:,1)-y1(:,1));
      end
      if check_stop(hCRP,hCtrl,nogui,obj), return, end

      set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'String','Building CRP Matrix'),drawnow
      X=reshape(s,Ny,Nx);
      matext='';

      end

  end % end plugin
marwan's avatar
marwan committed

  if nogui==0
    for i=1:length(obj.enable), set(obj.children(i),'Enable',obj.enable{i}); end
    set(h(1),'String','Create Recurrence Plot',...
             'ToolTip','Starts the computation - be patient.',...
             'Callback','crp2 compute')
    setptr([hCRP,hCtrl],'arrow')
  end

  %%%%%%%%%%%%%%%%% show CRP 
marwan's avatar
marwan committed

marwan's avatar
marwan committed
  if nogui==0
     Shuttle.hCRP=hCRP;
     Shuttle.hCtrl=hCtrl;
     Shuttle.matext=matext;
     Shuttle.xscale=xscale;
     Shuttle.yscale=yscale;
     Shuttle.mflag=mflag;
     Shuttle.m=m;
     Shuttle.t=t;
     Shuttle.cm=cm;
     Shuttle.txt_cross=txt_cross;
     if isempty(X)
         warn_str = ['Uuups! Empty matrix.',10,'I give up ...'];
         if strcmpi(computer,'GLNXA64')
             warn_str = [warn_str,10,'(Maybe non-appropriate plugin version due to different glibc.)']
         end
         warning(warn_str); 
         return
     end
marwan's avatar
marwan committed
     show_crp(X,Shuttle)
 else
marwan's avatar
marwan committed
   if nargout==1, xout=X; end
marwan's avatar
marwan committed
 end

 if strcmpi(get(findobj('Tag','SetPoint','Parent',hCtrl),'Enable'),'off')
     set(findobj('Tag','TextLOSsearch','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','SetPoint','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','ClearPoint','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','ClearAllPoint','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','LOSwidthXtext','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','LOSwidthX','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','LOSwidthYtext','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','LOSwidthY','Parent',hCtrl),'Enable','On')
     set(findobj('Tag','Apply2','Parent',hCtrl),'Enable','On')
 end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% store2

  case 'store2'
  
  errcode=15;
  t=get(findobj('Tag','Apply2','Parent',hCtrl),'UserData');
  if isempty(t)
     warndlg('The LOS vector is still empty. Please start the computation of the LOS before storing.','No LOS')
     waitforbuttonpress
  else
     assignin('base','t_out', t)
  end

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOSmove

  case 'LOSmove'
  
  errcode=16;
  if isempty(get(gco,'UserData'))
    set(gco,'UserData',1,'ButtonDownFcn','crp2 LOSmove_end')
    set(gcf,'WindowButtonMotionFcn','crp2 LOSmove')
  end
  h1 = round(get(gca,'CurrentPoint'));
  set(gco, 'XData', h1(1,1), 'YData', h1(1,2))
  clear h1

  case 'LOSmove_end'
  errcode=161;
  if ~isempty(get(gco,'UserData'))
    set(gco,'UserData',[],'ButtonDownFcn','')
    set(gcf,'WindowButtonMotionFcn','')
  end
    
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOSclear

  case 'LOSclear'
  
  errcode=17;
  if gcf==hCtrl
    figure(hCRP)
    k=waitforbuttonpress;
    h1 = round(get(gca,'CurrentPoint')); h1(2,:)=[]; h1(:,3)=[];
    finalRect = rbbox;
    h2 = round(get(gca,'CurrentPoint')); h2(2,:)=[]; h2(:,3)=[];
    h(1,1)=min(h1(:,1),h2(:,1));h(2,1)=max(h1(:,1),h2(:,1));
    h(1,2)=min(h1(:,2),h2(:,2));h(2,2)=max(h1(:,2),h2(:,2));
    
    i=find(fixp(:,1)>=h(1,1) & fixp(:,2)>=h(1,2) & fixp(:,1)<=h(2,1) & fixp(:,2)<=h(2,2));
    for j=1:length(i); delete(findobj('tag','fixp','Parent',findobj('Parent',hCRP,'Tag','CRPPlot'),'xdata',fixp(i(j),1))), end
  else
    h(1,1)=get(gco,'XData');h(1,2)=get(gco,'YData');
    delete(gco)
  end
  clear h

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOSallclear

  case 'LOSallclear'
  
    errcode=171;
    delete(findobj('tag','fixp','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')))
  
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOSset

  case 'LOSset'
  
  errcode=18;
  if gcf==hCtrl
    figure(hCRP)
    ginput(1);
  end
  
  h=round(get(gca,'currentp'));
  fixp(end+1,1)=h(1,1); fixp(end,2)=h(1,2);
  [i j]=sort(fixp(:,1));
  fixp=fixp(j,:);
  h0=uicontextmenu;
  line(h(1,1),h(1,2),1000,...
       'MarkerSize',12,...
       'Marker','.',...
       'Color',[1 0 0],...
       'Tag','fixp',...
       'UIContextMenu',h0)
  uimenu(h0, 'Label', 'Set Point', 'Callback', 'crp2 LOSset')
  uimenu(h0, 'Label', 'Move Point', 'Callback', 'crp2 LOSmove')
  uimenu(h0, 'Label', 'Clear Point', 'Callback', 'crp2 LOSclear')
  clear h h0

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LOSsearch

  case 'LOSsearch'

  errcode=19;
  if nogui==0
    setptr([hCRP,hCtrl],'watch')
    h=findobj('Tag','Apply2','Parent',hCtrl);
    obj.children=get(hCtrl,'Children');
    obj.enable=get(obj.children,'Enable'); 
    set(obj.children,'Enable','off')
    set(h(1),'String','Stop',...
             'ToolTip','Stops the computation.',...
             'Enable','on',...
	     'Callback','set(gcbo,''String'',''Stopped'')')
  end
  x0=0;
  y0=0;
  Dmax1=str2num(get(findobj('Tag','LOSwidthX','Parent',hCtrl),'string'));
  Dmax2=str2num(get(findobj('Tag','LOSwidthY','Parent',hCtrl),'string'));
  X=double(get(findobj('Tag','CRPData','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')),'UserData'));
  
  N=size(X); Nleer=0; Nvoll=0;

  minvalue=min(min(X));maxvalue=max(max(X));
  if (length(find(X==minvalue))+length(find(X==maxvalue))==length(X(:)))

  flagpoint=0;
   
% looks for the beginning of the diagonal LOS

  errcode=191;
  for i=1:N(2);
    if check_stop_LOS(hCRP,hCtrl,nogui,obj), break, end
    if i<=N(1)
      if ~isempty(fixp), if i>=fixp(1,1), x0=fixp(1,1); y0=fixp(1,2); end, end
      if X(i+y0,1+x0)~=0 
        v=i+y0;
        h=1+x0;
        break
      end
    end
    if X(1+y0,i+x0)~=0
      h=i+x0;
      v=1+y0;
      break
    end
  end
  
  Nleer=i-1;
%  t(1:v)=h;
  t(1+x0:h)=v;

% start estimation of the LOS

  errcode=192;
  mflag=0;
  i=2;
%  h_wait=waitbar(0,'Compute LOS ...',props.window);
  h_wait=waitbar(0,'Compute LOS ...');
  set(h_wait,'HandleVisibility','on');

  while h<N(2)-1 & v<N(1)-1 & mflag~=1,
    waitbar(h/N(2))
    if check_stop_LOS(hCRP,hCtrl,nogui,obj), break, end

    dw=1;

    if v < 1, v = 1; end
    if h < 1, h = 1; end
  
marwan's avatar
marwan committed
    W=X(v:v+dw,h:h+dw);
    W(1,1)=0; 

    if ~isempty(fixp)
      if find(h==fixp(:,1))
        dv=fixp(find(h==fixp(:,1)),2)-v;
marwan's avatar
marwan committed
        dh=1; 
            h0 = min(find(t > v+dv));
            if isempty(h0), h0 = h; end
            t0 = t(end) + dv;
            fixold = find(h==fixp(:,1));
            if ~isempty(fixold) & fixold > 1; fixold = fixp(fixold - 1,1); else fixold = 0; end
            h1 = max([h0-min([Dmax1,Dmax2]);1;fixold]);
            h2 = max([h0;1;fixold]);
%            h1 = max([h-2*(h-h0);1])
            if length(t) > h1 & length(t) > h2
                t1 = t(h1);
                t2 = t(h2);
                p = polyfit([h1 h2 h],[t1 t2 t0],1);
                t(h1:h) = polyval(p,h1:h);
%                t(h1:h) = spline([h1 h2 h],[t1 t2 t0],h1:h);
            end
            v = v + dv;
            dv = 1; dw = 0;
        end
marwan's avatar
marwan committed
        flagpoint=1;
      end
    end

    if flagpoint==0;

% looks for the existence of the next recurrence point in diagonal direction

    errcode=193;
    while sum(sum(W))==0 & mflag==0 & flagpoint==0,
        Nleer=Nleer+1;
        if ~isempty(fixp)
           if find(h+dw==fixp(:,1))
  	           W=1; dh=dw;
	           flagpoint=1;
	           break
           end
        end
        dw=dw+1;
        if v+dw < N(1) & h+dw < N(2)
           W=X(v:v+dw,h:h+dw);
           W(1,1)=0; 
        else
           mflag=1;
marwan's avatar
marwan committed
        end
    end

    if mflag==1
      break
    end

% determines the coordinates of the next recurrence point
  
    errcode=194;
    dh0=min(find(sum(W)));
    dv0=min(find(sum(W')));
    dh=min(find(W(dv0,:)));
    dv=min(find(W(:,dh0)));
    if dh>dh0, dh=dh0;end
    if dv>dv0, dv=dv0;end
  

% determines the local width of the diagonal LOS

    errcode=195;
    dh1=dh;
    dv1=dv;
    
    
    % neues Fenster
    WL1=Dmax1;
    WL2=Dmax2;
    if v+dv1-2+WL2>=N(1), WL2=N(1)-(v+dv1-2); end
    if h+dh1-2+WL1>=N(2), WL1=N(2)-(h+dh1-2); end
    Wn=X(v+dv1-1:v+dv1-2+WL2,h+dh-1:h+dh-2+WL1);
    % Schwerpunkt davon ausrechnen
    if sum(sum(Wn))~=0, Sh=sum(sum(Wn).*[1:WL1])/sum(sum(Wn));else Sh=WL1/2;end
    if sum(sum(Wn'))~=0,Sv=sum(sum(Wn').*[1:WL2])/sum(sum(Wn'));else Sv=WL2/2;end
    % neue Dmax berechnen
    if Sh>=Sv, Dmax2n=WL1*Sv/Sh; Dmax1n=WL1; end
    if Sv>Sh,  Dmax1n=WL2*Sh/Sv; Dmax2n=WL2; end

%    while X(v+dv1-1,h:dh-1)==1 & v+dv1-1<N(1) &dv1<Dmax2

    while X(v+dv1-1,h+dh-1)==1 & v+dv1-1<N(1) &dv1<Dmax2n
     dv1=dv1+1;
    end
%    while X(v:dv-1,h+dh1-1)==1 & h+dh1-1<N(2) & dh1<Dmax1
    while X(v+dv-1,h+dh1-1)==1 & h+dh1-1<N(2) & dh1<Dmax1n
      if ~isempty(fixp)
        if find(h+dh1-1==fixp(:,1)) & find(h+fix((dh1+dh)/2)==fixp(:,1))
          dv=fixp(find((h+dh1-1)==fixp(:,1)),2)-v;
	  if isempty(dv), dv=fixp(find((h+fix((dh1+dh)/2))==fixp(:,1)),2)-v; end
          flagpoint=1;
          break
        end
      end
      dh1=dh1+1;
    end


% compute the mean of the diagonal LOS
  
    errcode=196;
    if flagpoint==0
      dh=fix((dh1+dh)/2);
      dv=fix((dv1+dv)/2);

      if dh>0
        dh=dh-1;
      end
      if dv>0
        dv=dv-1;
      end
%      dh=dh+dh1;
    end
    
    end % flagpoint end
    flagpoint=0;


% output
    if dh~=0 & dv~=0
      t(h:h+dh)=v:dv/dh:v+dv;
    elseif dv~=0
      t(h)=v+dv;
    elseif dv==0
      t(h:h+dh)=v;
    end
    Nvoll=Nvoll+sqrt(dv^2+dh^2);

    if dh==0 & dv==0
      dh=1;
    end

% moves the startpoint for further looking
    h=h+dh;
    v=v+dv;

  end

  else
% DTW algorithm

   h_wait=waitbar(0,'Compute LOS ...');
   set(h_wait,'HandleVisibility','on',props.window);
  
   t=1;
   i=y0+1; j=x0+1;
   flag2=0;
   while i<N(1)-Dmax1-1 & j<N(2)-Dmax2-1
     errcode=197;
     waitbar(i/N(1)), j0=j;
%     [temp pos]=min([sum(sum(X(i:i+Dmax1,j+1:j+1+Dmax2))),sum(sum(X(i+1:i+1+Dmax1,j+1:j+1+Dmax2))),sum(sum(X(i+1:i+1+Dmax1,j:j+Dmax2)))]);
     [temp pos]=min([(mean(X(i,j+1:j+1+Dmax2))),(mean(diag(X(i+1:i+1+Dmax1,j+1:j+1+Dmax2)))),(mean(X(i+1:i+1+Dmax1,j)))]);
     switch(pos)
       case 1
        j=j+1;
	flag1=1;
       case 2
        i=i+1; j=j+1;
	flag1=1;
	flag2=1;
       case 3
        i=i+1;
	flag2=1;
     end
     if ~isempty(fixp) & flag1==1 & flag2==1
       errcode=1981;
       h=find(fixp(:,1)==j);
       if ~isempty(h) & i<max(fixp(h,2)); 
       h2=find(i<=fixp(h,2)); 
       i=fixp(h(h2(1)),2);  flag1=0; end
     end
     if ~isempty(fixp) & flag2==1 & flag1==1
       errcode=1982;
       h=find(fixp(:,2)==i);
       if ~isempty(h) & j<max(fixp(h,1)); 
       h2=find(j<=fixp(h,1));
       j=fixp(h(h2(1)),1);
       t(j0:j-1)=t(j0);
       flag2=0; end
     end
     t(j)=i;

   end

   t(find(~t))=1;

  end

  delete(h_wait)
  errcode=199;
  
  h1=findobj('Parent',hCRP,'Tag','CRPPlot');
  h2=findobj('Tag','Diagonal','Parent',h1);
  if isempty(h2)
    h2=line('Parent',h1,'visible','on','Tag','Diagonal','color',[1 0 0],...
    'LineWidth',1);
  end
  set(h2,'xdata',[1:length(t)],'ydata',t,'visible','on')
  set(findobj('Tag','Apply2','Parent',hCtrl),'UserData',t)
  if nogui==0
    setptr([hCRP,hCtrl],'arrow')
    h=findobj('Tag','Apply2','Parent',hCtrl);
    set(h(1),'String','Apply',...
     	         'ToolTip','Searches the LOS.',...
	         'Callback','crp2 LOSsearch')
    for i=1:length(obj.enable), set(obj.children(i),'Enable',obj.enable{i}); end
  end
  set(findobj('Tag','Store2','Parent',hCtrl),'Enable','On') 

end
try, set(0,props.root), end
warning on
set(0,'ShowHidden','Off')

%%%%%%% error handling

  try
    if nogui==0
        for i=1:length(obj.enable), set(obj.children(i),'Enable',obj.enable{i}); end
        set(h(1),'String','Apply',...
                 'ToolTip','Starts the computation - be patient.',...
                 'Callback','crp compute')
        setptr([hCRP,hCtrl],'arrow')
    end
    if nargout, xout = NaN; end
  end
marwan's avatar
marwan committed
  z=whos;x=lasterr;y=lastwarn;in=varargin{1};
  print_error('crp2',z,x,y,in,mflag,action)
  try, set(0,props.root), end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out=check_stop_LOS(hCRP,hCtrl,nogui,obj)

    global errcode
    errcode=errcode+.02;
    out=0;
    if nogui==0
      h=findobj('Tag','Apply2','Parent',hCtrl);
      if strcmpi(get(h(1),'String'),'stopped')
        set(h(1),'String','Apply',...
     	         'ToolTip','Searches the LOS.',...
	         'Callback','crp2 LOSsearch')
        for i=1:length(obj.enable), set(obj.children(i),'Enable',obj.enable{i}); end
        set(findobj('Tag','Status','Parent',findobj('Parent',hCRP,'Tag','CRPPlot')'),'String','Stopped'),drawnow
        setptr([hCRP,hCtrl],'arrow')
        out=1;
      end
    end