Home > NoiseTools > nt_rereference.m

# nt_rereference

## PURPOSE

[y,mn]=nt_rereference(x,w,factor) - rereference by subtracting robust mean

## SYNOPSIS

function [y,mn]=nt_rereference(x,w,factor)

## DESCRIPTION

    [y,mn]=nt_rereference(x,w,factor) - rereference by subtracting robust mean

       y: rereferenced data
       mn: subtracted reference

       x: data to rereference (time*channels or time*channels*trials
       w: mask
       factor: apply to mean before subtracting [default: 1]

     See also: nt_detrend, nt_outliers

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [y,mn]=nt_rereference(x,w,factor)
    0002 %[y,mn]=nt_rereference(x,w,factor) - rereference by subtracting robust mean
    0003 %
    0004 %   y: rereferenced data
    0005 %   mn: subtracted reference
    0006 %
    0007 %   x: data to rereference (time*channels or time*channels*trials
    0008 %   w: mask
    0009 %   factor: apply to mean before subtracting [default: 1]
    0010 %
    0011 % See also: nt_detrend, nt_outliers
    0012 %
    0013 % NoiseTools
    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);
    0030
    0031 if ~nargout
    0032     % don't return values, just plot
    0033     figure(1); clf;
    0034     subplot 411; plot(x); title('raw');
    0035     subplot 412; plot(y); title('rereferenced');
    0036     subplot 413; plot(mn); title('reference');
    0037     subplot 414; plot(mean(w)); title('mean weight'); ylim([-.1 1.1']); xlabel('channel');
    0038     figure(2);
    0039     histogram(mn(:)/std(mn(:)), 100);
    0040     clear y mn
    0041 end