Skip to content
Snippets Groups Projects
crqa.m 47.1 KiB
Newer Older
marwan's avatar
marwan committed
function xout=crqa(varargin)
%CRQA   Computes and plots the CRQA measures.
%    Y=CRQA(X [,Y] [,param1,param2,...]) 
%    Recurrence quantification analysis of the cross recurrence
%    plot of the vectors X and Y. 
%
%    The input vectors can be multi-column vectors, where
%    each column will be used as a component of the 
%    phase-space vector. However, if the first column is
%    monotonically increasing, it will be used as an
%    time scale for plotting.
%
%    Y=CRQA(X,M,T,E,W,WS,LMIN,VMIN,TW) computes the 
%    recurrence quantification analysis of the recurrence
%    plot of X by using the dimension M, delay T, the
%    size of neighbourhood E, the window size W and 
%    a window shifting value of WS. LMIN and VMIN 
%    specify the minimal length of diagonal and vertical 
%    line structures (default is 2) and TW specifies the
%    Theiler window (default is 1).
%
%    CRQA(...) without any output arguments opens a
%    GUI for interactively control the CRQA. If an
%    output is specified with using the option 'gui',
%    then the output will contain the figure handle.
% 
%    Parameters: 
%    Dimension M, delay T, the size of
%    neighbourhood E, the window size W and the shift
%    value WS are the first five numbers after the data 
%    series; if W is empty, the whole plot will be calculated.
%    The next two optional numeric parameters LMIN and VMIN
%    specify the minimal length of line structures.
%
%    As the last numeric parameter, the size of the Theiler 
%    window TW can be specified (default is 1). This window 
%    excludes the recurrence points parallel to the main 
%    diagonal from the analysis. The application of the
%    Theiler window is useful only for recurrence plots. In
%    cross recurrence plots, the size of the Theiler window will
%    be set automatically to zero.
%
%    Further parameters can be used to switch between various 
%    methods of finding the neighbours of the phasespace 
%    trajectory, to suppress the normalization of the data 
%    and to suppress the GUI (useful in order to use this 
%    programme by other programmes). The minimal length of 
%    diagonal and vertical structures can be setted only in 
%    the GUI.
%
%    Methods of finding the neighbours.
%      maxnorm     - Maximum norm.
%      euclidean   - Euclidean norm.
%      minnorm     - Minimum norm.
%      nrmnorm     - Euclidean norm between normalized vectors
%                    (all vectors have the length one).
%      maxnorm     - Maximum norm, fixed recurrence rate.
marwan's avatar
marwan committed
%      fan         - Fixed amount of nearest neighbours.
%      inter       - Interdependent neighbours.
%      omatrix     - Order matrix.
%      opattern    - Order patterns recurrence plot.
marwan's avatar
marwan committed
%
%    Normalization of the data series.
%      normalize   - Normalization of the data.
%      nonormalize - No normalization of the data.
%
%    Suppressing the GUI.
%      gui         - Creates the GUI.
%      nogui       - Suppresses the GUI.
%      silent      - Suppresses all output.
%
%    Parameters not needed to be specified.
%
%    Output:
marwan's avatar
marwan committed
%      Y(:, 1) = RR     (recurrence rate)
%      Y(:, 2) = DET    (determinism)
%      Y(:, 3) = <L>    (mean diagonal line length)
%      Y(:, 4) = Lmax   (maximal diagonal line length)
%      Y(:, 5) = ENTR   (entropy of the diagonal line lengths)
%      Y(:, 6) = LAM    (laminarity)
%      Y(:, 7) = TT     (trapping time)
%      Y(:, 8) = Vmax   (maximal vertical line length)
%      Y(:, 9) = T1     (recurrence time of 1st type)
%      Y(:,10) = T2     (recurrence time of 2nd type)
marwan's avatar
marwan committed
%
%    Warning:
%    The RQA measures may differ from those of the RQA programmes by
%    Charles Webber Jr. For compatibility use a Theiler window of
%    size one and ensure that the data are normalized before by the
%    same distance which is used in the RQA programmes; e.g. normalize
%    with the maximal phase space diameter, which can be estimated 
%    with the programme PSS:
%
marwan's avatar
marwan committed
%      RQA = crqa(100*x/pss(x,dim,lag,'euclidean'),...
marwan's avatar
marwan committed
%               dim,lag,e,[],[],l_min,v_min,1,...
%               'euclidean','nonormalize','silent')
%
marwan's avatar
marwan committed
%    Examples: a = randn(300,1);
marwan's avatar
marwan committed
%              crqa(a,1,1,.2,40,2,'euc')
%
marwan's avatar
marwan committed
%              N = 500; w = 40; ws = 2;
%              a = 3.4:.6/(N-1):4;
%              b = .5; for i = 2:N, b(i) = a(i)*b(i-1)*(1-b(i-1)); end
%              y = crqa(b,3,2,.1,w,ws);
marwan's avatar
marwan committed
%              subplot(2,1,1), plot(a,b,'.','markersize',.1)
%              title('logistic map'), axis([3.4 4 0 1])
%              subplot(2,1,2), plot(a(1:ws:N-w),y(1:ws:N-w,1))
%              ylabel('recurrence rate'), axis([3.4 4 0 1])
%
%      
%    See also CRQAD, CRQAD_BIG, CRP, CRP2, CRP_BIG, DL, TT, PSS.
marwan's avatar
marwan committed
%
%    References: 
%    Trulla, L. L., Giuliani, A., Zbilut, J. P., Webber Jr., C. L.: 
%    Recurrence quantification analysis of the logistic equation with 
%    transients, Phys. Lett. A, 223, 1996.
%
%    Marwan, N., Wessel, N., Meyerfeldt, U., Schirdewan, A., Kurths, J.: 
%    Recurrence Plot Based Measures of Complexity and its Application to 
%    Heart Rate Variability Data, Phys. Rev. E, 66(2), 2002.
%
%    Gao, J. B.: 
%    Recurrence Time Statistics for Chaotic Systems and Their 
%    Applications, Phys. Rev. Lett., 83(16), 1999.
%
%    Thiel, M., Romano, M. C., Kurths, J., Meucci, R., Allaria, E., 
%    Arecchi, F. T.:
%    Influence of observational noise on the recurrence quantification 
%    analysis, Physica D, 171(3), 2002.

% Copyright (c) 1998-2007 by AMRON
marwan's avatar
marwan committed
% Norbert Marwan, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
%
% $Date$
% $Revision$
%
% $Log$
% Revision 5.31  2007/01/10 12:29:24  marwan
% bug fix: rare bug due to a Matlab bug in MenuBar property for some XWindows systems (and only if matlab is running without java)
%
% Revision 5.30  2006/11/27 11:45:40  marwan
% bug entropy vertical white lines resolved
%
% Revision 5.29  2006/10/24 14:16:16  marwan
% minor change: sigma in title line of RP shown only for normalised data
%
% Revision 5.28  2006/09/13 15:53:36  marwan
% Bug during printing solved
%
marwan's avatar
marwan committed
% Revision 5.27  2006/07/04 14:04:22  marwan
% order patterns for multi-column vectors without embedding
%
% Revision 5.26  2006/03/29 13:07:55  marwan
% problems regarding OPRPs and embedding resolved
%
% Revision 5.25  2006/02/14 11:45:49  marwan
% *** empty log message ***
%
marwan's avatar
marwan committed
% Revision 5.24  2006/02/08 13:25:12  marwan
% bug in plugin support (lmin and vmin not correct) resolved
%
% Revision 5.23  2006/02/06 14:55:41  marwan
% bug in order patterns plugin support solved
%
% Revision 5.22  2006/02/06 13:46:17  marwan
% plugin for order patterns recurrence plots supported
%
% Revision 5.21  2005/11/28 10:16:35  marwan
% && and || changed to & and |
% (seems to cause problems in Matlab 12.1)
%
marwan's avatar
marwan committed
% Revision 5.20  2005/09/13 12:08:12  marwan
% fix of a serious bug in the GUI
%
marwan's avatar
marwan committed
% Revision 5.19  2005/07/29 09:02:59  marwan
% normalization bug resolved
%
marwan's avatar
marwan committed
% Revision 5.18  2005/04/15 09:02:32  marwan
% minor bugfix in plugin section
%
marwan's avatar
marwan committed
% Revision 5.17  2005/04/08 09:54:11  marwan
% plugin added
%
marwan's avatar
marwan committed
% Revision 5.15  2005/04/06 13:29:12  marwan
% computation of recurrence times is now faster
%
% Revision 5.14  2005/04/06 12:59:24  marwan
% several small bug fixes which improves the precision
%
% Revision 5.13  2005/04/01 12:19:51  marwan
% bug in minimal length for diagonal and vertical lines fixed
%
% Revision 5.12  2005/03/16 13:16:12  marwan
% bug in output fixed (same time scales for all sub-plots)
%
% Revision 5.11  2005/03/16 12:23:08  marwan
% support long data series by using crp_big
%
% Revision 5.10  2005/03/16 11:23:52  marwan
% automatic detection:
% * of provided time-scale
% * if multi-column input can be used as phase-space vector
%
marwan's avatar
marwan committed
% Revision 5.9  2004/12/23 07:49:03  marwan
% bug in order patterns RP fixed (empty order patterns)
%
% Revision 5.8  2004/11/15 12:56:01  marwan
% bug fix in method checking
%
marwan's avatar
marwan committed
% Revision 5.7  2004/11/15 12:53:27  marwan
% covariance plot commented out
%
marwan's avatar
marwan committed
% Revision 5.6  2004/11/12 08:40:46  marwan
% order patterns recurrence plot added
%
% Revision 5.5  2004/11/10 07:04:50  marwan
% initial import
%
marwan's avatar
marwan committed
%
% This program is part of the new generation XXII series.
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or any later version.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% programme properties

global errcode props


init_properties

lmin=2;
vmin=2;
nonorm=1;
theiler_window=1;
hw=-1;
marwan's avatar
marwan committed
xscale = [];
yscale = [];
marwan's avatar
marwan committed


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% check the input

error(nargchk(1,13,nargin));
if nargout>1, error('Too many output arguments'), end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% splash the GPL

splash_gpl('crp');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% check and read the input

marwan's avatar
marwan committed
errcode=1;

   set(0,'ShowHidden','on')
   varargin{14}=[];
   i_double=find(cellfun('isclass',varargin,'double'));
   i_char=find(cellfun('isclass',varargin,'char'));
   t=1;
   w=[];wstep=0; method='max'; method_n=1;
marwan's avatar
marwan committed
   time_scale_flag=1; time_scale_flag_x=1;
marwan's avatar
marwan committed
   nogui=0;
      check_meth={'ma','eu','mi','nr','rr','fa','in','om','op','di'}; 	% maxnorm, euclidean, nrmnorm,  fan, distance
marwan's avatar
marwan committed
      check_gui={'gui','nog','sil'};                % gui, nogui, silent
      check_norm={'non','nor'};                        % nonormalize, normalize
marwan's avatar
marwan committed
   
   if nargin & ischar(varargin{1})
marwan's avatar
marwan committed
        set(0,'ShowHidden','on');h=findobj('Tag','Msgbox_Check Input');delete(h)
        action=varargin{1};
        h=findobj('Tag','crqa_Fig');
        temp=get(h(1),'UserData');
        x=temp{1}; y=temp{2};
        h=findobj('Tag','crqa_axes_Data','Parent',h(1)); % get handle of plot-axes object
        h_crqa_axes_Data = h(1);
        h=findobj('Type','line','Parent',h(1)); % get handle of line-object
        xscale=get(h(1),'xdata');
        h=findobj('Tag','crqa_m');
        m=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_maxLag');
        t=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_eps');
        e=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_method');
        method={'Maximum Norm','Euclidean Norm','Minimum Norm','Normalized Norm','Maximum Norm, fixed RR','FAN','Interdependent','Order Matrix','Order Pattern','Distance Plot'};
marwan's avatar
marwan committed
        method=method{get(h(1),'Value')};
        method_n = get(h(1),'Value');
        nonorm=get(h(1),'UserData');
        h=findobj('Tag','crqa_lmin');
        lmin=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_vmin');
        vmin=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_theiler');
        theiler_window=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_w');
        w=str2num(get(h(1),'String'));
        h=findobj('Tag','crqa_ws');
        wstep=str2num(get(h(1),'String'));
marwan's avatar
marwan committed

marwan's avatar
marwan committed
   elseif nargin & isnumeric(varargin{1})

marwan's avatar
marwan committed
       % check the text input parameters for method, gui 
        temp_meth=0;
        temp_norm=0;
        temp_gui=0;
        if ~isempty(i_char)
             for i=1:length(i_char), 
                varargin{i_char(i)}(4)='0';
                temp_gui=temp_gui+strcmpi(varargin{i_char(i)}(1:3),check_gui'); 
                temp_norm=temp_norm+strcmpi(varargin{i_char(i)}(1:3),check_norm'); 
                temp_meth=temp_meth+strcmpi(varargin{i_char(i)}(1:2),check_meth'); 
             end
             method_n=min(find(temp_meth));
             nogui=min(find(temp_gui))-1;
             nonorm=min(find(temp_norm))-1;
             for i=1:length(i_char); temp2(i,:)=varargin{i_char(i)}(1:3); end
             i_char(strmatch(check_gui(find(temp_gui)),temp2))=[];
             if isempty(nonorm), nonorm=1; end
             if nonorm>1, nonorm=1; end
             if isempty(nogui), nogui=0; end
             if isempty(method_n), method_n=1; end
             if nogui>2, nogui=1; end
             if method_n>length(check_meth), method0=length(check_meth); end
             method=check_meth{method_n};
        else
             nogui=0;
             if nargout
                nogui=1;
                action='compute'; 
             end
        end
        if nogui==0
            action='init';
        else
marwan's avatar
marwan committed
            action='compute'; 
marwan's avatar
marwan committed
        end
marwan's avatar
marwan committed
      
marwan's avatar
marwan committed
        % get the parameters for creating RP
        if max(size(varargin{1}))<=3
            disp('Error using ==> crqa')
            disp('To less values in data X.')
            return
        end
        x=double(varargin{1});
        if isempty(varargin{2}) | ~isnumeric(varargin{2}), y=x; else
        y=double(varargin{2}); end
  %      if sum(double(diff(x(:,1))<=0)), time_scale_flag=0; end

        if (isnumeric(varargin{2}) & max(size(varargin{2}))==1) | ~isnumeric(varargin{2})
            y=x;
            if ~isempty(varargin{i_double(2)}), m=varargin{i_double(2)}(1); else m=1; end
            if ~isempty(varargin{i_double(3)}), t=varargin{i_double(3)}(1); else t=1; end
            if ~isempty(varargin{i_double(4)}), e=varargin{i_double(4)}(1); else e=.1; end
            if ~isempty(varargin{i_double(5)}), w=varargin{i_double(5)}(1); else w=varargin{i_double(5)}; end
            if ~isempty(varargin{i_double(6)}), wstep=varargin{i_double(6)}(1); else wstep=1; end
            if ~isempty(varargin{i_double(7)}), lmin=varargin{i_double(7)}(1); end
            if ~isempty(varargin{i_double(8)}), vmin=varargin{i_double(8)}(1); end
            if ~isempty(varargin{i_double(9)}), theiler_window=varargin{i_double(9)}(1); end
        else
            if ~isempty(varargin{i_double(3)}), m=varargin{i_double(3)}(1); else m=1; end
            if ~isempty(varargin{i_double(4)}), t=varargin{i_double(4)}(1); else t=1; end
            if ~isempty(varargin{i_double(5)}), e=varargin{i_double(5)}(1); else e=.1; end
            if ~isempty(varargin{i_double(6)}), w=varargin{i_double(6)}(1); else w=varargin{i_double(6)}; end
            if ~isempty(varargin{i_double(7)}), wstep=varargin{i_double(7)}(1); else wstep=1; end
            if ~isempty(varargin{i_double(8)}), lmin=varargin{i_double(8)}(1); end
            if ~isempty(varargin{i_double(9)}), vmin=varargin{i_double(9)}(1); end
            if ~isempty(varargin{i_double(10)}), theiler_window=varargin{i_double(10)}(1); end
        end
   else
        disp('Error using ==> crqa')
        disp('No valid arguments.')
        return
   end
   if method==8 & m > 1, 
       m=1; 
       disp('Warning: For order matrix a dimension of one is used.')
   end
   
   
   Nx=length(x); Ny=length(y);
   if size(x,1)<size(x,2), x=x'; end
   if size(y,1)<size(y,2), y=y'; end

   if (strcmpi(method,'Order Pattern') | strcmpi(method,'op')) & m == 1 & size(x,2) < 2
       m=2; 
       h = findobj('Tag','crqa_m');
       if ~isempty(h)
           errordlg(['For order patterns recurrence plots the',10,'dimension must be larger than one.'],'Dimension too small')
           set(h(1),'String',num2str(m))
       else
           disp(['Warning: For order patterns recurrence plots the dimension must',10,...
                'be larger than one. ',...
                'Embedding dimension is set to ',num2str(m),'.'])
       end
   end
marwan's avatar
marwan committed

   if size(x,2)>=2
      xscale=x(:,1); 
marwan's avatar
marwan committed
      if ~isempty(find(diff(xscale)<0)) % multi-column data vector, each column used as vector component
          time_scale_flag=0; 
          time_scale_flag_x=0; 
          xscale=(1:length(x))'; 
      end
marwan's avatar
marwan committed
   else
      xscale=(1:length(x))'; 
marwan's avatar
marwan committed
      time_scale_flag=0; 
      time_scale_flag_x=0; 
marwan's avatar
marwan committed
   end
marwan's avatar
marwan committed

marwan's avatar
marwan committed
   if size(y,2)>=2
      yscale=y(:,1); 
marwan's avatar
marwan committed
      time_scale_flag=1; 
      if ~isempty(find(diff(yscale)<0))
          time_scale_flag=0; 
          yscale=(1:length(y))';
      end
marwan's avatar
marwan committed
      if time_scale_flag & ~time_scale_flag_x % if time-scale given in y, but not in x -> error
marwan's avatar
marwan committed
          if ~nogui
             errordlg(['A time-scale for the second data is series given, but not for the first!',10,'(The time-scale has to be inlcuded as the first colummn of the first data vector.)'],'Check Data')
             waitforbuttonpress
             return
          else
             error(['A time-scale for the second data series is given, but not for the first!',10,'(The time-scale has to be inlcuded as the first colummn of the first data vector.)'])
          end
      end
marwan's avatar
marwan committed
   else
       yscale=(1:length(y))';
marwan's avatar
marwan committed
       time_scale_flag=0; 
marwan's avatar
marwan committed
   end
marwan's avatar
marwan committed
   if time_scale_flag_x & ~time_scale_flag % if time-scale given in x, but not in y
marwan's avatar
marwan committed
       if length(x) ~= length(y)
          if ~nogui
             errordlg(['If you are using the time-scale given by the first vector also for',10,'the second vector, both vectors should have the same size!'],'Check Data')
             waitforbuttonpress
             return
          else
             error(['If you are using the time-scale given by the first vector also for',10,'the second vector, both vectors should have the same size!'])
          end
       end
       y = [xscale, y];
       yscale = xscale;
       time_scale_flag=1; 
   end
marwan's avatar
marwan committed
   if max(size(x))~=max(size(y)),
        if ~nogui, errordlg('Data must have the same length.','Check Data'), waitforbuttonpress, return, else error('Data must have the same length.'), end
marwan's avatar
marwan committed
   end
   if e<0, 
marwan's avatar
marwan committed
       e=1; 
       if ~nogui
          warndlg('The threshold size E can not be negative and is now set to 1.','Check Data')
          waitforbuttonpress
          h=findobj('Tag','crqa_eps');
          if ~isempty(h), set(h(1),'String',num2str(e)), end
       else 
          disp('The threshold size E can not be negative and is now set to 1.'), 
       end
   end
   if t<1, 
     t=1; 
     if ~nogui
        warndlg('The delay T can not be smaller than one and is now set to 1.','Check Data')
        waitforbuttonpress
        h=findobj('Tag','crqa_maxLag');
        if ~isempty(h), set(h(1),'String',num2str(t)), end
     else
        disp('The delay T can not be smaller than one and is now set to 1.')
     end
   end
   if m < 1; m = 1; end
marwan's avatar
marwan committed
   if isempty(w), w=Nx; wstep=1; end
   if w < 5+(m-1)*t, 
     w=5+(m-1)*t;
     if ~nogui, warndlg('The window size W exceeds the valid range.','Check Data')
        waitforbuttonpress
        h=findobj('Tag','crqa_w');
        if ~isempty(h), set(h(1),'String',num2str(w)), end
     else, disp('The window size W exceeds the valid range.'), end
   end
   if w>Nx, 
     w=Nx; wstep=1;; 
     if ~nogui, warndlg('The window size W exceeds the valid range.','Check Data')
        waitforbuttonpress
        h=findobj('Tag','crqa_w');
        if ~isempty(h), set(h(1),'String',num2str(w)), end
     else, disp('The window size W exceeds the valid range.'), end
   end
   if wstep<1 | wstep>Nx/3, 
     wstep=2; 
     if ~nogui, warndlg('The window shifting value WS exceeds the valid range.','Check Data')
        waitforbuttonpress
        h=findobj('Tag','crqa_ws');
        if ~isempty(h), set(h(1),'String',num2str(wstep)), end
     else, disp('The window shifting value WS exceeds the valid range.'), end
   end
   if vmin<1 | vmin>Nx, 
     vmin=2; 
     if ~nogui, warndlg('The minimal length for vertical lines is not valid.','Check Data')
        waitforbuttonpress
        h=findobj('Tag','crqa_vmin');
        if ~isempty(h), set(h(1),'String',num2str(vmin)), end
     else, disp('The minimal length for vertical lines is not valid.'), end
   end
   if lmin<1 | lmin>Nx, 
     lmin=2; 
     if ~nogui, warndlg('The minimal length for diagonal lines is not valid.','Check Input')
        waitforbuttonpress
        h=findobj('Tag','crqa_lmin');
        if ~isempty(h), set(h(1),'String',num2str(lmin)), end
     else, disp('The minimal length for diagonal lines is not valid.'), end
   end
   if theiler_window<0 | theiler_window>Nx, 
     theiler_window=1; 
     if ~nogui, warndlg('The value for the Theiler window is not valid.','Check Input')
        waitforbuttonpress
        h=findobj('Tag','crqa_theiler');
        if ~isempty(h), set(h(1),'String',num2str(theiler_window)), end
     else, disp('The value for the Theiler window is not valid.'), end
   end
   if x~=y, theiler_window=0; end
   t=round(t); m=round(m); w=round(w); wstep=round(wstep); vmin=round(vmin); lmin=round(lmin); theiler_window=round(theiler_window);
marwan's avatar
marwan committed

marwan's avatar
marwan committed
   % normalize data if necessary
   if nonorm
       x = (x - repmat(mean(x),length(x),1)) ./ repmat(std(x),length(x),1);
       y = (y - repmat(mean(y),length(y),1)) ./ repmat(std(y),length(y),1);
   end
      
marwan's avatar
marwan committed

marwan's avatar
marwan committed
switch(action)

  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% create gui

case 'init'

  errcode=3;
       
  oldunit=get(0,'unit');
  set(0,'Unit','char')
  scr=get(0,'ScreenSize'); 
  set(0,'Unit',oldunit)
  
marwan's avatar
marwan committed
  h8=figure(props.window,...                                % Plot Figure
marwan's avatar
marwan committed
            'Tag','crqa_Fig',...
marwan's avatar
marwan committed
            'MenuBar','Figure',...
marwan's avatar
marwan committed
            'Position',[(scr(3)-150)/2 scr(4)-50 150.0000 44],...
            'PaperType','a4',...
marwan's avatar
marwan committed
            'PaperPosition',[0.25 0.25 7.7677 11.193],...
            'PaperOrientation','portrait',...
marwan's avatar
marwan committed
            'UserData',{x,y,time_scale_flag},...
marwan's avatar
marwan committed
            'Name','Cross Recurrence Quantification Analysis');
marwan's avatar
marwan committed
  
  set(0,'showhidden','on')
  h=findobj('Label','&Help','Type','uimenu');
  if isempty(h)
    h=uimenu('Label','&Help');
    h2=uimenu('Parent',h(1),'Label','&Help Cross Recurrence Quantification Analysis','Callback','helpwin crqa');
  else
    h1=flipud(get(h(1),'Children'));
    set(h1(1),'Separator','on')
    h2=uimenu('Parent',h(1),'Label','&Help Cross Recurrence Quantification Analysis','Callback','helpwin crqa');
    copyobj(h1,h(1))
    delete(h1)
  end
  set(0,'showhidden','off')


  h=axes(props.axes,...
            'Position',[89+30 24.8+14.5 6.8 3.5]);    
  logo=load('logo');
  h2=imagesc([logo.logo fliplr(logo.logo)]);
  set(h2,'Tag','uniLogo')
  set(h,props.logo,'Tag','axes_logo')
  h=uicontrol(props.text,...
            'Tag','text_logo',...
marwan's avatar
marwan committed
            'String','Uni Potsdam',...
marwan's avatar
marwan committed
            'Position',[97+30 24.2143+14.5 22  3.5714]);    
  h2=textwrap(h,{[char(169),' AGNLD'],'University of Potsdam','1998-2007'});
marwan's avatar
marwan committed
  set(h,'String',h2)


%%%%%%%%%%% plots

  axes_height = 5.8;
  axes_base = 3;
  axes_hoffset = 2.5;
  h=axes(props.axes,...
            'Tag','crqa_axes_Data',...
marwan's avatar
marwan committed
                'Box','On',...
marwan's avatar
marwan committed
            'Position',[11.2017  axes_base+4*(axes_height+axes_hoffset)    28.1785+15    axes_height]);    
marwan's avatar
marwan committed

  if time_scale_flag
      plot(xscale,x(:,2:end),'color',props.line.Color)
      if ~all(x(:)==y(:)) hold on; plot(yscale,y(:,2:end),'r'), end
  else
marwan's avatar
marwan committed
      plot(xscale,x(:,1:end),'color',props.line.Color)
marwan's avatar
marwan committed
      if ~all(x(:)==y(:)) hold on; plot(yscale,y(:,1),'r'), end
  end
marwan's avatar
marwan committed
  set(h,'Tag','crqa_axes_Data','color',props.axes.Color)
  ylabel('Data')

  h=axes(props.axes,...
            'Tag','crqa_axes_Var',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[49.8023+15   axes_base+4*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
marwan's avatar
marwan committed
  if time_scale_flag
     x_var = winplot(x(:,2:end),w,wstep,2);
  else
     x_var = winplot(x(:,1),w,wstep,2);
  end
marwan's avatar
marwan committed
  if size(x_var,1) > 2
    h2=stairs(xscale(round(x_var(:,1))),x_var(:,2));
    set(h2,'color',props.line.Color)
  else
    cla
    text(0.5,0.5,sprintf('%6.4f',x_var(1,2)),'FontWeight','bold','HorizontalAlign','Center')
  end
marwan's avatar
marwan committed
  if ~all(x(:)==y(:)) 
    hold on; 
    if time_scale_flag
        y_var = winplot(y(:,2:end),w,wstep,2); 
    else
        y_var = winplot(y(:,1),w,wstep,2); 
    end
marwan's avatar
marwan committed
    if size(y_var,1) > 2
      stairs(yscale(round(y_var(:,1))),y_var(:,2),'r')
    else
      cla
      text(0.5,0.5,sprintf('%6.5f, %6.5f',x_var(2,2),y_var(1,2)),'FontWeight','bold','HorizontalAlign','Center')
    end
    h3=axes('Units','Char',...
              'Pos',get(h,'Pos'),...
              'XLim',get(h,'XLim'),...
              'YLim',get(h,'YLim'),...
              'YAxisLocation','right',...
              'Color','none',...
              'Tag','crqa_axes_CoVar',...
              'visible','off');
marwan's avatar
marwan committed
  end
  set(gcf,'CurrentAxes',h);
  ylabel('Variance')
  set(h,'Tag','crqa_axes_Var','color',props.axes.Color)


  h=axes(props.axes,...
            'Tag','crqa_axes_RR',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[11.2017   axes_base+3*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('RR')

  h=axes(props.axes,...
            'Tag','crqa_axes_DET',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[49.8023+15   axes_base+3*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('DET')

  h=axes(props.axes,...
            'Tag','crqa_axes_L',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[11.2017   axes_base+2*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('L')

  h=axes(props.axes,...
            'Tag','crqa_axes_ENTR',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[49.8023+15   axes_base+2*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('ENTR')

  h=axes(props.axes,...
            'Tag','crqa_axes_LAM',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[11.2017    axes_base+1*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('LAM')

  h=axes(props.axes,...
            'Tag','crqa_axes_TT',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[49.8023+15    axes_base+1*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('TT')

  h=axes(props.axes,...
            'Tag','crqa_axes_T1',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[11.2017    axes_base+0*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('T_1')

  h=axes(props.axes,...
            'Tag','crqa_axes_T2',...
marwan's avatar
marwan committed
            'Box','On',...
marwan's avatar
marwan committed
            'Position',[49.8023+15    axes_base+0*(axes_height+axes_hoffset)   28.1785+15    axes_height]);    
  ylabel('T_2')

%%%%%%%%%%% embedding
  h=uicontrol(props.frame,...
            'Tag','frame',...
            'Position',[86+30 29.+2.8 29 5.7]);    
 
  h=uicontrol(props.text,...
            'Tag','text',...
            'Fontangle','italic',...
marwan's avatar
marwan committed
            'String','Embedding parameters',...
marwan's avatar
marwan committed
            'Position',[87+30 34.2+1.6 16.8333  1.5000]);    
 
  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','Dimension:',...
marwan's avatar
marwan committed
            'Position',[89+30 32+2.2 16.8333  1.5000]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_m',...
marwan's avatar
marwan committed
            'String',num2str(m),...
            'ToolTip','Select the embedding dimension.',...
marwan's avatar
marwan committed
            'Position',[104+30 32+.2+2.2 7  1.5000]);    

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','Delay:',...
marwan's avatar
marwan committed
            'Position',[89+30 30+2.32 16.8333  1.5000]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_maxLag',...
marwan's avatar
marwan committed
            'String',num2str(t),...
            'ToolTip','Insert the embedding delay time.',...
marwan's avatar
marwan committed
            'Position',[104+30 30+.2+2.32 7  1.5000]);    

%%%%%%%%%%% neigbourhood
  h=uicontrol(props.frame,...
            'Tag','frame',...
            'Position',[86+30 21.9+2.5 29 6.7]);    
 
  h=uicontrol(props.text,...
            'Tag','text',...
            'Fontangle','italic',...
marwan's avatar
marwan committed
            'String','Neighbourhood',...
marwan's avatar
marwan committed
            'Position',[87+30 26.8+2.5 23  1.5000]);    
 
  h=uicontrol(props.popup,...
            'Tag','text',...
            'Tag','crqa_method',...
marwan's avatar
marwan committed
            'UserData',nonorm,...
            'Value',method_n,...
            'String','Maximum Norm|Euclidean Norm|Minimum Norm|Normalized Norm|Fixed RR|Fixed Amount|Interdependent|Order Matrix|Order Patterns|Distance Plot',...
marwan's avatar
marwan committed
            'Position',[89+30 24.6+.2+2.5 22  1.7]);    

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','Threshold:',...
marwan's avatar
marwan committed
            'Position',[89+30 22.6+2.5 16.8333  1.5]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_eps',...
marwan's avatar
marwan committed
            'String',num2str(e),...
            'ToolTip','Insert the size of neighbourhood.',...
marwan's avatar
marwan committed
            'Position',[104+30 22.6+.2+2.5 7  1.5000]);    

%%%%%%%%%%% crqa parameters
  h=uicontrol(props.frame,...
            'Tag','frame',...
            'Position',[86+30 11+.5 29 12.2]);    
 
  h=uicontrol(props.text,...
            'Tag','text',...
            'Fontangle','italic',...
marwan's avatar
marwan committed
            'String','CRQA parameters',...
marwan's avatar
marwan committed
            'Position',[87+30 19.4+2.5 23  1.5000]);    

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','min. Diagonal:',...
marwan's avatar
marwan committed
            'Position',[89+30 17.6+2.5 16.8333  1.5]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_lmin',...
marwan's avatar
marwan committed
            'String',num2str(lmin),...
            'ToolTip','Insert the minimal length of a diagonal line.',...
marwan's avatar
marwan committed
            'Position',[104+30 17.6+.2+2.5 7  1.5000]);    

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','min. Vertical:',...
marwan's avatar
marwan committed
            'Position',[89+30 15.6+2.5 16.8333  1.5]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_vmin',...
marwan's avatar
marwan committed
            'String',num2str(vmin),...
            'ToolTip','Insert the minimal length of a vertical line.',...
marwan's avatar
marwan committed
            'Position',[104+30 15.6+.2+2.5 7  1.5000]);    

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','Theiler wind.:',...
marwan's avatar
marwan committed
            'Position',[89+30 15.6+.5 16.8333  1.5]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_theiler',...
marwan's avatar
marwan committed
            'String',num2str(theiler_window),...
            'ToolTip','Insert the size for the Theiler window.',...
marwan's avatar
marwan committed
            'Position',[104+30 15.6+.2+.5 7  1.5000]);    
  if x~=y; set(h,'enable','off'); end

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','Window size:',...
marwan's avatar
marwan committed
            'Position',[89+30 13.6+.5 16.8333  1.5]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_w',...
marwan's avatar
marwan committed
            'String',num2str(w),...
            'ToolTip','Insert the size of the sliding window.',...
marwan's avatar
marwan committed
            'Position',[104+30 13.6+.2+.5 7  1.5000]);    

  h=uicontrol(props.text,...
            'Tag','text',...
marwan's avatar
marwan committed
            'String','Window step:',...
marwan's avatar
marwan committed
            'Position',[89+30 11.6+.5 16.8333  1.5]);    

  h=uicontrol(props.edit,...
            'Tag','crqa_ws',...
marwan's avatar
marwan committed
            'String',num2str(wstep),...
            'ToolTip','Insert the step width for sliding the window.',...
marwan's avatar
marwan committed
            'Position',[104+30 11.6+.2+.5 7  1.5000]);    


%%%%%%%%%%% buttons

  h=uicontrol(props.frame,...
            'Tag','frame',...
            'Position',[86+30 1.3+.5 29 9]);    


  h=uicontrol(props.button,...
marwan's avatar
marwan committed
                'String','Store',...
                'Tag','crqa_button_store',...
              'Enable','Off',...
                'ToolTip','Stores the CRQA analysis into a variable in the workspace.',...
                'Callback','crqa store',...
                'Position',[100.5+30  7.4+.5 10.5  2.2143]);
marwan's avatar
marwan committed

  h=uicontrol(props.button,...
            'Tag','crqa_button_print',...
marwan's avatar
marwan committed
            'CallBack','crqa print',...
                'ToolTip','Prints the CRQA window.',...
            'String','Print',...
marwan's avatar
marwan committed
            'Position',[89+30  7.4+.5 10.5  2.2143]);    

  h=uicontrol(props.button,...
            'Tag','crqa_button_close',...
marwan's avatar
marwan committed
            'CallBack','crqa close',...
                'ToolTip','Closes the CRQA window.',...
            'String','Close',...
marwan's avatar
marwan committed
            'Position',[89+30  2.1+.5 22  2.2143]);    
  
  h=uicontrol(props.button,...
marwan's avatar
marwan committed
                'String','Apply',...
                'Tag','crqa_button_apply',...
                'ToolTip','Starts the computation.',...
                'Callback','crqa compute',...
                'Position',[89+30  4.75+.5 22  2.2143]);
marwan's avatar
marwan committed
  
  set(0,'ShowHidden','on')
  set(h8, 'HandleVis','CallBack')
  tags={'crqa_Fig';'axes_logo';'text_logo';'crqa_theiler';'frame';'text';'crqa_axes_Data';'crqa_axes_Var';'crqa_axes_CoVar';'crqa_axes_RR';'crqa_axes_DET';'crqa_axes_L';'crqa_axes_ENTR';'crqa_axes_LAM';'crqa_axes_TT';'crqa_axes_T1';'crqa_axes_T2';'crqa_m';'crqa_maxLag';'crqa_method';'crqa_eps';'crqa_lmin';'crqa_vmin';'crqa_w';'crqa_ws';'crqa_button_store';'crqa_button_print';'crqa_button_close';'crqa_button_apply'};
  h=[];
  for i=1:length(tags); h=[h; findobj('Tag',tags{i})]; end
  set(h,'Units','Norm')
  if nargout; xout=h8; end
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% close windows 

case 'close'
  errcode=101;
  set(0,props.root)
  h=findobj('Tag','crqa_Fig');
  if ~isempty(h), close(h(1)), end 
  clear all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% store

case 'store'
  errcode=0;
  if ~isempty(findobj('Tag','crqa_button_store'))
    h=findobj('Tag','crqa_button_store');
    h1=findobj('Tag','crqa_button_close');
    if ~isempty(h1), vname_old=get(h1(1),'UserData'); else vname_old=''; end
    if isempty(vname_old), vname_old=''; end
    vname=char(inputdlg('Choose a variable name.','Store output',1,{vname_old}));
    if isempty(vname)
      return
    else
      crqa_values=get(h(1),'UserData');
      assignin('base',vname, [crqa_values])
      warndlg(['CRQA measures have been assigned to the workspace variable ''',vname,'''.'],'Store output');
marwan's avatar
marwan committed
           waitforbuttonpress
marwan's avatar
marwan committed
      set(h1(1),'UserData',vname)
    end
  end
  set(0,props.root)

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% print
  case 'print'

    errcode=91;
    h=findobj('Tag','crqa_axes_Data','Parent',gcf); h_axes.h(1)=h(1);
    h=findobj('Tag','crqa_axes_Var','Parent',gcf); h_axes.h(2)=h(1);
marwan's avatar
marwan committed
    h=findobj('Tag','crqa_axes_CoVar','Parent',gcf); if ~isempty(h), h_axes.h(11)=h(1); end
marwan's avatar
marwan committed
    h=findobj('Tag','crqa_axes_RR','Parent',gcf); h_axes.h(3)=h(1);
    h=findobj('Tag','crqa_axes_DET','Parent',gcf); h_axes.h(4)=h(1);
    h=findobj('Tag','crqa_axes_L','Parent',gcf); h_axes.h(5)=h(1);
    h=findobj('Tag','crqa_axes_ENTR','Parent',gcf); h_axes.h(6)=h(1);
    h=findobj('Tag','crqa_axes_LAM','Parent',gcf); h_axes.h(7)=h(1);
    h=findobj('Tag','crqa_axes_TT','Parent',gcf); h_axes.h(8)=h(1);
    h=findobj('Tag','crqa_axes_T1','Parent',gcf); h_axes.h(9)=h(1);
    h=findobj('Tag','crqa_axes_T2','Parent',gcf); h_axes.h(10)=h(1);
    h=findobj('Tag','uniLogo');
    tags={'text_logo';'frame';'text';'crqa_m';'crqa_maxLag';'crqa_method';'crqa_eps';'crqa_lmin';'crqa_vmin';'crqa_theiler';'crqa_w';'crqa_ws';'crqa_button_store';'crqa_button_print';'crqa_button_close';'crqa_button_apply';};
    for i=1:length(tags); h=[h; findobj('Tag',tags{i},'Parent',gcf)]; end
    set(h,'Visible','Off')
    
    set(h_axes.h,'Units','Character');
    h_axes.old_pos=get(h_axes.h,'Position');

    axes_height = .13;
    axes_base = 0.065;
    axes_hoffset = .06;
    
    for i=2:2:10
marwan's avatar
marwan committed
        set(h_axes.h(i-1),  'Units','normalize','Position',[0.1300    axes_base+(5-i/2)*(axes_height+axes_hoffset)    0.3270    axes_height])
        set(h_axes.h(i),'Units','normalize','Position',[0.5780    axes_base+(5-i/2)*(axes_height+axes_hoffset)    0.3270    axes_height])
    end
    if length(h_axes.h) > 10
        set(h_axes.h(11),  'Units','normalize','Position',[0.5780    axes_base+(5-2/2)*(axes_height+axes_hoffset)    0.3270    axes_height])
marwan's avatar
marwan committed
    end
    h_dlg=printdlg;
    waitfor(h_dlg)

    for i=1:10, set(h_axes.h(i),'Units','Character','Position',h_axes.old_pos{i}), set(h_axes.h(i),'Units','Norm'),end
marwan's avatar
marwan committed
    if length(h_axes.h) > 10
        set(h_axes.h(11),'Units','Character','Position',h_axes.old_pos{11}), set(h_axes.h(11),'Units','Norm')
    end
marwan's avatar
marwan committed
    set(h,'Visible','On')
    set(0,props.root)







%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% compute
case 'compute'

  errcode=11;
marwan's avatar
marwan committed
  if length(method)>1 & strcmpi(method(1:2),'di')
      disp('Warning: RQA from distance plot not possible!')
      return
  end
  
marwan's avatar
marwan committed
  if ~nogui
    h_fig=findobj('tag','crqa_Fig');
    setptr(gcf,'watch'), 
    obj=({'text';'crqa_m';'crqa_maxLag';'crqa_method';'crqa_eps';'crqa_lmin';'crqa_vmin';'crqa_theiler';'crqa_w';'crqa_ws';'crqa_button_store';'crqa_button_print';'crqa_button_close'});
    for j=1:length(obj); 
        h=findobj('Tag',obj{j},'Parent',h_fig(1)); 
        if ~isempty(h)
          set(h,'Enable','Off')
        end
    end
    h=findobj('tag','crqa_button_apply');
    set(h(1),'ToolTip','Stops the computation.','String','Stop','Callback','set(0,''ShowHidden'',''on'');h=findobj(''tag'',''crqa_button_apply'');set(h(1),''String'',''Stopped'');set(0,''ShowHidden'',''off'')')
  end

if Nx==w & wstep<2, wstep=1; Nx=w+1; end
if Nx==w, Nx=w+1; end
if nogui~=2, hw=waitbar(0,['0/',num2str(Nx-w)]);set(hw,'Name','Please Wait!');h1=get(hw,'chil');h1=get(h1,'title'); drawnow; end
if strcmpi(method,'Order Pattern') method = 'op'; end
if strcmpi(method,'Order Matrx') method = 'om'; end
if strcmpi(method,'Maximum Norm, fixed RR') method = 'rr'; end
marwan's avatar
marwan committed

marwan's avatar
marwan committed

% check if plugin exist and is executable
marwan's avatar
marwan committed
[plugin_exist, plugin_name, plugin_path] = is_crp_plugin;
if nogui == 1 & plugin_exist & ( method_n < 4 | method_n == 8 ) & length(x) == length(y)
marwan's avatar
marwan committed
    disp('(plugin used)')
end
marwan's avatar
marwan committed

errcode=20;
marwan's avatar
marwan committed
for i=1:wstep:Nx-w; 
     if ~nogui
marwan's avatar
marwan committed
         set(0,'ShowHidden','on')
         h=findobj('tag','crqa_button_apply','Parent',h_fig(1));
         if strcmpi(get(h(1),'string'),'stopped')
              Y(i:Nx-w,1:6)=NaN;
              break
         end
marwan's avatar
marwan committed
     end