function wigb(varargin)
% plot waveforms
%
% usage: wigb(S)
%    or: wigb(S,mag)
%    or: wigb(S,mag,cp)
%    or: wigb(S,mag,cp,cn)
%    or: wigb(S,mag,cp,cn,dx,dt,x0,t0)
%
% input:
%       S: waveform data (row=time, column=trace)
%       mag: magnification
%       cp: color for positive values
%       cn: color for negative values
%
% output:
%       void
%
% code by: Ruiqing He, University of Utah, 2003
switch nargin
case 0, 
    S=rand(100,10); mag=1; cp='r'; cn='g';dt=1;dx=1;x0=0;t0=0;
case 1,
    S=varargin{1}; mag=1; cp='r'; cn='g';dt=1;dx=1;x0=0;t0=0;
case 2,
    S=varargin{1}; mag=varargin{2}; cp='r'; cn='g';dt=1;dx=1;x0=0;t0=0;
case 3;
    S=varargin{1}; mag=varargin{2}; cp=varargin{3}; cn='g';dt=1;dx=1;x0=0;t0=0;
case 4;
    S=varargin{1}; mag=varargin{2}; cp=varargin{3}; cn=varargin{4};dt=1;dx=1;x0=0;t0=0;
otherwise;
    S=varargin{1}; mag=varargin{2}; cp=varargin{3}; cn=varargin{4};dx=varargin{5};dt=varargin{6};
    x0=varargin{7};t0=varargin{8};
end;

[nz,nx]=size(S);
M=mean(S);
for i=1:nx
    s=S(:,i)-M(i); 
    s=s*mag/max(abs(s));    
    f=sign(s);
    ind=find(f(1:nz-1).*f(2:nz)<0);    
    f=length(ind);
    if f>0
        % interpolate zeros
        X=zeros(1,nz+f+2); Y=X;
        X(1)=0; Y(1)=1;
        p2=ind(1);
        X(2:p2+1)=s(1:p2); Y(2:p2+1)=[1:p2]; 
        p=p2+3;
        X(p-1)=0; Y(p-1)=p2+abs(s(p2)/(s(p2)-s(p2+1)));
        for j=2:f
            p1=ind(j-1);
            p2=ind(j);            
            X(p:p+p2-p1-1)=s(p1+1:p2); Y(p:p+p2-p1-1)=[p1+1:p2];
            p=p+p2-p1+1;
            X(p-1)=0; Y(p-1)=p2+abs(s(p2)/(s(p2)-s(p2+1)));
        end
        X(p:nz+f+1)=s(p2+1:nz); Y(p:nz+f+1)=[p2+1:nz];
        X(nz+f+2)=0; Y(nz+f+2)=nz;
        % patch
        p1=(X>=0); p2=(X<=0);
        patch((X(p1)+i)*dx+x0,(Y(p1)-1)*dt+t0,cp); patch((X(p2)+i)*dx+x0,(Y(p2)-1)*dt+t0,cn);
        % delete zero line
        X0=ones(1,nz+f+2)*i; 
        line( 'Color','w','Xdata',X0*dx+x0,'Ydata',(Y-1)*dt+t0);
        % compensate zero line
        line((s+i)*dx+x0,[0:nz-1]*dt+t0,'color','k');
    end   
end
axis ij;
if nargin > 8;
   xsh1=varargin{9};xsh2=varargin{10};tsh1=varargin{11};tsh2=varargin{12};
   str_labelx=varargin{13};str_labely=varargin{14};str_title=varargin{15};
   axis([xsh1 xsh2 tsh1 tsh2]);xlabel(str_labelx,'FontSize',16);ylabel(str_labely,'FontSize',16);title(str_title,'FontSize',16);
   box on;
end;
