互相关系数

发布于 2024-08-13 08:25:02 字数 477 浏览 4 评论 0原文

我在时域中有两个波形,我需要测量其中的互相关 MATLAB 中的系数。我已经尝试过 max(abs(xcorr(m,n,'coeff'))) 但它似乎无法正常工作。

另外我需要测量波形不同部分的互相关系数,例如以1分钟的间隔测量互相关系数。如果可能的话,将这些值输出到矩阵或其他东西。

我知道这是一个很多问题,但我是 MATLAB 新手,发现这项任务令人畏惧!
如果您能就这个问题的任何部分向我提供任何帮助,我将不胜感激。


编辑: 这是我用来测试相关代码的代码:

x = rand(1,14400);
y = rand(1,14400);
r = max( abs(xcorr(x,y,'coeff')) )

I have two waveforms in the time domain, of which I need to measure the cross-correlation coefficient in MATLAB. I have tried max(abs(xcorr(m,n,'coeff'))) but it doesn't seem to be working properly.

Also I need to measure the cross correlation coefficient for different sections of the waveform, e.g. measure the cross correlation coefficient at 1 minute intervals. And if possible output these values to a matrix or something.

I know this is a lot to ask but I'm a MATLAB novice and find this task daunting!
Any help you could give me on any section of this question would be gratefully received.


EDIT:
This is the code I used to test the correlation code:

x = rand(1,14400);
y = rand(1,14400);
r = max( abs(xcorr(x,y,'coeff')) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与风相奔跑 2024-08-20 08:25:02

根据本文中的方程,您可以计算互相关系数这样:

% Assuming: m and n are your signals organized as row vectors
r = cov([m;n]) / (std(m)*std(n));

如果您只想计算信号某些部分的系数,只需使用:

r = cov([m(1:100);n(1:100)]) / (std(m(1:100))*std(n(1:100)));

您是否也尝试过 corrcoef 功能?

编辑
好的,我检查了 corrcoef 函数,它似乎工作正常,看一下:

>> x = 100*randn(1000,1);
>> y=34*randn(1000,1);
>> corrcoef(x,y)

ans =

    1.0000   -0.0543
   -0.0543    1.0000

所以相关系数等于 -0.0543 - 相似度很小(如预期)。
为了检查这一点,让我们计算相同信号的系数:

>> y=x;
>> corrcoef(x,y)

ans =

     1     1
     1     1

正如预期的那样,它等于 1。

编辑
正如您所看到的, corrcoef 的结果是这两个信号之间所有可能的相关系数的矩阵:

       x        y
x    1.0000   -0.0543
y   -0.0543    1.0000

因此,对于互相关,您需要选择主对角线之外的元素之一(存在自相关系数,在此大小写始终等于 1)。
如果您选择 ans(2,1) 或 ans(1,2),则没有区别 - 如果您计算 x 和 y 的相关性,或者 y 和 x 的相关性,则没有区别。

所以最终的代码应该类似于:

R = corrcoef(x,y); % Correlation matrix
r = R(2,1); % this is your Cross-Correlation coefficient

According to equations in this article you can count the cross-correlation coefficient in this way:

% Assuming: m and n are your signals organized as row vectors
r = cov([m;n]) / (std(m)*std(n));

if you want to compute the coefficient only for some part of the signals, just use:

r = cov([m(1:100);n(1:100)]) / (std(m(1:100))*std(n(1:100)));

Have you also tried the corrcoef function?

Edit
Ok, I have checked the corrcoef function and it seems to be working properly, take a look:

>> x = 100*randn(1000,1);
>> y=34*randn(1000,1);
>> corrcoef(x,y)

ans =

    1.0000   -0.0543
   -0.0543    1.0000

So the correlation coefficient is equal -0.0543 - small similarity (as expected).
To check that, let's compute the coefficient for identical signals:

>> y=x;
>> corrcoef(x,y)

ans =

     1     1
     1     1

As expected, it's equal 1.

Edit.
As you can see, the result of corrcoef is a matrix of all possible correlation coefficients between these two signals:

       x        y
x    1.0000   -0.0543
y   -0.0543    1.0000

So for cross-correlation you need to select one of the elements outside the main diagonal (there are located self-correlation coefficients, in this case always equal 1).
There is no difference if you would select ans(2,1) or ans(1,2) - there is no difference, if you compute the correlation of x and y, or correlation of y and x.

So the final code should look similar to this:

R = corrcoef(x,y); % Correlation matrix
r = R(2,1); % this is your Cross-Correlation coefficient
把时间冻结 2024-08-20 08:25:02

尝试使用交叉协方差代替

x = rand(1,14400);
y = rand(1,14400);
r = max( abs(xcov(x,y,'coeff')) )

交叉协方差序列是均值去除的互相关
序列。就像 Joonas 提到的那样,rand 的 DC 偏移为 0.5,并且会给您一个“不正确”的结果。

Try using Cross-Covariance instead

x = rand(1,14400);
y = rand(1,14400);
r = max( abs(xcov(x,y,'coeff')) )

cross-covariance sequence is the cross-correlation of mean-removed
sequences. Like Joonas mentioned, rand has a DC offset at 0.5 and will give you an "incorrect" results.

烟酒忠诚 2024-08-20 08:25:02

我尝试过 max(abs(xcorr(m,n,'coeff'))) 但它似乎无法正常工作。

你这是什么意思?它输出什么,您期望什么?

互相关中的一个可能的问题是波形中的直流偏置会破坏结果。据我所知,没有通用的方法可以解决这个问题。您必须以某种方式确保您的波形不包含任何直流偏置。

I have tried max(abs(xcorr(m,n,'coeff'))) but it doesn't seem to be working properly.

What do you mean by that? What does it output, and what do you expect?

One possible gotcha in cross correlation is that a DC bias in the waveform will corrupt the result. And as far as I know, there's no universal way to do anything about it. You have to somehow ensure that your waveforms do not contain any DC bias.

歌枕肩 2024-08-20 08:25:02

这是我用来测试相关代码的代码:

x = rand(1,14400);
y = 兰特(1,14400);
r = max(abs(xcorr(x,y,'coeff')) )

问题是 rand 返回的数字均匀分布在区间 (0,1) 中。换句话说,您的 DC 偏置(平均值)为 0.5!这就是为什么看似随机的信号会获得高相关系数的原因:它们并不完全随机,因为每个信号都有一个类似的常数分量,显示在相关系数中。

因此,尝试使用 randn 来代替:它返回随机数,其元素均值 0 呈正态分布,这正是您想要的。

This is the code I used to test the correlation code:

x = rand(1,14400);
y = rand(1,14400);
r = max( abs(xcorr(x,y,'coeff')) )

The problem is that rand returns numbers that are are uniformly distributed in the interval (0,1). In other words, you have a DC bias (mean) of 0.5! That's why you get high correlation coefficient for seemingly random signals: they are not quite random, since each has a similar constant component that shows up in the correlation coefficient.

So, try using randn instead: it returns random numbers whose elements are normally distributed with mean 0, which is what you want here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文