MATLAB: Signal processing

delaysignal processingtest

Hello all, I am still struggling with this and I don't know why its producing wrong answer. I have my two signals and I want to produce the delay time between the two signals and see if they both originate from the same source or not so delay time is not known and the two signals might not be from the same source..I have many source data and I need to do it for any two then calculate the delay time.the problem is that I only get large positive numbers for the delay and if I swap x,y in the xcorr argument I get totally different answer and I believe I should get the same answer as the x,y but opposite sign (minus/positive)…
Thank you all in advance,
x = sample1(:,1);
X = (x).';
y = sample2(:,1);
Y = (y).';
figure;
clf
subplot(3,1,1);
[xi,f]=ksdensity(X);
plot(f,xi);
line(repmat(X,2,1),repmat([0;0.1*max(xi)],1,length(X)),'color','r' );
subplot(3,1,2);
[xi,f]=ksdensity(Y);
plot(f,xi);
line(repmat(Y,2,1),repmat([0;0.1*max(xi)],1,length(Y)),'color','r' );
[Rxx,lags] = xcorr(X,Y);
[Z,delay] = max(Rxx);
lags(delay);

Best Answer

I do not quite see what you are finding in that delay is the same in both cases (0):
sample1 = [6.526760455502977E7
6.526803236688536E7
6.5268252754813336E7
6.526834674377677E7
6.526860278268609E7
6.526885882158541E7
6.526893660555825E7
6.526923801844921E7
6.526936765841391E7
6.526948757538823E7
6.527011957014128E7
6.527021680012478E7
6.527026541509657E7
6.527042422403239E7
6.5271075664836034E7
6.527129929376417E7];
sample2 = [6.52689008725069E7
6.526904023546197E7
6.526926062337003E7
6.526948425228816E7
6.526999633010681E7
6.527001901709762E7
6.527068342188179E7
6.527216131739556E7
6.527217752238616E7
6.527234605434227E7
6.527377857593433E7
6.527422907480074E7
6.527427768977253E7
6.527455641569267E7
6.5274640681675725E7
6.527486431061386E7];
x = sample1(:,1);
X = (x).';
y = sample2(:,1);
Y = (y).';
[Rxy,lags] = xcorr(X,Y);
[Zxy,delay] = max(Rxy);
lags(delay)
[Ryx,lags] = xcorr(Y,X);
[Zyx,delay] = max(Ryx);
lags(delay)
figure
plot(1:31, Rxy, 'b', 1:31, fliplr(Ryx), 'r')
figure
plot(Rxy-fliplr(Ryx))
There are some noticeable differences between Rxy and fliplr(Ryx), but they are are approaching double precision.
eps(sum(sample1.^2))
What happens if you scale your vectors?