# Is xcov()More Accurate Than xcorr() to Compute the Cross Correlation of Signals with Non Zero Mean Values?

I have to compute the cross-correlation between two transient signals, which could be decomposed into a trend (with non-zero mean value) + noise.

In particular I am interested in the max of the cross-correlation.

It seems that in MATLAB the cross-correlation is more accurate if it is calculated using the function xcov() rather then xcorr(), probably as a consequence of the fact that the function xcov() removes the mean value of the signal.

An example of two signals between which I would have to measure similarity can be this one: Do you know if this makes sense and why?

• covariance and correlation are simply two different things – if you want one, then you don't want the other and vice versa. What is it that you need this for? That defines which you want. – Marcus Müller Nov 19 '20 at 23:11
• I'd like to compute the max of the cross-correlation as a measure of similarity between the two signals – EmThorns Nov 20 '20 at 0:17
• then, the correlation is indeed what you want, otherwise high-variance signals have a high chance of showing an exceedingly large metric – Marcus Müller Nov 20 '20 at 9:19
• I agree. But do I have to remove the mean value to optimize the MATLAB routine? If I don't do that, I have a case in which the max of the cross-correlation of two slightly different signals (but not identical) computed with xcorr is higher then the max value of the auto-correlation (so in this case the two signals are identical), still computed using xcorr,. This doesn't make sense to me. If I remove the mean value, this doesn't happen. Does this depend on the MATLAB algorithm? – EmThorns Nov 20 '20 at 13:14

When trying to measure similarity between signals we're basically building a metric.
When doing so we need to ask what we want to be sensitive about.

For instance, if you don't remove the DC Component (The Mean) and use something like an integral to measure similarity (Correlation / Convolution based) then you are sensitive the added DC component.

For instance, take 2 random white noises and add to each a DC component of 1e6.
Measure their similarity by correlation and compare it to 2 Sine Waves with amplitude of 1e-3 and a relative shift phase of 1e-6 [Rad].
In your eyes, which should be more similar? Which is indeed?

I'd say that in most cases in signal processing, similarity should be measured ignoring the DC component.

• In my case, I don't have an actual DC components, because my signals are transients, so they represent the quick evolution of a variable. Do you think that the mean value should also be removed in this case? In this case, it is even hard to define a mean value, actually there is a trend + superimposed noise. In addition, I suspect that a mean value interferes with the MATLAB algorithm, which uses the FFT to compute the cross-correlation rapidly. Probably this algorithm is optimized for signals with a DC component (more or less constant in time) + zero-mean components/noise. What do you think? – EmThorns Nov 21 '20 at 11:46
• I modified the question, to be more specific. – EmThorns Nov 21 '20 at 11:52
• Is it like rise? Can you model it in a parametric model? – Royi Nov 21 '20 at 15:40
• I think that the shape does not matter so much. As an example, you can imagine a ramp, a successive flat part with a pulse inside and then a descent ... with superimposed noise of course – EmThorns Nov 21 '20 at 15:54
• DC Component in DSP is basically the Mean of a signal. Regarding to the white noises I wanted to show the case the Mean of 2 signals hides the fast they are basically so different. Hence it makes sense in most cases not to measure it. – Royi Nov 30 '20 at 5:43