I want to compare two periodic signals. Lets say I have 1 period for each of the signals, and I want to know how similar they are. The final target of this is that if they are similar enough (for a given criteria) I can stop calculating some other stuff and save a lot of time. But that doesn't really matter, the question is the same, how to tell if two signals are similar.
I first considered the xcorr(x,y) function, the problem I see with cross correlation is that it gives good info about how to phase one of the signals for it to best match the other one, but it doesn't say how similar they are (or maybe I don't understand it very well, haha). From what I understand and have tested, cross correlation can be really affected by the signals amplitude, for example:
f1 = 1000*sin(x)
f2 = sin(x)
c = xcorr(f2,f2) is the ideal case, where the correlation is perfect c2 = xcoor(f1,f2) is the case I am evaluating, and the peak at c2 is bigger than c, suggesting better correlation? There is a normalization option in MATLAB which I don't really understand (I do understand it when you are doing autocorrelation, like c = xcorr(f2,f2,'coeff'), then you geat the peak correlation to be 1 at zero lag, that is OK, but for crosscorrelation between two functions, I don't understand how this normalization works).
I don't care about the phase since I will compare just one period of each of the signals, so I just need an algorithm to quantify how equal or similar these two signals are. But from what I see the correlation can sometimes be miss leading.
I may add that the signals are discrete, with the same number of elements, same sampling frequency and their frequency is the same, and known, if that helps.
Thanks in advance. JP
No products are associated with this question.
Perhaps you are looking for a scalar quantity to represent the similarity between two signal as is? If that's the case, you basically only need zero lag, try
Yes, I am looking for a scalar quantity to represent the similarity between two signals, and yes, I only need zero lag. But I don't understand the result when using 'coeff'. How is the normalization done?
I ask this because I see that the final result I get is not sensible to amplitude changes. For instance, to illustrate my doubt, run this:
x = 1 : 0.1 : 10; f1 = sin(x); f2 = 2*sin(x); f3 = 5*sin(x);
[c11,lag11]=xcorr(f1,f1);%,'coeff'); [c12,lag12]=xcorr(f1,f2);%,'coeff'); [c13,lag13]=xcorr(f1,f3);%,'coeff');
[c11norm,lag11]=xcorr(f1,f1,'coeff'); [c12norm,lag12]=xcorr(f1,f2,'coeff'); [c13norm,lag13]=xcorr(f1,f3,'coeff');
If you don't normalize, then figure(2) shows that you get more correlation as your functions have more amplitud, which is expected if you see the correlation definition. If you then use 'coeff' to normalize (and I don't know what happens here), f2 and f3 correlate equally with f1, which we know should not be the case.
Conclusion, either I don't know how to use xcorr well, or this will not work for what I need.
The correlation coefficient is bounded by 1 in absolute value. (Normalized) correlation values always lie between -1 and 1. At either extreme, you are saying that the linear relationship between the two signals is perfect. In other words, one is perfectly linearly predictable from knowledge of the other.
If you use xcorr without the 'coeff' option then you are obtaining the unnormalized cross correlation (at zero lag) in your case. That value absolutely depends on the values of the two waveforms. Just as covariance (not correlation) depends on the values of the quantities you are measuring. Look at the mathematical expression for the (unnormalized) cross correlation at zero lag. You are multiplying the two vectors element by element and summing all the values. If you do that, then as you increase or decrease the amplitude, you increase or decrease the value at zero lag accordingly.
That is precisely why xcorr() with 'coeff' is easier to interpret for your case. You can immediately tell the strength of the linear relationship. It's the same situation with covariance and correlation in inferential statistics. If I tell you the covariance between two random variables is 10,000, then what does that mean? Does it mean the linear relationship is strong or not? The answer is you don't know based on that number alone. But if I tell you the correlation is 0.95, that tells you something. That tells you something about the strength of the linear relationship between the two.
Hi Juan, I've got the exact same problem. Xcorr simply gets larger as the amplitude of your comparing signal's multiplier gets larger and if you use the 'coeff' option you get the same value for every multiplier. What I ended up doing is simply dividing one by the other. So.. first do xcorr to find the time shift and then divide to find the amplitude comparison. This makes sense for my situation but mine's a bit different from yours, so maybe it won't work for you. But I thought I'd throw it out there.
Hi to all, I am sorry I didn't answer again. In the end I just ended comparing the two samples by term-to-term subtraction. I needed to do this because I was integrating the transient response of a system, and each step of integration costs a lot so I wanted to be able to recognize when it was valid to assume that a steady state was reached. I tried correlation, tried filtering the signal to see just the DC or offset and evaluate its convergence, and many other stuff but in the end the best option still was the subtraction. It was, of course, the most demanding in terms of similarity, so it was the most safe option to go with. Thanks to all for your help!