Problem with cross correlation

조회 수: 5 (최근 30일)
Ivan Mich
Ivan Mich 2021년 2월 2일
편집: Ivan Mich 2021년 2월 24일
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?

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 2월 2일
편집: KALYAN ACHARJYA 2021년 2월 2일
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
Ivan Mich
Ivan Mich 2021년 2월 2일
편집: Ivan Mich 2021년 2월 2일
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 2021년 2월 2일
편집: Ivan Mich 2021년 2월 24일
Could you please help me?
Thank you in advance

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by