function f = correlate(v1, v2)
%CORRELATE Calculate the correlation f of two vectors, v1 and v2, of equal length


n=length(v1);

% dimension of f = 2n-1.
for sn=-(n-1):(n-1),

% shift vector
  for j=1:n,
    if (j-sn) <= 0
      vtmp(j)=0;
    elseif (j-sn) > n
      vtmp(j)=0;
    else
      vtmp(j)=v2(j-sn);
    end
  end
    
%calculate vector index (1 to 2n+1)
  id=(n-1)+sn+1;
  f(id)=dot(v1,vtmp');  % dot product
end
