'xcorr()' giving some negative values

I have two arrays A and B both having only positive values with some being zero. I am using c=xcorr(A,B) to get the cross correlation. I would expect the result to be all positive but getting some negative values. What is the reason?

4 件のコメント

Jan
Jan 2017 年 1 月 29 日
Could you post the concerned data and the code? Perhaps it is only it is only a typo and wild guessing the reason is a waste of time. Thanks.
dpb
dpb 2017 年 1 月 29 日
More than likely it's just rounding error...for example if use the example data for xcorr
>> load sensorData s1 s2
>> plot(s1)
>> hold all
>> plot(s2)
>> cor=xcorr(abs(s1),abs(s2));
>> max(cor)
ans =
0.0025
>> min(cor)
ans =
-3.3204e-19
>> sum(cor<0)
ans =
255
>> length(cor)
ans =
5241
>>
the data going in were all >=0 by abs() operation and still some 5% of the resulting autocorrelation values are negative. Now the largest in absolute value of those is only on order of machine double precision, but it's still negative.
Biplab Roy
Biplab Roy 2017 年 1 月 31 日
Thanks all. As dpb correctly placed, is a classic example of some results being negative even though all inputs are positive. And i also feel it is the rounding error. Could not this be corrected?
dpb
dpb 2017 年 1 月 31 日
Sure
cor(cor<0)=0;
if you feel you must. But, in general, when you use floating point, rounding errors are inherent with the beastie.

サインインしてコメントする。

回答 (0 件)

製品

タグ

質問済み:

2017 年 1 月 29 日

コメント済み:

dpb
2017 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by