function wigb (a,scal,x,z,amx)
%WIGB: Plot seismic data using wiggles
%
% WIGB(a,scal,x,z,amx)
%
% IN a: seismic data
% scale: multiple data by scale
% x: x-axis;
% z: vertical axis (time or depth)
% x and z are vectors with offset and time.
%
% If only 'a' is enter, 'scal,x,z,amn,amx' are decided automatically;
% otherwise, 'scal' is a scalar; 'x, z' are vectors for annotation in
% offset and time, amx are the amplitude range.
%
% Author:
% Xingong Li, Dec. 1995
% Changes:
% Jun11,1997: add amx
% May16,1997: updated for v5 - add 'zeros line' to background color
% May17,1996: if scal ==0, plot without scaling
% Aug6, 1996: if max(tr)==0, plot a line
if nargin == 0, nx=10;nz=10; a = rand(nz,nx)-0.5; end;
[nz,nx]=size(a);
trmx= max(abs(a)); %得到一个行向量,没一列是a中所在列的最大值
if (nargin <= 4); amx=mean(trmx); end;
if (nargin <= 2); x=[1:nx]; z=[1:nz]; end;
if (nargin <= 1); scal =1; end;
if nx <= 1; disp(' ERR:PlotWig: nx has to be more than 1');return;end;
% take the average as dx
dx1 = abs(x(2:nx)-x(1:nx-1));
dx = median(dx1);
dz=z(2)-z(1);
xmx=max(max(a)); xmn=min(min(a));
if scal == 0; scal=1; end;
a = a * dx /amx; %先对a进行归一化,之后根据x方向间距大小调整振幅显示
a = a * scal;
fprintf(' PlotWig: data range [%f, %f], plotted max %f \n',xmn,xmx,amx);
% set display range
x1=min(x)-2.0*dx; x2=max(x)+2.0*dx;
z1=min(z)-dz; z2=max(z)+dz;
set(gca,'NextPlot','add','Box','on', ...
'XLim', [x1 x2], ...
'YDir','reverse', ...
'YLim',[z1 z2]);
fillcolor = [0 0 0];
linecolor = [0 0 0];
linewidth = 0.1;
z=z'; % input as row vector
zstart=z(1);
zend =z(nz);
for i=1:nx,
if trmx(i) ~= 0; % trmx是对地震数据a的每一列的最大值,如果都为0的话。说明该道的所有值均为负值
tr=a(:,i); % --- 标记每一列
s = sign(tr) ; %sign函数是标记正负值,s的形状与tr形状一致,只不过返回值,正值为1,负值为-1,0返回为0
i1= find( s(1:nz-1) ~= s(2:nz) ); % 找到正负和零值变换的位置,
% find函数返回结果表示为符合条件的值所在的位置
npos = length(i1);
%太好了!!!!!!
zadd = i1 + tr(i1) ./ (tr(i1) - tr(i1+1)); %%锁定零点所在的位置
aadd = zeros(size(zadd)); %锁定零点所在的位置
[zpos,vpos] = find(tr >0); %找到当前列大于0的位置,vpos是固定的,为当前列,
% sort函数是对该向量进行重新排序,[zpos; zadd]是吧零点加入这列中,并进行重新排序
%sort函数返回值,zz是返回的从小到大的向量,iz是排序后每个量在以前向量中所在的位置
[zz,iz] = sort([zpos; zadd]);
aa = [tr(zpos); aadd];
%数据进行重新排序,赋值
aa = aa(iz);
% be careful at the ends
if tr(1)>0, a0=0; z0=1.00;
else, a0=0; z0=zadd(1);
end;
if tr(nz)>0, a1=0; z1=nz;
else, a1=0; z1=max(zadd);
end;
zz = [z0; zz; z1; z0];
aa = [a0; aa; a1; a0];
zzz = zstart + zz*dz -dz;
patch( aa+x(i) , zzz, fillcolor); %颜色填充
line( 'Color',[1 1 1],'EraseMode','background', ...
'Xdata', x(i)+[0 0], 'Ydata',[zstart zend]); % remove zero line
%'LineWidth',linewidth, ...
%12/7/97 'Xdata', x(i)+[0 0], 'Ydata',[z0 z1]*dz); % remove zero line
line( 'Color',linecolor,'EraseMode','background', ...
'LineWidth',linewidth, ...
'Xdata', tr+x(i), 'Ydata',z); % negatives line
else % zeros trace
line( 'Color',linecolor,'EraseMode','background', ...
'LineWidth',linewidth, ...
'Xdata', [x(i) x(i)], 'Ydata',[zstart zend]);
end;
end;