%{
Good day to all. I have a problem when cross correlating periodic signals and finding the correct delay between the two signals. In the code below i generated a 100 hertz sine wave and 0.5 sec delayed version of it. When i cross correlate the two sequences i expect to find the delay between the two sequences to be 0.5 sec but MATLAB gives in correct value of 4.000000000000000e-002. (Finding the delay is done by finding the indices of the maximum of Rxy as illustrated in the code below. I know this is to do with using the max function and the floating point representation of values. I would like to know how to correct this so that i get the correct delay when cross correlating periodic signals.

> I know this is to do with using the max function and the floating point representation of values.

No, it isn't.

The problem is that the cross correlation produces a discrete
series, and a naive search for the maximum amplitude will be
constrained to the discrete delays. If the actual delay is
not an integer number of samples, there will be poblems.

You might want to refine your delay estimates by investigating
the phase of the cross spectrum.

Rune Allnor <allnor@tele.ntnu.no> wrote in message <f11579f8-18c1-4640-b9ef-81976f32a77d@z4g2000yqa.googlegroups.com>...
> On 9 apr, 12:24, "Michal Kotze" <mkotz...@yahoo.de> wrote:
>
> > I know this is to do with using the max function and the floating point representation of values.
>
> No, it isn't.
>
> The problem is that the cross correlation produces a discrete
> series, and a naive search for the maximum amplitude will be
> constrained to the discrete delays. If the actual delay is
> not an integer number of samples, there will be poblems.
>
> You might want to refine your delay estimates by investigating
> the phase of the cross spectrum.
>
> Rune

No your wrong look at the plot of the cross correlation function you can clearly see at -0.7 the first maximum starts and repeats. However with floating point the values of these peaks are not the same (Up until a certain amount of digits they are the same). Use format long u will see the values of the peaks are 5.477225575051663e-001 ; 5.477225575051663e-001 ; 5.477225575051664e-001 as an example, thus using the max function it will find the max value but not at the correct delay due to the fact that the values at the peaks differ. Here is the update code and could you please explain to me how to determine the delay from the phase of the cross spectrum that i dont understand. Thanks for you reply i appreciate it alot.

What makes you think that your CH2 is just a delayed version of CH1? Where in CH1 do you have 0.7 seconds of continuous zeros? MATLAB is doing exactly what you've asked it to, and calculated the crosscorrelation of two arbitrary vectors.

"Mark Shore" <mshore@magmageosciences.ca> wrote in message <hpndn9$lcd$1@fred.mathworks.com>...
> Your code is not at all doing what you think it is.
>
> What makes you think that your CH2 is just a delayed version of CH1? Where in CH1 do you have 0.7 seconds of continuous zeros? MATLAB is doing exactly what you've asked it to, and calculated the crosscorrelation of two arbitrary vectors.

Halo Mark thanks for your reply. If t = 0:0.001:0.999 then its length is 1000 points and since it represent a time vector of 1 second the sample rate must equal the number of points generated in one second (Fs = 1000). So it is correct to say one point represents 1/1000 = 1e-3 seconds thus the 700 point will represent in time domain 0.7 seconds. So setting CH2 to zero from 1:700 and from 701:1000 = CH1(1:300) ,Channel 2 is then the delayed version of channel 1 with the delay equal to 700 points and if you want to get this value in "time domain" you must divide by the sample rate (Fs). In this case 700/Fs = 700/1000 = 0.7. Copy my code and make CH1 = randn(1,1000) you will then calculate the correct value of 0.7. The problem is with periodic signals.

On Apr 9, 6:24 am, "Michal Kotze" <mkotz...@yahoo.de> wrote:
> %{
> Good day to all. I have a problem when cross correlating periodic signals and finding the correct delay between the two signals. In the code below i generated a 100 hertz sine wave and 0.5 sec delayed version of it. When i cross correlate the two sequences i expect to find the delay between the two sequences to be 0.5 sec but MATLAB gives in correct value of 4.000000000000000e-002. (Finding the delay is done by finding the indices of the maximum of Rxy as illustrated in the code below. I know this is to do with using the max function and the floating point representation of values. I would like to know how to correct this so that i get the correct delay when cross correlating periodic signals.
>
> Thanks
>
> Regards
>
> Michal Kotze
> mko...@mtnloaded.co.za
>
> %}
> clear all
> t = 0:0.0001:0.9999 ; %time vector
> Fs = 10000 ; % Sample Rate
>
> CH1 = sin(2.*pi.*100.*t) ; %Change sine to randn(1,10000) then get correct delay
> CH2 = zeros(1,10000) ;
> CH2(7001:10000) = CH1(1:3000) ; % Set up delayed vector of CH1
>
> [Rxy,lags] = xcorr(CH1,CH2,'coef') ;

Thanks Greg you seem to be the only one understanding the problem. I appreciate it alot.I found another method to determine the correct value by rounding of the maximum value of the cross correlation function and finding the values were the cross correlation is bigger or equal to it. See the section in code {%__Section rounding of maximum value of the Rxy__}.

On Apr 9, 7:05 am, Rune Allnor <all...@tele.ntnu.no> wrote:
> On 9 apr, 12:24, "Michal Kotze" <mkotz...@yahoo.de> wrote:
>
> > I know this is to do with using the max function and the floating point representation of values.
>
> No, it isn't.
>
> The problem is that the cross correlation produces a discrete
> series, and a naive search for the maximum amplitude will be
> constrained to the discrete delays. If the actual delay is
> not an integer number of samples, there will be poblems.
>
> You might want to refine your delay estimates by investigating
> the phase of the cross spectrum.

Rune,

I have not been able to get anywhere with this. If Rxy = fft(rxy),
is it a simple matter of dividing angle(Rxy) at the peak of
abs(Rxy) by 2*pi*f?

Thanks Greg and Mark for your replies and for looking into my problem. Greg and Mark I will answer you questions soon but i am a bit busy now at university. Check the post round about Wednesday, 14 April 2010. Regards Michal Kotze

On Apr 10, 9:19 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Apr 9, 7:05 am, Rune Allnor <all...@tele.ntnu.no> wrote:
>
> > On 9 apr, 12:24, "Michal Kotze" <mkotz...@yahoo.de> wrote:
>
> > > I know this is to do with using the max function and the floating point representation of values.
>
> > No, it isn't.
>
> > The problem is that thecrosscorrelationproduces a discrete
> > series, and a naive search for the maximum amplitude will be
> > constrained to the discrete delays. If the actual delay is
> > not an integer number of samples, there will be poblems.
>
> > You might want to refine your delay estimates by investigating
> > the phase of thecrossspectrum.
>
> Rune,
>
> I have not been able to get anywhere with this. If Rxy = fft(rxy),
> is it a simple matter of dividing angle(Rxy) at the peak of
> abs(Rxy) by 2*pi*f?