%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out1]=agc(in,np)
% "agc.m" program applied automatic gain to traces in "in"
% matrix, assuming the time axis is along columns and offset axis
% is along rows (i.e., in(row,column)). AGC applies a gain factor
% to each amplitude s(T) at time T so that the sqrt(seismic energy)
% within a np-long window centered about T is divided into s(T), i.e.,
% s(T)' = s(T)/energy(s(T-np/2)..s(T+np/2)
% AGC tends to make display of seismic traces easier to interpret.
%
[nx,nz]=size(in);
inn=[fliplr(in(:,1:np)) in fliplr(in(:,nz-np:nz))];
inn=inn.^2;
f=ones(np,1)/np;
out=conv2(inn,f');
start=round(np+np/2);endd=start+nz-1;
outt=out(:,start:endd);
out1=in./(sqrt(outt)+.000001);



