Reply by Rune Allnor February 7, 20082008-02-07
On 6 Feb, 20:52, "john1985" <murray_1...@hotmail.com> wrote:
> >I have tested the script and corrected a couple of bugs. > >The correct script is: > > >N=33; > >x = reshape(sin((0:N-1)*0.2*pi),N,1); > >M = 512; > >y = 0.5*randn(M,1); > >K = 128; > >y(K:K+N-1)=y(K:K+N-1)+x; > >% cxy = crosscorr(y,x); &#4294967295; % Home-made cross correlation routine > >%===================== > >cxy =xcorr(x,y); > >cxy =xcorr(y,x); > >%===================== > >kv=(1:N+M-1)-N; > >plot(kv,cxy); > >xlabel('Lag k') > > >I don't have the SP toolbox, so I don't know which of > >the xcorr statements is the correct one, but one of them > >gives a peak at k = 128. > > >Rune > > Hi Rune, > Did exactly as above and still get the error > ??? Error using ==> plot > Vectors must be the same lengths. > > I think the length of the x and y axis need to be the same > regards
I checked the dosumentation of xcorr, and it says "c = xcorr(x,y) returns the cross-correlation sequence in a length 2*N-1 vector, where x and y are length N vectors (N>1). If x and y are not the same length, the shorter vector is zero-padded to the length of the longer vector." I find it counterintuitive to do things that way but... anyway, in that case it seems the line kv = ... above should be changed to N=max(length(x),length(y)); kv= (1:2*N-1)-N; Rune
Reply by john1985 February 6, 20082008-02-06
>I have tested the script and corrected a couple of bugs. >The correct script is: > >N=33; >x = reshape(sin((0:N-1)*0.2*pi),N,1); >M = 512; >y = 0.5*randn(M,1); >K = 128; >y(K:K+N-1)=y(K:K+N-1)+x; >% cxy = crosscorr(y,x); % Home-made cross correlation routine >%===================== >cxy =xcorr(x,y); >cxy =xcorr(y,x); >%===================== >kv=(1:N+M-1)-N; >plot(kv,cxy); >xlabel('Lag k') > >I don't have the SP toolbox, so I don't know which of >the xcorr statements is the correct one, but one of them >gives a peak at k = 128. > >Rune >
Hi Rune, Did exactly as above and still get the error ??? Error using ==> plot Vectors must be the same lengths. I think the length of the x and y axis need to be the same regards John
Reply by Rune Allnor February 6, 20082008-02-06
I have tested the script and corrected a couple of bugs.
The correct script is:

N=33;
x = reshape(sin((0:N-1)*0.2*pi),N,1);
M = 512;
y = 0.5*randn(M,1);
K = 128;
y(K:K+N-1)=y(K:K+N-1)+x;
% cxy = crosscorr(y,x);   % Home-made cross correlation routine
%=====================
cxy =xcorr(x,y);
cxy =xcorr(y,x);
%=====================
kv=(1:N+M-1)-N;
plot(kv,cxy);
xlabel('Lag k')

I don't have the SP toolbox, so I don't know which of
the xcorr statements is the correct one, but one of them
gives a peak at k = 128.

Rune
Reply by Rune Allnor February 6, 20082008-02-06
On 6 Feb, 13:16, "john1985" <murray_1...@hotmail.com> wrote:
> >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.
OK, just clear everything: clear all; close all; clc and then run the script again. If the error returns, use the command whos and paste the output here.
> Besides that I don't think > it will help much as the problem i'm having is scaling the x-axis > according to time.
The script is intended to help you understand the relation between the x axis and time. Rune
Reply by john1985 February 6, 20082008-02-06
>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
Reply by Rune Allnor February 6, 20082008-02-06
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
Reply by john1985 February 6, 20082008-02-06
>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
Reply by Rune Allnor February 6, 20082008-02-06
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
Reply by john1985 February 5, 20082008-02-05
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 > >
Reply by gokul_s1 February 4, 20082008-02-04
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