# What does "Lag" mean in terms of Cross Correlation?

I am working on a program using the Essentia library for audio analysis. I've implemented the Cross Correlation algorithm (here: http://essentia.upf.edu/documentation/reference/std_CrossCorrelation.html), but I'm not sure if I really understand the output. Sadly, barely any of the description on that page makes sense to me. I do not have a math background, and only a cursory understanding of frequency analysis / signal processing.

So... can anyone explain what the optional parameters (minLag, maxLag) do here?

using the simplest definition of the cross-correlation:

$$R_{xy}[k] \triangleq \sum_n x[n]\,y[n+k]$$

the "lag" is the displacement $k$. (i am being deliberately vague about the limits to the summation.) in the correlation $x[n]$ is lagging behind $y[n]$ by $k$ sample periods.

• If I am to compare or measure similarity between 2 signals one input and another output signal vector, by using cross-correlation function mentioned by @livingtech above, with x and y number of samples in them, what should be the ideal value of maxLag and minLag I must choose (in the cross-correlation vector between the two input arrays (its size is equal to maxLag - minLag + 1)) that I get a good result? Oct 1 '20 at 10:55
• i can't quite decode the question here. Oct 1 '20 at 16:20
• Sorry if I made it ambiguous. So, if k samples are being displaced, and if Rxy[k] is the correlation between signals x[n] and y[n], what should be the range of values of k above, if x[n] has P samples and y[n] has Q samples, where P and Q may or may not be equal? Like if we have an input audio signal with say 64000 samples and the output has 72000 samples, what would be the ideal range of lag or displacement variable k (i.e, from the question above, the values of minLag and the maxLag)? Oct 1 '20 at 21:18
• i think, to make your question make some sense, you have to zero-pad both $x[n[$ and $y[n]$ out to $\pm \infty$. so both of them are infinitely long, the summation is ostensibly infinitely long, but the number of non-zero terms is finite and gets smaller as $|k|$ gets larger. Oct 1 '20 at 21:24
• That's what Matlab does I figured. The above library requires it to be done explicitly. So, in order to get the best results P and Q should be equal, right? Oct 16 '20 at 9:41

Hi: I don't use that software but, generally speaking, when you calculate the cross correlation between two series you are calculating the correlations between the two series at different lags of the two series. So, say the lag is 3. Then, that means that, the computation takes the 2 sets of data points where x is 3 lags ahead of y ( or the opposite, depending on the convention. Note also that cross-correlation is not symmetric so you probably are allowed negative lags) and calculates the correlation between these 2 sets of points. It then does this for all the lags and the output is a plot of the lag versus the correlation. So, by specifying minlag and maxlag, you are telling the function how many different lags you want to consider. So, if minlag is 3 and maxlag is 8, then I assume the function calculate the correlations for all the lags from 3-8. Obviously, someone who uses that software can give you the specifics for it but that's generally how cross-correlation works. I hope this helps.

• Hi robert: In econometrics, statistics etc, the summation for the autocorrelation goes from zero to N-1, assuming that there are N data points and that z[0] denotes the first one. OTOH, with cross-correlation, where one has x and y which can be of different sizes ( but hopefully start at the same place ), one would use the double sum in to make sure to not hit places where x or y were not there. No idea what the DSP convention is. Dec 30 '17 at 8:11
• oh, mark, there can be many definitions. it depends on whether the expression of the auto/cross correlation is with code going into a computer or equations written on paper or a white board. and it depends on whether you are dealing with finite energy signals or finite power signals. limits can go negative. limits might be a function of the lag $k$ or might not. "it all depends..." Dec 31 '17 at 7:26