function y = wmavg(x,lag,lead,wgt);
%WMAVG       is a weighted two-sided moving average.
%            y = mavg(x,lag,lead,wgt) computes:
%
%            y[t] = w[-lag]*x[t-lag]+..w[-1]*x[t-1]+w[0]*x[t]
%                       +w[1]*x[t+1]+...+w[lead]*x[t+lead]
%            
%            where wgt=[w[-lag],...w[-1],w[0],w[1],...,w[lead]]'.
%            For the initial and end points, use available observations,
%            e.g.,  y[1] = (w[0]*x[1]+...+w[lead]*x[1+lead])/(sum(w[0:lead]))
% 
%              
 
%            Ellen McGrattan, 9-8-00

n    = length(x);

for t=1:n;
  i      = max(1,t-lag);
  j      = min(n,t+lead);
  k      = max(1,lag+2-t);
  l      = min(lag+1+n-t,lag+1+lead);
  w      = wgt(k:l)/sum(wgt(k:l));
  y(t,1) = sum(w.*x(i:j));
end;
