问题描述:

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.

相关阅读:
Top