Home > wafo > onedim > dat2crossind.m

dat2crossind

PURPOSE ^

Finds indices to level v down and/or upcrossings from data

SYNOPSIS ^

[ind , Nc]= dat2crossind(x,v,wdef)

DESCRIPTION ^

 DAT2CROSSIND Finds indices to level v down and/or upcrossings from data
 
   CALL:  [ind, Nc]= dat2crossind(x,v,wdef/cdef);
 
  ind  = indices to the level v crossings of the original sequence x.
  Nc   = number of crossings (i.e.length of ind). 
   x   = the surface elevation data.
   v   = the reference level (default  v = mean of  x).
  wdef = defines the type of wave. Possible options are
         'dw', 'uw', 'cw', 'tw' or 'none'. (Default 'none').
         If wdef='none' all crossings will be returned,
         otherwise only the crossings which defines a 
         wave according to the wave definition will be returned.
  cdef = defines the type crossings returned. Possible options are
         'd' 'u' or 'all'. (Default 'all').
         If def='d' all down-crossings will be returned.
         Similarly if def='u' only the up-crossings will be returned
         otherwise 'all' the crossings will be returned.
 
  Example: 
    t = linspace(0,7*pi,250); 
    x = sin(t);
    [ind, Nc] = dat2crossind(x,0.75,'u')
    plot(t,x,'.',t(ind),x(ind),'o')  
 
  See also  findcross, wavedef, crossdef

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function [ind , Nc]= dat2crossind(x,v,wdef)
002 %DAT2CROSSIND Finds indices to level v down and/or upcrossings from data
003 %
004 %  CALL:  [ind, Nc]= dat2crossind(x,v,wdef/cdef);
005 %
006 % ind  = indices to the level v crossings of the original sequence x.
007 % Nc   = number of crossings (i.e.length of ind). 
008 %  x   = the surface elevation data.
009 %  v   = the reference level (default  v = mean of  x).
010 % wdef = defines the type of wave. Possible options are
011 %        'dw', 'uw', 'cw', 'tw' or 'none'. (Default 'none').
012 %        If wdef='none' all crossings will be returned,
013 %        otherwise only the crossings which defines a 
014 %        wave according to the wave definition will be returned.
015 % cdef = defines the type crossings returned. Possible options are
016 %        'd' 'u' or 'all'. (Default 'all').
017 %        If def='d' all down-crossings will be returned.
018 %        Similarly if def='u' only the up-crossings will be returned
019 %        otherwise 'all' the crossings will be returned.
020 %
021 % Example: 
022 %   t = linspace(0,7*pi,250); 
023 %   x = sin(t);
024 %   [ind, Nc] = dat2crossind(x,0.75,'u')
025 %   plot(t,x,'.',t(ind),x(ind),'o')  
026 %
027 % See also  findcross, wavedef, crossdef
028 
029 %tested on: Matlab 6.0, 5.3, 5.2, 5.1
030 % History:
031 % revised pab Feb2004
032 % revised by pab 12.06.2001
033 %  -added check on ind returned from findcross.
034 % Revised by jr 02.04.2001
035 % - Added example, updated help 
036 % By Per A. Brodtkorb 07.07.1998,  27.07.1998,  
037 
038 error(nargchk(1,3,nargin))
039 xn=x;
040 
041 [n m]= size(xn);
042 if n<m
043  b=m;m=n;n=b; 
044  xn=xn';
045 end
046 
047 if n<2, 
048   error('The vector must have more than 2 elements!')
049 end
050 
051 istime=1;
052 
053 switch m
054  case 1, istime=0;
055  case 2, xn= xn(:,2);% dimension OK!
056  otherwise, error('Wrong dimension of input! dim must be 2xN, 1xN, Nx2 or Nx1 ')          
057 end
058 
059 if ((nargin<3) | isempty(wdef)),
060   wdef='none';
061 end
062 
063 if ((nargin<2) | isempty(v)),
064   v = mean(xn);
065   disp(['   The level v is set to: ', num2str(v)])
066 end
067 
068 
069 % find level v down-crossings and/or up-crossings
070 % according to wdef or cdef
071 ind = findcross(xn,v); % faster than find
072 
073 if isempty(ind), %added pab 12.06.2001
074    Nc = 0; 
075    txt = sprintf('No level v = %0.5g crossings found in x',v)   
076    warning(txt)
077    return,
078 end
079 
080 
081 switch wdef   % switch wdef/cdef    
082   case 'd', %downcrossings only
083     if xn(ind(1)+1)>v,
084       ind =ind(2:2:end);
085     else
086       ind =ind(1:2:end);
087     end
088     
089  case 'u',%upcrossings  only
090    if xn(ind(1)+1)<v,
091       ind =ind(2:2:end);
092     else
093       ind =ind(1:2:end);
094     end
095     
096   case {'dw','uw'},
097     % make sure that the first is a level v down-crossing if wdef == 'dw'
098     % or make sure that the first is a level v up-crossing if wdef == 'uw'
099 
100     if xor(((xn(ind(1))>xn(ind(1)+1))),strcmp(wdef,'dw')),
101       ind(1)=[];
102     end
103     Nc=length(ind); % number of level v crossings
104     % make sure the number of troughs and crests are according to the
105     % wavedef, i.e., make sure length(ind) is odd
106     if ~(mod(Nc,2)), % if Nc is even do
107       ind(end)=[];
108     end    
109   case {'tw','cw'},
110     % make sure that the first is a level v down-crossing if wdef == 'tw'
111     % or make sure that the first is a level v up-crossing if wdef == 'cw'
112 
113     if xor(((xn(ind(1))>xn(ind(1)+1))),strcmp(wdef,'tw')),
114       ind(1)=[];
115     end
116     Nc=length(ind); % number of level v crossings
117     % make sure the number of troughs and crests are according to the
118     % wavedef, i.e., make sure length(ind) is even
119     if (mod(Nc,2)), % if Nc is odd do
120       ind(end)=[];
121     end    
122   case {'du','all','none'},
123     % do nothing
124  otherwise,  error('Unknown wave/crossing definition!')
125 end
126 if nargout>1,
127   Nc=length(ind); % number of level v crossings
128 end
129 
130 return
131 
132 
133 
134

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group


Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003