function y = runaver(x, weights)
%
%   The function y = runaver(x, weights) computes the running average
%   of the vector x and stores it in the vector y.  In other words, 
%   the resulting vector y is a low-passed filter of the vector x, and
%   the length of the vector y is smaller of the length of x by the 
%   width of the window (weights).  In order to run this routine, one
%   has to first have computed the weights of the sampling window and
%   stored them in the vector 'weights'.
%
%---------------------------------------------------------------------
%   Written by Vassilis Zervakis, while at
%              AINCO-Interocean, Madrid, Spain, in December 1995.
%---------------------------------------------------------------------
%
    nn = length(x);
    mm = length(weights);
    istart = round(mm/2.);
    iend   = fix(nn - mm/2.);
    for i = istart:iend,
       y(i) = 0;
       for j = 1 : mm,
          y(i) = y(i) + weights(j) * x(i - istart + j);
       end
       y(i) = y(i) / sum(weights);
    end
% ---fill in the beginning ---------
    for i = 1 : istart,
        y(i) = y(istart);
    end
% ---fill in the end  --------------
    for i = iend : nn,
        y(i) = y(iend);
    end
