function out = shift(x,n)
%
% out = shift(x,n)
%
% shifts a circulant buffer by n points: out=x(:+n)
%

% written by D.Long at BYU June 2001

% create index array of shifts to handle 
m=length(x);
l=[1:m]+mod(round(n),m);
ind=find(l<1);
if ~isempty(ind)
  l(ind)=l(ind)+m;
  ind=find(l<1);
  if ~isempty(ind)
    l(ind)=l(ind)+m;
  end
end
ind=find(l>m);
if ~isempty(ind)
  l(ind)=l(ind)-m;
  ind=find(l>m);
  if ~isempty(ind)
    l(ind)=l(ind)-m;
  end
end

% pre allocate output array so it has same shape as input
out=zeros(size(x));

% copy input into output array using shifted indexes
out(l)=x;
