MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Emelie Leht
on 27 Apr 2013

I am currently using the finddelay function to visualize a wave pattern. I have a matrix with periodic signals recorded and I use one of the signals as a reference to which I compare all of the others to achieve a matrix of delays which I then visualize as different colours in a matrix plot.

I my case I would assume to have sufficient correlation with the majority of the signals but what do the function actually return for the few that does not have a correlation? Will I get 0 as delay? I have looked into the code of the function but could not figure it out by myself. Would it be possible to adapt the function to get it to return a special value when there is no correlation so that I could single out and mark theses spots in my plot?

Related to this I also wonder if there is some general rule of how long this type of signals should be for the finddelay function to be applicable?

Thank you, Emelie

*No products are associated with this question.*

Answer by Wayne King
on 27 Apr 2013

If the signals are equal length, then why not use xcorr() with the 'coeff' option?

Then the cross-correlation will lie in the range [-1,1] and you pretty easily assess how significant the cross-correlation is when it achieves its maximum.

Emelie Leht
on 28 Apr 2013

Well, yes. I looked into it and it would be a possible solution but not a very neat one since I sort of need to redo what my finddelay function already does. I thought since the finddelay function itself uses xcorr to correlate the signals and finds the delay by taking out the lag for the highest correlation the information on the significance must already be in there. I just want to extract the information on when the signals does not have a sufficient correlation somehow. From what I understand I will get a 0 back in delay both in the cases of an actual 0 in delay and when I have no correlation in signals. I wonder if it would be possible to maybe alter some of the last rows in the code of the function which I assume sets the signals under the threshold for correlation to 0. I would like it to return NaN, -100, or whatever I can easily sort out from the significantly correlated signals. Anybody who has directions on how to do this?

//Emelie

Answer by Wayne King
on 28 Apr 2013

Edited by Wayne King
on 28 Apr 2013

It is not true that you will get back a delay of zero for cases when the signals are not significantly correlated at any lag.

rng default; x = randn(100,1); y = randn(100,1); [xc,lags] = xcorr(x,y,20,'coeff'); [rho,idx] = max(abs(xc)); lags(idx)

And cross-correlation sequences that achieve a maximum at zero lag can certainly indicate significant cross-correlation. Think about the autocorrelation of a sequence.

Depending on what distributional assumptions you can reasonably make about your signals, it may well be possible to come up with a principled threshold for significant cross-correlation, but just for the moment, let's assume you want to see at least 0.5

You can do something like this

[xc,lags] = xcorr(x,y,20,'coeff'); [rho,lags] = max(abs(xc)); if (rho>=0.5) Lag = lags(idx); else Lag = NaN; end

Emelie Leht
on 29 Apr 2013

I see what you are doing and why it would work. I know that a zero lag in cross correlation is not an indication of no correlation but rather a high correlation. However,I am not sure I was clear about it before, but the finddelay function I am using is an already implemented function by Matlab that is based on the xcorr function and will deliver me the delays directly and in this case a delay of zero even if there is no correleation. Documentation of the function can be found here: http://www.mathworks.nl/help/comm/ref/finddelay.html

I had a look at its code by typing: type finddelay

into Matlab and from that I understood that I would have a zero returned by the function even if there was no correlation by the signals. Since this finddelay function already makes use of the xcorr I thought it was a bit of double work to implement the xcorr again. Also, the reason I rather use the finddelay is that it can handle multiple signals at the same time while the xcorr only operates on one signal and its reference at a time which would require a lot of iteration in my case since I have a matrix of signals that I want to compare. Hope I made my case more clear!

//Emelie

## 0 Comments