% zero padding matrix in the Fourier domain: matrix vector multiplication
%
% (c) by Hannes Nickisch, MPI for Biological Cybernetics, 2010 December 6

function y = mvm(A,x,ctransp)

  even = 1-mod(A.imsz,2); D = numel(A.imsz);
  if ctransp
    y = reshape(x,A.imsz+2*A.pad);
    for d=1:D
      id = repmat({':'}, 1, D); 
      id{d} = [1:even(d), A.pad(d)+(even(d)+1:A.imsz(d))];
      y = y(id{:});
    end
  else
    y = reshape(x,A.imsz);
    for d=1:D
      sz = size(y); sz(d) = A.pad(d); z = zeros(sz);
      id1 = repmat({':'}, 1, D); id1{d} = 1:even(d);
      id2 = repmat({':'}, 1, D); id2{d} = even(d)+1:size(y,d);
      y = cat(d, y(id1{:}), z, y(id2{:}), z);
    end
  end
