# xcorr in MATLAB for periodic function

I have a periodic signal and I want to find it's autocorrelation function. I can calculate it exactly:

$$R_{uu}(h) = \frac 1M \sum_{k=0}^{M-1} u(k)\cdot u(k-h)$$

But will xcorr() use this function for periodic signals?

BTW, I've created my own function for periodic signals:

function [R h] = intcor(u,y)
%Calculates the correlation between two vectors u, y
M=length(u);
h=0:1:M-1;
R=zeros(M,1);
for i=1:M
for k=1:M
if k-h(i) <= 0
R(i)=R(i)+u(k)*y(k-h(i)+M); %since periodic, add M -> gets same result
else
R(i)=R(i)+u(k)*y(k-h(i));
end
end
R(i)=R(i)/M;
end
end

• What do the Matlab docs say about that function? – MBaz Jan 13 '17 at 22:25
• @MBaz Here you go: ch.mathworks.com/help/signal/ref/xcorr.html – james Jan 14 '17 at 7:32
• Does not explain the periodic case. – james Jan 14 '17 at 7:32

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'});


1. 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.
2. 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<0$ or $k>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.