function b = smooth(a, n);

%  function b = smooth(a, n);
%
%  Takes array 'a' and convolves it with a rectangular window of length n.
%  NOTE:  n should be odd.  If n is negative, use a square function, if
%  positive use a Hamming window.
%
%  Returns a vector the same length as a...

n = round(n);  % Make sure n an integer...

if abs(n) > 1  
    
    if n < -1
        n = -n;
        b = conv(a, ones([1 n]))/n;
    else
        w = 0.54 - 0.46 * cos(2 * pi() * (1:n)/(n-1));
        b = conv(a, w)/sum(w);
    end;
    
    i1 = 1 + floor((n-1)/2 + 0.0002);
    
    i2 = i1 + length(a) - 1;
    
    b = b(i1:i2);
else
    b = a;
end

return;
