Skip to content

Commit b85f558

Browse files
committed
Stabilized correlation for DC signals
Previous single-pass algorithm for computing sample correlations was returning complex numbers for DC signals.
1 parent d8eafe0 commit b85f558

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

mtrf/mTRFevaluate.m

+10-11
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,34 @@
8282
if arg.window % use window
8383
idx = arg.window*(i-1)+1:arg.window*i;
8484
yi = y(idx,:);
85-
pi = pred(idx,:);
85+
predi = pred(idx,:);
8686
nobs = numel(idx);
8787
else % use entire trial
8888
yi = y;
89-
pi = pred;
89+
predi = pred;
9090
nobs = yobs;
9191
end
9292

9393
% Compute error
9494
switch arg.error
9595
case 'mse'
96-
err(i,:) = sum(abs(yi - pi).^2,1)/nobs;
96+
err(i,:) = sum(abs(yi - predi).^2,1)/nobs;
9797
case 'mae'
98-
err(i,:) = sum(abs(yi - pi),1)/nobs;
98+
err(i,:) = sum(abs(yi - predi),1)/nobs;
9999
end
100100

101101
switch arg.corr
102102
case 'Spearman' % convert to rank values
103103
yi = num2rank(yi,nobs,yvar);
104-
pi = num2rank(pi,nobs,pvar);
104+
predi = num2rank(predi,nobs,pvar);
105105
end
106106

107-
% Compute standard deviation
108-
sumy = sum(yi,1); sump = sum(pi,1);
109-
sdyp = sqrt((sum(yi.^2,1) - (sumy.^2)/nobs) .* ...
110-
(sum(pi.^2,1) - (sump.^2)/nobs));
107+
% Demean signals
108+
y0 = yi - sum(yi,1)/nobs;
109+
pred0 = predi - sum(predi,1)/nobs;
111110

112-
% Compute correlation
113-
r(i,:) = (sum(yi.*pi,1) - sumy.*sump/nobs)./sdyp;
111+
% Compute correlation coefficient
112+
r(i,:) = sum(y0.*pred0)/sqrt(sum(y0.^2)*sum(pred0.^2));
114113

115114
end
116115

0 commit comments

Comments
 (0)