signal processing

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);

3 个评论

Can your provide a short (maybe length 10 or so) example of sample1 and sample2 in which you do not get what you expect?
This is some of Sample 1 ..
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
And this is Some of Sample 2
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
If you run it, you will get an answer for the delay but if you swap the argument in xcorr(y,x) you will get different answer but I believe it should be the same answer but opposite sign. I tested it this code with artificial data with known delay and It worked, Not sure why its not working for the real one?

请先登录,再进行评论。

 采纳的回答

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?

更多回答(2 个)

Susan
Susan 2011-8-24
I tried to add the sample to my file instead of using it from dat file, If you run the full version of Sample 1 and Sample 2 .. I get one answer but with opposite sign and thats what I was expecting so technically my code was working fine at the start but what don't understand is why if I use the file source it produce wrong answer. I just copied the data in the file and run it? whats going wrong by calling it instead.. this is what I did to call the data file
Sample1 = importdata(filename1);
Sample2 = importdata(filename2);
I have so many source code and I want just to type any two dat files and get the delay, my code seems to work but is that mean when I call the file names from the command it does something else?
The other thing, Can you please explain to me this code, you mentioned it above. Is it away to see if both of the signals are originating from the same source but one is delayed
figure
plot(1:31, Rxy, 'b', 1:31, fliplr(Ryx), 'r')
figure
plot(Rxy-fliplr(Ryx))
eps(sum(sample1.^2))

2 个评论

Daniel's code compare the results of XCORR if the inputs are swapped. He shows, that except for the reverted order and some rounding errors, the inputs are equal.
You can be sure that MATLAB is not confused by your source files. It does exactly what you told it to do. It MATLAB does "something else", it recieved different input data. Therefore its answer is not wrong, but your expectations. A general suggestion for such situations is a coffee break.
The coffee advice did help :)

请先登录,再进行评论。

Jan
Jan 2011-8-24
Did you get the point about FLIPLR?
"I believe I should get the same answer as the x,y but opposite
sign (minus/positive)"
But in addition the replies of xcorr(X,Y) and xcorr(Y,X) are reverted.

4 个评论

Do you know how to draw on top of the signal, the signal signal with the delay calculated so It will be easier to see if they around the same?
Thanks,
I think my answer answered the question you asked and that this is an unrelated question. Please ask it as a new one so others can follow, contribute, and learn. If you do not think your original question has been answered, please comment in the appropriate answer.
See: "help hold".
If you have a question concerning plotting, it is always a good idea to type "help plot" and look in the "See also:" list at the bottom. This list is created very carefully to assist beginners and advanced MATLAB users.
OK- Thank you both..I appreciate the help !

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by