Forums

cross correlation lag scale

Started by john1985 February 2, 2008
Hi,
I am currently using the xcorr function in Matlab to correlate two
waveforms(plotted as voltage v's time). Both these waveforms have 2500
samples. When I use the xcorr function, the resultant waveform has 5000
samples (as expected). What I want to know is how do I scale these 5000
samples in order to get the lag time(between the two wavefroms)? ie. when
matlab finds the best match for waveform_A in waveform_B.
Thanks for any help
John
On Feb 2, 4:04 pm, "john1985" <murray_1...@hotmail.com> wrote:
> Hi, > I am currently using the xcorr function in Matlab to correlate two > waveforms(plotted as voltage v's time). Both these waveforms have 2500 > samples. When I use the xcorr function, the resultant waveform has 5000 > samples (as expected). What I want to know is how do I scale these 5000 > samples in order to get the lag time(between the two wavefroms)? ie. when > matlab finds the best match for waveform_A in waveform_B. > Thanks for any help > John
The output of xcorr() is the cross-correlation between the two vectors, at all possible lags. So, the first element would be at a lag of -2500, the next at a lag of -2499, and so on. Jason
>On Feb 2, 4:04 pm, "john1985" <murray_1...@hotmail.com> wrote: >> Hi, >> I am currently using the xcorr function in Matlab to correlate two >> waveforms(plotted as voltage v's time). Both these waveforms have 2500 >> samples. When I use the xcorr function, the resultant waveform has
5000
>> samples (as expected). What I want to know is how do I scale these
5000
>> samples in order to get the lag time(between the two wavefroms)? ie.
when
>> matlab finds the best match for waveform_A in waveform_B. >> Thanks for any help >> John > >The output of xcorr() is the cross-correlation between the two >vectors, at all possible lags. So, the first element would be at a lag >of -2500, the next at a lag of -2499, and so on. > >Jason >
Yes but how do I scale this according to time?I want to find the time (in wavefrom_B) when matlab finds a match for waveform_A in waveform_B Regards John
Here is small piece of code that would do the job.


  t = maxLag:length(wavA)-maxLag;



  r = xcorr(wavA(t),wavB(t));


  index1 = find(r == max(r));
  diff1 = median([1:length(r)]) - index1(1);


I guess the last message was incomplete. Anyways here is what I would do:

   t = maxLag:length(wavA)-maxLag

   % Find the cross correlation
    r = xcorr(wavA(t),wavB(t));

    %Find the index where maximum correlation occurs (best match)
    index1 = find(r == max(r));

    % Get the time lag in samples (multiply by sampling frequency if you
want   time lag in seconds)
    lag = median([1:length(r)]) - index1(1);

    % Shift waveform B to match waveform A
      wavA_new = wavA(t);
      wavB_new = wavB(t+lag);

Hope this helps
Gokul

Hi Gokul,
Sorry I didn't explain what I wanted correctly.....I am currently using
the xcorr function in Matlab to correlate two waveforms(plotted as voltage
v's time). Both these waveforms have 2500 samples. When I use the xcorr
function, the resultant waveform plot has 5000 samples(on the x-axis of my
cross correlation plot). What I want to know is how do I scale these 5000
samples according to a time base,so that insteadt of 0-5000 on my x-axis I
have the corresponding time. ie. I want to be able to indicate on my cross
correlation plot everytime matlab finds a match in waveform1 for
waveform2.(waveform2 occurs around 3 separate times in waveform1)
Thanks for any help
John





>I guess the last message was incomplete. Anyways here is what I would
do:
> > t = maxLag:length(wavA)-maxLag > > % Find the cross correlation > r = xcorr(wavA(t),wavB(t)); > > %Find the index where maximum correlation occurs (best match) > index1 = find(r == max(r)); > > % Get the time lag in samples (multiply by sampling frequency if you >want time lag in seconds) > lag = median([1:length(r)]) - index1(1); > > % Shift waveform B to match waveform A > wavA_new = wavA(t); > wavB_new = wavB(t+lag); > >Hope this helps >Gokul > >
On 2 Feb, 22:04, "john1985" <murray_1...@hotmail.com> wrote:
> Hi, > I am currently using the xcorr function in Matlab to correlate two > waveforms(plotted as voltage v's time). Both these waveforms have 2500 > samples. When I use the xcorr function, the resultant waveform has 5000 > samples (as expected).
5000 samples? Not 4999? A detail but important as you will see below.
> What I want to know is how do I scale these 5000 > samples in order to get the lag time(between the two wavefroms)? ie. when > matlab finds the best match for waveform_A in waveform_B.
With two waveforms of equal length N, the length K of the resulting waveform is K=2*N-1. The maximum overlap occurs at k = N, indexed from the start. Choosing k = N as the reference point is a convenient choise. To plot the correlation with respect to both negative and positive lags then becomes easy: cxy = xcorr(x,y); kv = 1:length(cxy)-lengh(x); plot(kv,cxy) Rune
>On 2 Feb, 22:04, "john1985" <murray_1...@hotmail.com> wrote: >> Hi, >> I am currently using the xcorr function in Matlab to correlate two >> waveforms(plotted as voltage v's time). Both these waveforms have 2500 >> samples. When I use the xcorr function, the resultant waveform has
5000
>> samples (as expected). > >5000 samples? Not 4999? A detail but important as you will see below. > >> What I want to know is how do I scale these 5000 >> samples in order to get the lag time(between the two wavefroms)? ie.
when
>> matlab finds the best match for waveform_A in waveform_B. > >With two waveforms of equal length N, the length K of the resulting >waveform is K=2*N-1. The maximum overlap occurs at k = N, indexed >from the start. > >Choosing k = N as the reference point is a convenient choise. >To plot the correlation with respect to both negative and positive >lags then becomes easy: > >cxy = xcorr(x,y); >kv = 1:length(cxy)-lengh(x); >plot(kv,cxy) > >Rune
Hi Rune, I tried your snippet of code but it didn't seem to work as got the following error
>> plot(kv,cordat)
??? Error using ==> plot Vectors must be the same lengths. This is the code (below) I'm using to cross correlate my two waveforms(saved as .fig files).The correlated plot should express the time in secs everytime matlab finds a match for waveform1 in waveform2. open waveform1.fig open waveform2.fig figure(1) x1=get(findobj(gca,'type','line'),'xdata'); y1=get(findobj(gca,'type','line'),'ydata'); figure(2) x2=get(findobj(gca,'type','line'),'xdata'); y2=get(findobj(gca,'type','line'),'ydata'); cordat=xcorr(y1,y2); plot(cordat) Thanks for any help John
On 6 Feb, 11:55, "john1985" <murray_1...@hotmail.com> wrote:

> >cxy = xcorr(x,y); > >kv = 1:length(cxy)-lengh(x); > >plot(kv,cxy) > > >Rune > > Hi Rune, > I tried your snippet of code but it didn't seem to work as got the > following error>> plot(kv,cordat) > > ??? Error using ==> plot > Vectors must be the same lengths.
Try kv = (1:length(cxy))-length(x); (parentheses added) instead. And test this with as simple data as possible to make sure you understand how it works! Test with this script (not tested!): N=33; x = reshape(sin((0:N-1)*0.2*pi),N,1); M = 512; y = randn(M,1); K = 128; y(K:K+N-1)=y(K:K+N-1)+x; cxy = xcorr(x,y); kv=(1:N+K-1)-K; plot(kv,cxy); If it runs (I don't have access to matlab right now, so I can't test it), it should have a peak near kv = K. Do *not* try to analyze your measured data until you get this script to run, and you understand how and why it works. Rune
>On 6 Feb, 11:55, "john1985" <murray_1...@hotmail.com> wrote: > >> >cxy = xcorr(x,y); >> >kv = 1:length(cxy)-lengh(x); >> >plot(kv,cxy) >> >> >Rune >> >> Hi Rune, >> I tried your snippet of code but it didn't seem to work as got the >> following error>> plot(kv,cordat) >> >> ??? Error using ==> plot >> Vectors must be the same lengths. > >Try > >kv = (1:length(cxy))-length(x); > >(parentheses added) instead. And test this with as >simple data as possible to make sure you understand >how it works! > >Test with this script (not tested!): > >N=33; >x = reshape(sin((0:N-1)*0.2*pi),N,1); >M = 512; >y = randn(M,1); >K = 128; >y(K:K+N-1)=y(K:K+N-1)+x; >cxy = xcorr(x,y); >kv=(1:N+K-1)-K; >plot(kv,cxy); > >If it runs (I don't have access to matlab right now, so I can't >test it), it should have a peak near kv = K. Do *not* try >to analyze your measured data until you get this script >to run, and you understand how and why it works. >Rune
Hi Rune, I ran your code but I got the ame error as before. I don't really understand it as you seem to definr kv twice. Besides that I don't think it will help much as the problem i'm having is scaling the x-axis according to time. Regards John