XCORR: How to find the location of the highest correlation on the actual data using XCORR

Hi all,
It is already taking me days to understand the xcorr function and I am not able to get it :(!
Example:
x1=[5 4 2.5 0.5 0 0.5 2.5 4 5 5 5]
x2=[0 0.5 2.5 4 5 5 5]
y1=xcorr(x1,x2)
plot(y1) gives me:
First I thought: GREAT!
But why is the highest correlation on 15, I wrote down 100 of notes to figure it out. All I need is the location on x1 where the correlation is the highest.
In my actual dataset x1 is deoxygenation during exercise (first down during exercise, then mono-exponentially up in recovery). My x2 is a monoexponential shaped curve, in this way I want to find the response time, the point where the data is shaped mono-exponential.
Maybe the answer is easy, which would save me lots of frustrations!
Thanks in advance

Answers (2)

If you just need the highest peak location, you can simply use max
[~,idx] = max(y1)
HTH

3 Comments

Hi,
Thanks for the answer. Though, that was not what I meant :(. The peak does not correspond with the actual location on which the correlation is the highest. That has to do with the kernel shifting over the data in the beginning and the end (therefore the correlation is probably zero in the beginning).
The help xcorr did not give me the answer yet.
I see, I misunderstood. You should use the second output argument in xcorr, such as
[y1, lags] = xcorr(x1,x2);
[~,idx] = max(y1);
lags(idx)
Is this what you want?
He HTH,
I don't have much time now, but I checked it fast and it seems to work! If it works with my GUI you made my day(well, year!!)!

Sign in to comment.

Hmm..
Still I have some problems while it did not work on my bigger dataset. Therefore, I tried something else:
NIRS_KERNEL_DLOW=zeros(1,50)-0.5;
NIRS_KERNEL_DLOW2=zeros(1,22000)-0.5;
NIRS_KERNEL_x=0:0.1:32;
NIRS_KERNEL_DHIGH=NIRS_MONO_KERNEL(NIRS_KERNEL_x);
NIRS_KERNEL=NIRS_KERNEL_DHIGH;
NIRS_KERNEL2=[NIRS_KERNEL_DLOW2 NIRS_KERNEL_DHIGH];
where function NIRS_MONO_KERNEL is:
function [output]=NIRS_MONO_KERNEL(x)
output=(0.5-exp(-x/15));
end
The correlation part:
EXAMPLE(1,:)=xcorr(NIRS_KERNEL2,NIRS_KERNEL);
CORR=EXAMPLE(1,((round((length(EXAMPLE)/2)))-length(NIRS_KERNEL)):end);
while the DHIGH is exactly the same, I want the location of the of the highest correlation peak (where I would assume this is the value of the length of NIRS_KERNEL2, while at this point the NIRS_KERNEL is exactly on correlated to the data). When I check this value, it is 18 datapoints different..
Maybe, the lags still works and I am doing something really stupid :(!
Altogether, I need to find the location of the original data where the correlation is highest.
Can somebody explain me where I go wrong?

1 Comment

I thought I had it, but with increasing sizes of datasets, the function fails to be accurate. Does anybody know the solution?

Sign in to comment.

Asked:

on 8 May 2015

Commented:

on 27 Jul 2015

Community Treasure Hunt

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

Start Hunting!