两种大小不同的两个信号之间的相关性 [英] Correlation between two signals with two different sizes
问题描述
我正在尝试计算两个信号之间的相关性,如果两个信号相同,则返回1,否则返回0到1.两个信号的大小不同,因此需要重采样的问题.我已经做到了,但是输出不正确.谁能帮助我有效地实施它.
我的代码:
MaxRow = max(size(A,1),size(B,1));
MaxCol = max(size(A,2),size(B,2));
NewA = resample(A,MaxRow,size(A,1));
NewB = resample(B,MaxRow,size(B,1));
NewA = resample(NewA',MaxCol,size(A,2))';
NewB = resample(NewB',MaxCol,size(B,2))';
for s = 1:MaxRow
a = NewA(s,:);
b = NewB(s,:);
c(s)=real(corr(a',b'));
end
c(isnan(c)) = 0 ;
score = mean(c);
这是一个玩具示例.
% Example Data
x = 0:9;
y = 1:0.1:10;
% Check if y is longer
if length(x) < length(y)
x = interp1( x, linspace( 1, length(x), length(y) ) ); % Resample x
else
y = interp1( y, linspace( 1, length(y), length(x) ) ); % Resample y
end
% Get corrcoeff
c = abs( corrcoef( x, y ) ); % Corrcoeff solution here
c = c(2,1);
% Get MSE
m = mse( x - y ); % MSE solution here
linespace
将在1
和length(x)
之间生成带有length(y)
格数的索引.
基本上interp1
会将变量重新采样为另一个变量的长度. if语句将检查哪个需要重新采样.函数corrcoef
将获得2个信号的系数的相关系数.由于corrcoef
在0到1之间,因此我们需要一个绝对值.如果您不关心缩放和偏差,则corrcoef
将为您工作.
如果您打算改用MSE
,则可以对错误(x-y)
使用MSE
函数,但是它不会在0到1之间.任何比较方法都适用于此重采样代码.>
I am trying to calculate the correlation between two signals, where it returns 1 if both are the same and it will return between 0 and 1 otherwise. The problem that the two signals have different sizes so resampling is needed. I already did it but the output is not correct. Can anyone help me to implement it in an efficient way.
My code:
MaxRow = max(size(A,1),size(B,1));
MaxCol = max(size(A,2),size(B,2));
NewA = resample(A,MaxRow,size(A,1));
NewB = resample(B,MaxRow,size(B,1));
NewA = resample(NewA',MaxCol,size(A,2))';
NewB = resample(NewB',MaxCol,size(B,2))';
for s = 1:MaxRow
a = NewA(s,:);
b = NewB(s,:);
c(s)=real(corr(a',b'));
end
c(isnan(c)) = 0 ;
score = mean(c);
Here is a toy example.
% Example Data
x = 0:9;
y = 1:0.1:10;
% Check if y is longer
if length(x) < length(y)
x = interp1( x, linspace( 1, length(x), length(y) ) ); % Resample x
else
y = interp1( y, linspace( 1, length(y), length(x) ) ); % Resample y
end
% Get corrcoeff
c = abs( corrcoef( x, y ) ); % Corrcoeff solution here
c = c(2,1);
% Get MSE
m = mse( x - y ); % MSE solution here
linespace
will generate indicies between 1
and length(x)
with length(y)
number of divisions.
Essentially interp1
will resample the variable to the length of the other one. The if statement will check which one needs resampling. The function corrcoef
will get the correlation coefficient of the coefficient of the 2 signals. Since corrcoef
is between 0 and 1, we need an absolute value. If you don't care about scaling and bias corrcoef
will work for you.
If you plan to use MSE
instead, then you can use the MSE
function on the error (x-y)
but it will not be between 0 and 1. Any comparison method will work with this resampling code.
这篇关于两种大小不同的两个信号之间的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!