Home > wafo > onedim > dat2tc.m

dat2tc

PURPOSE ^

Extracts troughs and crests from data.

SYNOPSIS ^

[tc, ind, l_ind]=dat2tc(x,v,wdef)

DESCRIPTION ^

 DAT2TC Extracts troughs and crests from data.
 
  CALL:  [TC, tc_ind, v_ind] = dat2tc(x,v,wdef);
 
       TC     = a two-column matrix with the trough and crest turning
                 points, times in first column, values in second.
 
       tc_ind = indices to the trough and crest turningpoints 
         of the original sequence x.
 
       v_ind  = indices to the level v crossings of the original 
         sequence x. (d,u)
 
         x    = the surface elevation.
 
         v    = the reference level (default  v = mean of  x).
 
     wdef   = defines the type of wave. Possible options are
         'dw', 'uw', 'tw', 'cw' or 'none'. Default is 'none'.
         If wdef='none' all troughs and crests will be returned,
         otherwise only the paired ones will be returned
         according to the wavedefinition.
          
  Example:
    x = load('sea.dat'); x1 = x(1:200,:);
    tc = dat2tc(x1,0,'dw');
    plot(x1(:,1),x1(:,2),tc(:,1),tc(:,2),'ro',x1(:,1),zeros(1,200),':')
  
  See also  dat2crossind

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function [tc, ind, l_ind]=dat2tc(x,v,wdef)
002 %DAT2TC Extracts troughs and crests from data.
003 %
004 % CALL:  [TC, tc_ind, v_ind] = dat2tc(x,v,wdef);
005 %
006 %      TC     = a two-column matrix with the trough and crest turning
007 %                points, times in first column, values in second.
008 %
009 %      tc_ind = indices to the trough and crest turningpoints 
010 %        of the original sequence x.
011 %
012 %      v_ind  = indices to the level v crossings of the original 
013 %        sequence x. (d,u)
014 %
015 %        x    = the surface elevation.
016 %
017 %        v    = the reference level (default  v = mean of  x).
018 %
019 %    wdef   = defines the type of wave. Possible options are
020 %        'dw', 'uw', 'tw', 'cw' or 'none'. Default is 'none'.
021 %        If wdef='none' all troughs and crests will be returned,
022 %        otherwise only the paired ones will be returned
023 %        according to the wavedefinition.
024 %         
025 % Example:
026 %   x = load('sea.dat'); x1 = x(1:200,:);
027 %   tc = dat2tc(x1,0,'dw');
028 %   plot(x1(:,1),x1(:,2),tc(:,1),tc(:,2),'ro',x1(:,1),zeros(1,200),':')
029 % 
030 % See also  dat2crossind
031 
032 % Tested on: Matlab 6.0, 5.3, 5.2, 5.1
033 %
034 % History: 
035 % revised pab Feb2004  
036 % Revised by jr 02.04.2001
037 % - Added example, updated info. 
038 % Modified by Per A Brodtkorb 27.07.98
039 % Modified version of the old tp2tc routine which is significantly faster 
040 % if you use it to extract trough and crest turningpoints from the surface 
041 % elevation. If you use it to extract trough and crest turningpoints from a 
042 % sequence of turningpoints the gain in computational time is smaller. 
043 %
044 % However, this new version is more flexible. It is able to return the 
045 % indices to all the trough and crest turningpoints and 
046 % level v - crossings of the original sequence x or just those 
047 % turningpoints and crossings according to the wave definition. 
048 % (This is useful in a zero down cross analysis). 
049 
050 
051 error(nargchk(1,3,nargin))
052 
053 xn = x;
054 [n m]= size(xn);
055 if n<m
056  b=m;m=n;n=b; 
057  xn=xn';
058 end
059 
060 if n<2, 
061   error('The vector must have more than 2 elements!')
062 end
063 
064 istime=1;
065 
066 switch m
067  case 1, xn=[ (1:n)' xn(:)];istime=0;
068  case 2, % dimension OK!
069  otherwise, error('Wrong dimension of input! dim must be 2xN, 1xN, Nx2 or Nx1 ')          
070 end
071 
072 if ((nargin<3) | isempty(wdef)),
073   wdef='none';
074 end
075 
076 if ((nargin<2) | isempty(v)),
077   v=mean(xn(:,2));
078   disp(['   The level v is set to: ', num2str(v)])
079 end
080 
081 n = length(xn);
082 
083 % find level v down-crossings and up-crossings
084 % according to wavedefinition and number of level l crossings
085 [l_ind Nc] = dat2crossind(xn,v,wdef);
086 
087 if Nc<=2, 
088   disp('Warning: There are no waves!') 
089   disp('         Empty matrix returned.')
090   tc=[];
091   return
092 end
093 
094 % determine the number of trough2crest (or crest2trough) cycles
095 if mod(Nc,2),
096   Ntc=(Nc-1)/2;     % Nc is odd 
097 else
098   Ntc=(Nc-2)/2;     % Nc is even
099 end
100 
101 % allocate variables before the loop increases the speed
102 tc=zeros(Nc-1,2);
103 ind=tc(:,1);
104 
105 if (xn(l_ind(1),2)>xn(l_ind(1)+1,2)),%%%% the first is a down-crossing
106   for i=1:Ntc;, 
107     % trough 
108     [tc(2*i-1,2) ind(2*i-1)]=min(xn((l_ind(2*i-1)+1):l_ind(2*i),2)); 
109     % crest 
110     [tc(2*i,2) ind(2*i)]=max(xn((l_ind(2*i)+1):l_ind(2*i+1),2)); 
111   end  % for i=1:Ntc loop
112   
113   if (2*Ntc+1<Nc)&(strcmp(wdef,'none')|strcmp(wdef,'tw')), 
114     % trough 
115     [tc(Nc-1,2) ind(Nc-1)]=min(xn((l_ind(Nc-1)+1):l_ind(Nc),2));
116   end
117 else %%%% the first is a up-crossing
118   for i=1:Ntc;, 
119     % crest 
120     [tc(2*i-1,2) ind(2*i-1)]=max(xn((l_ind(2*i-1)+1):l_ind(2*i),2)); 
121     % trough 
122     [tc(2*i,2) ind(2*i)]=min(xn((l_ind(2*i)+1):l_ind(2*i+1),2)); 
123   end  % for i=1:Ntc loop
124   
125   if (2*Ntc+1<Nc)&(strcmp(wdef,'none')|strcmp(wdef,'cw')), 
126     % crest 
127     [tc(Nc-1,2) ind(Nc-1)]=max(xn((l_ind(Nc-1)+1):l_ind(Nc),2));
128   end
129 end
130                     
131 
132 ind=l_ind(1:(Nc-1))+ind;
133 tc(:,1)=xn(ind,1);
134 
135 %if (l_ind(Nc)+ind(Nc-1)<=n)&(wdef=='none'),
136 %    [tc(Nc,2) ind(Nc)]=max(xn((l_ind(Nc)+1):n,2)); % local max; probably 
137 %                                                    % a crest
138 %end
139 
140 
141 
142 
143 
144 
145

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