Problem with cross correlation

8 views (last 30 days)
Ivan Mich
Ivan Mich on 2 Feb 2021
Edited: Ivan Mich on 24 Feb 2021
Hello. I have a problem with a code. I have two kinds of data (lets call them a and b). These are cells, and in each cell I have double/numbers (see images attached). i would like to cross correlate , using a loop, data of a with data of b. I am using the following code:
but I realise that the results are wrong, as I should have 100% correlation bacause of the similarity of the numbers.
for i=1:numel(a)
[c,lag]=crosscorr(a{i},b{i})
r = [0.65, 1];
ii = c >= r(1) & c <= r(2)
% this finds the index of he rows(2) that have x in between
idx = find(abs(c) > r(1) & abs(c) <= r(2));
% number of intervals with positive check
numIdx{i} = sum(abs(c) > r(1) & abs(c) <= r(2))
Final{i}=(numIdx{i})'
n=Final'
end
Could you help me in order to fix it?

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Feb 2021
Edited: KALYAN ACHARJYA on 2 Feb 2021
Might be you are looking for corrcoef Correlation coefficients.
or
r = xcorr(x,y) returns the cross-correlation of two discrete-time sequences. Cross-correlation measures the similarity between a vector x and shifted (lagged) copies of a vector y as a function of the lag.
In cross correlation,refer the MATLAB docs Specify Additional Lags for Cross-Correlation
Please refer the following external link to see, how to compute the cross corelation between discrete data
x [n] = {-3, 2, -1, 1}
y [n] = {-1, 0, -3, 2}
It's better to refer any standard signal analysis text book to undestand various types for corelation between two discrete signal data.
Example:
a=cell2mat({2,1,17,5,19});
b=cell2mat({2,1,17,5,19});
Results:
>> xcorr2(a,b)
ans =
38 29 362 199 680 199 362 29 38
>> xcorr(a); %Same as xcorr2 if both a and b are same
ans =
38.0000 29.0000 362.0000 199.0000 680.0000 199.0000 362.0000 29.0000 38.0000
>> result=crosscorr(a,b);
result =
-0.2369 -0.1835 0.1964 -0.2761 1.0000 -0.2761 0.1964 -0.1835 -0.2369
>> corrcoef(a,b)
ans =
1.0000 1.0000
1.0000 1.0000
  2 Comments
Ivan Mich
Ivan Mich on 2 Feb 2021
Edited: Ivan Mich on 2 Feb 2021
Is there a way to modify my script. I would like to have one number that should give me the correlation
(that's why I use
% number of intervals with positive check
numIdx{i} = sum(abs(c) > r(1) & abs(c) <= r(2))
numIdx shows me one number.
Do you know the way?
Ivan Mich
Ivan Mich on 2 Feb 2021
Edited: Ivan Mich on 24 Feb 2021
Could you please help me?
Thank you in advance

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!