% shifting matrix: matrix vector multiplication
%
% (c) by Hannes Nickisch, MPI for Biological Cybernetics, 2010 November 30

function y = mvm(A,x,ctransp)

  si = floor(A.shift);                                     % major integer shift
  sf = A.shift-si;                            % remaining fractional shift [0,1]
  y = circshift(reshape(x,A.imsz),(-1)^ctransp*si);              % integer shift
  if norm(sf)>sqrt(eps)                              % subpixel fractional shift
    z = y; y = 0; D = numel(sf);
    for I=0:2^D-1                                      % iterate over 2^D shifts
      id = zeros(1,D); i = I; a = 1;
      for d=1:D, id(D-d+1) = mod(i,2); i = fix(i/2); end
      for d=1:D, if id(d), a = a*sf(d); else a = a*(1-sf(d)); end, end
      y = y + a * circshift(z,(-1)^ctransp*id);
    end
  end