Home > wafo > onedim > covplot.m

covplot

PURPOSE ^

Plots the auto covariance function (ACF) 1D or 2D.

SYNOPSIS ^

covplot(rn,L,plotflag,lintype)

DESCRIPTION ^

 COVPLOT Plots the auto covariance function (ACF) 1D or 2D. 
 
  CALL:  covplot(r,L,plotflag,lintype)
 
          r =  covariance structure
   for one-dim:
          L = Maximum lag for which the ACF is plotted.
      plotflag = 1 then the ACF is plotted vs lag (sec or m) (default)
              2 then the ACF is plotted vs lag
              3 then the ACF is plotted vs lag and vs lag (sec or m)
   for two-dim:
          L = [Lx Lt] Maximum lag for which the ACF is plotted.
          (Not implemented!)
      plotflag = 1 then contour plot (default)
                 2 then mesh plot
                 3 one plot with R(x,0) and one with R(0,t)
 
    lintype : specify color and lintype, see PLOT for possibilities.
 
  Example:
    S1 = demospec; S2 = demospec('dir');
    R1 = spec2cov(S1); R2 = spec2cov(S2,0,30,2,100,[],2,[]);
    subplot(211), L1 = 17; covplot(R1,L1,1,'.-')
    subplot(212), covplot(R2)
  
  See also  dat2cov, dat2cor

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function covplot(rn,L,plotflag,lintype)
002 %COVPLOT Plots the auto covariance function (ACF) 1D or 2D. 
003 %
004 % CALL:  covplot(r,L,plotflag,lintype)
005 %
006 %         r =  covariance structure
007 %  for one-dim:
008 %         L = Maximum lag for which the ACF is plotted.
009 %     plotflag = 1 then the ACF is plotted vs lag (sec or m) (default)
010 %             2 then the ACF is plotted vs lag
011 %             3 then the ACF is plotted vs lag and vs lag (sec or m)
012 %  for two-dim:
013 %         L = [Lx Lt] Maximum lag for which the ACF is plotted.
014 %         (Not implemented!)
015 %     plotflag = 1 then contour plot (default)
016 %                2 then mesh plot
017 %                3 one plot with R(x,0) and one with R(0,t)
018 %
019 %   lintype : specify color and lintype, see PLOT for possibilities.
020 %
021 % Example:
022 %   S1 = demospec; S2 = demospec('dir');
023 %   R1 = spec2cov(S1); R2 = spec2cov(S2,0,30,2,100,[],2,[]);
024 %   subplot(211), L1 = 17; covplot(R1,L1,1,'.-')
025 %   subplot(212), covplot(R2)
026 % 
027 % See also  dat2cov, dat2cor
028 
029 % Tested on: matlab 6.0, 5.3, 5.2, 5.1
030 % History:
031 % revised by jr 02.04.2000 
032 % revised by es 24.01.2000 various small things for two-dim  
033 % revised by es 30.09.1999 (two-dim), 13.10.1999 (ishold and linetype)
034 % by pab 11.08.98
035 
036 if ndims(rn.R)>2
037   error('Can only handle one- and two dimensional ACF')
038 end
039 if (nargin <3 | isempty(plotflag))
040   plotflag=1;
041 end
042 if nargin<4|isempty(lintype)
043   lintype='b-';
044 end
045 if ~isfield(rn,'norm')
046   rn.norm=0;
047 end
048 
049 n=length(rn.R);
050 
051 if prod(size(rn.R))==n, % one-dim ---------------------------------
052   ih=ishold;
053 
054   if n<2, 
055     error('The vector must have more than 2 elements!')
056   end
057   
058   if (nargin <2 | isempty(L)),
059     L=n-1;
060   else
061     L=min(L,n-1);
062   end
063   names=fieldnames(rn);
064   ind=find(strcmp(names,'x')+strcmp(names,'t')); %options are 't' and 'x'
065   vari=lower(names{ind});
066   t=getfield(rn,vari);  %t is either R.t or R.x
067   dT=t(2)-t(1);
068   if strcmpi(vari,'t')
069     unit=' (sec)';
070   else
071     unit=' (m)';
072   end
073   
074   if isfield(rn,'stdev') & ~isempty(rn.stdev),
075     Stdev=rn.stdev(1:(L+1));
076     size(Stdev)
077   else
078     Stdev=[];
079   end
080   
081   
082   if (dT==1 ), % sampling frequency 1 Hz
083     plotflag=1; % sufficient to make one plot
084   end  
085   
086   if (plotflag == 3), subplot(211),  end
087   
088   r = rn.R(1:(L+1));
089   tau =  (0:L) ; % # of lags
090   mx=max(abs(r));
091   if (plotflag == 2) |(plotflag == 3)
092     
093     plot(tau,r,lintype) , hold on
094     plot([tau(1) tau(L+1)],[0 0],'k-')
095     if  ~isempty(Stdev),
096       plot(tau,2*Stdev,'r--')
097       plot(tau,-2*Stdev,'r--')
098     end ,if ~ih, hold off, end
099     if dT==1,
100       xlabel(['Lag Lag',unit])
101     else
102       xlabel('Lag')
103     end
104     ylabel('ACF')
105     grid on
106     if ih, a=axis; else a=zeros(1,4); end
107     axis([0 max(tau(L+1),a(2)) min(-1.01*mx,a(3)) max(1.01*mx,a(4)) ])
108     if rn.norm,
109       title('Auto Correlation Function (ACF)' )
110     else
111       title('Auto Covariance Function (ACF)' )
112     end
113   end
114   
115   if (plotflag == 3) 
116     subplot(212)
117   end
118 
119   if (plotflag == 1) |(plotflag == 3)
120     tau = tau*dT ;
121     plot(tau,r,lintype),hold on
122     plot([tau(1) tau(L+1)],[0 0],'k-')
123     if  ~isempty(Stdev),    
124       plot(tau,2*Stdev,'r--')
125       plot(tau,-2*Stdev,'r--')
126     end, if ~ih,hold off,end
127     xlabel(['Lag',unit])
128     ylabel('ACF')
129     grid on
130     if ih, a=axis; else a=zeros(1,4); end
131     axis([0 max(tau(L+1),a(2)) min(-1.01*mx,a(3)) max(1.01*mx,a(4)) ])
132     if rn.norm,
133       title('Auto Correlation Function (ACF)' )
134     else
135       title('Auto Covariance Function (ACF)' )
136     end
137   end
138   subplot(111)
139   if ih, hold on, end
140 else % two-dim ----------------------------------------------------
141 
142   [nx,nt]=size(rn.R);
143   if (nargin <2 | isempty(L)),
144     L=[nx-1, nt-1];
145   elseif length(L)==1
146     L=[min(L,nx-1) min(L,nt-1)];
147   else
148     L=[min(L(1),nx-1) min(L(2),nt-1)];
149   end
150   
151   names=fieldnames(rn);
152   ind=find(strcmp(names,'x')+strcmp(names,'t')+strcmpi(names,'y'));
153   %options are 'x' and 't' and 'y'
154   vari1=names{ind(1)}; % always 'x' or 'y' 
155   vari2=names{ind(2)}; % always 'y' or 't'
156   if strcmpi(vari2,'t')
157     unit=' [sec]';
158   else
159     unit=' [m]';
160   end
161   x=eval(['rn.',vari1]);
162   t=eval(['rn.',vari2]);
163   if strcmp(vari2,'y')
164     rn.R=rn.R'; % otherwise sizes does not match
165   end
166   if plotflag == 1
167     if strcmp(rn.type,'polar')
168       if rn.y(1)>=0 % exploit symmetry
169     c=contour([-rn.y(end:-1:2);rn.y],rn.x,[rn.R(:,end:-1:2) rn.R]);
170       else
171     c=contours(rn.y,rn.x,rn.R);
172       end
173       limit = size(c,2);
174       ix = 1;
175       while(ix < limit)
176     z_level(ix) = c(1,ix);
177     npoints = c(2,ix);
178     nexti = ix+npoints+1;
179     c(:,ix)=NaN;
180     ix = nexti;
181       end
182       polar(c(1,:),c(2,:),'b');
183     else     
184       contour(x,t,rn.R',lintype)
185       xlabel([vari1 ' [m]'])
186       ylabel([vari2 unit])
187     end
188     if rn.norm,
189       title('Auto Correlation Function (ACF)' )
190     else
191       title('Auto Covariance Function (ACF)' )
192     end
193   elseif plotflag==2
194     surf(x,t,rn.R')
195     shading interp
196     if rn.norm,
197       title('Auto Correlation Function (ACF)' )
198     else
199       title('Auto Covariance Function (ACF)' )
200     end
201     xlabel([vari1 ' [m]'])
202     ylabel([vari2 unit])
203   else
204     subplot(211)
205     plot(x,rn.R(:,t==0),lintype)
206     ih=ishold;
207     hold on
208     plot([x(1) x(end)],[0 0],':')
209     if ~ih, hold off, end
210     if rn.norm,
211       title('Auto Correlation Function (ACF)' )
212     else
213       title('Auto Covariance Function (ACF)' )
214     end
215     xlabel([vari1 ' (m)'])
216     subplot(212)
217     plot(t,rn.R(x==0,:),lintype)
218     ih=ishold;
219     hold on
220     plot([t(1) t(end)],[0 0],':')
221     if ~ih, hold off, end
222     if rn.norm,
223       title('Auto Correlation Function (ACF)' )
224     else
225       title('Auto Covariance Function (ACF)' )
226     end
227     xlabel([vari2 unit])
228   end
229 end
230

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