0001 function [y,mn]=nt_rereference(x,w,factor)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 if nargin<1; error('!'); end
0016 if nargin<2||isempty(w); w=ones(size(x)); end
0017 if nargin<3||isempty(factor); factor=1; end
0018
0019 if size(w,1)~=size(x,1); error('!'); end
0020 if size(w,2)==1; w=repmat(w,1,size(x,2)); end
0021 if size(w,2) ~= size(x,2); error('!'); end
0022
0023
0024 Reference is weighted mean.
0025
0026
0027 mn=sum(x.*w,2)./sum(w,2);
0028 mn(isnan(mn))=0;
0029 y=bsxfun(@minus,x,mn*factor);
0030
0031 if ~nargout
0032
0033 x=nt_unfold(x); y=nt_unfold(y); mn=nt_unfold(mn); w=nt_unfold(w);
0034 figure(1); clf;
0035 subplot 411; plot(x); title('raw');
0036 subplot 412; plot(y); title('rereferenced');
0037 subplot 413; plot(mn); title('reference');
0038 subplot 414; plot(mean(w)); title('mean weight'); ylim([-.1 1.1']); xlabel('channel');
0039 figure(2);
0040 histogram(mn(:)/std(mn(:)), 100);
0041 clear y mn
0042 end