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

92 views (last 30 days)
martijn on 8 May 2015
Commented: martijn on 27 Jul 2015
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!

Honglei Chen on 8 May 2015
If you just need the highest peak location, you can simply use max
[~,idx] = max(y1)
HTH
martijn on 8 May 2015
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!!)!

martijn on 16 Jul 2015
Edited: martijn on 16 Jul 2015
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?
martijn on 27 Jul 2015
I thought I had it, but with increasing sizes of datasets, the function fails to be accurate. Does anybody know the solution?