title: shift the elements of a vector by non-integer shift in matlab
body: <p>I want to shift a vector by non-integer shift, linear interpolation seems to be not very accurate so I'm trying to use <em>sinc</em> interpolation by the following code which uses Fourier transform.</p>
  
  <pre><code>function y = fshift(x,s)
  % FSHIFT Fractional circular shift
  %   Syntax:
  %
  %       &gt;&gt; y = fshift(x,s)
  %
  %   FSHIFT circularly shifts the elements of vector x by a (possibly
  %   non-integer) number of elements s. FSHIFT works by applying a linear
  %   phase in the spectrum domain and is equivalent to CIRCSHIFT for integer
  %   values of argument s (to machine precision).
  
  
  needtr = 0; if size(x,1) == 1; x = x(:); needtr = 1; end;
  N = size(x,1); 
  r = floor(N/2)+1; f = ((1:N)-r)/(N/2); 
  p = exp(-j*s*pi*f)'; 
  y = ifft(fft(x).*ifftshift(p)); if isreal(x); y = real(y); end;
  if needtr; y = y.'; end;
  </code></pre>
  
  <p>there is no problem in the code when i shift a square wave by integer shift but when the shift is non-integer the output suffers from significant fluctuations
  i.e.,</p>
  
  <pre><code>s=[zeros(1,20) ones(1,20) zeros(1,20)];
  b=fshift(s,3.5);
  stem(b)
  </code></pre>
  
  <p>How to overcome this problem and is there any other accurate method?</p>