xcorr in MATLAB for periodic function
The xcorr function assumes a linear cross-correlation, i.e. it assumes that the signals are zero outside the intervals. If you want to have an efficient implementation of the periodic cross-correlation, you can refer to the properties of the Fourier transform with

$$
\mathcal{F}(u \star y)=\mathcal{F}(u) \cdot \mathcal{F}(y)^*, 
$$

while exploiting the fact, that the DFT considers signals to be periodic anyway. Here's the corresponding code for that:

N = 20;

u = randn(N,1);
y = randn(N,1);

R = intcor(u, y);

R2 = real(ifft(fft(u) .* conj(fft(y))))/(N);  % Calculate the periodic CC

hold off;
plot([u y R])
hold on;
plot(R2, 'ko');

legend({'u', 'y', 'intcor', 'fft-based'});




To answer your questions in the comments:


It is still not clear to me, if I can use xcorr to calculate the autocorrelation of a periodic signal correctly? -
No, you cannot use the xcorr function, as it assumes a linear cross-correlation. What you want is a calculation, where you assume the signal is periodic, and you just input one period of the signal.
What is the difference between linear cross-correlation and periodic cross-corellation? - Consider the general equation for cross-correlation:
$$
R_uy(h)=\sum_{k=-\infty}^{\infty}u(k)^*y(k-h)
$$


Normally, the summation is done over all times (i.e from $-\infty$ to $\infty$). However, the signals you pass to the xcorr-function are time-limited (i.e. they consist only of $N$ non-zero samples). Now, in the linear case, the cross-correlation assumes that the input-sequences vanish for $k&lt;0$ or $k&gt;N-1$. In the circular-correlation (i.e. assuming periodic signals), the signals are not assumed to be zero for the index above, but instead we assume $u(k+iN)=u(k), \forall i\in\mathbb{Z}$. I.e., the sequences are assumed to be periodic with period N.