1
$\begingroup$

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 
$\endgroup$
3
  • 2
    $\begingroup$ What do the Matlab docs say about that function? $\endgroup$ – MBaz Jan 13 '17 at 22:25
  • $\begingroup$ @MBaz Here you go: ch.mathworks.com/help/signal/ref/xcorr.html $\endgroup$ – james Jan 14 '17 at 7:32
  • $\begingroup$ Does not explain the periodic case. $\endgroup$ – james Jan 14 '17 at 7:32
2
$\begingroup$

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

enter image description here

To answer your questions in the comments:

  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.

$\endgroup$
5
  • $\begingroup$ First of all, thank you very much for your answer. I have still some questions to ask: $\endgroup$ – james Jan 14 '17 at 13:41
  • $\begingroup$ 1. It is still not clear to me, if I can use xcorr to calculate the autocorrelation of a periodic signal correctly ? $\endgroup$ – james Jan 14 '17 at 13:42
  • $\begingroup$ 2. What is the difference between linear cross-correlation and periodic cross-corellation ? $\endgroup$ – james Jan 14 '17 at 13:43
  • $\begingroup$ Thanks a lot for the edit of your answer ! It is no clear to me ! :) :) $\endgroup$ – james Jan 14 '17 at 13:57
  • $\begingroup$ I wanted to type: now * $\endgroup$ – james Jan 14 '17 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.