function yf = boxfilt(y, bl)
% function yf = boxfilt(y, bl)
% Apply a boxcar filter, first padding the ends of the series
% with constant values to reduce end effects.
% The filter length, bl, must be an odd integer.
% y must be a vector, either row or column.


if rem(bl,2) ~= 1,
   error('Boxcar length must be an odd integer');
end

[nrows, ncols] = size(y);

nr = max(nrows, ncols);

npad = (bl-1)/2;
box = ones(bl,1)/bl;

yf = conv([ones(npad,1)*y(1); y(:); ones(npad,1)*y(nr)], box);
yf = yf(bl:(nr+bl-1));

% If the input is a row vector, convert output to row also.
if nrows == 1, y = y';
elseif ncols ~= 1, error('input to boxfilt is a matrix');
end

