DSPRelated.com
Forums

cross correlation lag scale

Started by john1985 February 2, 2008
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
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
>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
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