function smth=rmn(dat,len,rep)

% Function SMTH = RMN(dat,len,rep)
%   Creates a rectangular filter of length <len>, then smooths
%   <dat> by convolution <rep> times. If the total equivalent
%   filter length is n, then the (n-1)/2 points at the start
%   and end of the series are lost in the filtrand (smth) and
%   are replaced by NaN's.
%
%     EXAMPLE: If <dat> is monthly data, smth=rmn(dat,6,3)
%     will return a triple six-month running mean version
%     of the time series.
%
% WARNING - END POINT WRONG IF N EVEN (noticable if N small)
%

n=length(dat);m=ceil(len/2);mm1=m-1;
tmp=dat;filt=ones(len,1)/len;smth=NaN*ones(n,1);
for r=1:rep
  tmp=conv(filt,tmp);nn=length(tmp);
  tmp=tmp(m:nn-mm1);
end
n1=rep*mm1+1;n2=n-rep*mm1;smth(n1:n2)=tmp(n1:n2);
