Home > NoiseTools > nt_find_outlier_trials.m

# nt_find_outlier_trials

## PURPOSE

[idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,plot,regress_flag) - find outlier trials

## SYNOPSIS

function [idx,d]=nt_find_outlier_trials(x,criterion,disp_flag,regress_flag)

## DESCRIPTION

    [idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,plot,regress_flag) - find outlier trials

      idx: indices of trials to keep
      d: relative deviations from mean

      x: data (time * channels * trials)
      criterion: keep trials less than criterion from mean
      disp: if true plot trial deviations before and after
      regress_flag: if true regress out mean, rather than subtract

      For example criterion=2 rejects trials that deviate from the mean by
      more than twice the average deviation from the mean.

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_banner h=nt_banner(text,varargin) - annotate with text at head of figure
- nt_find_outlier_trials [idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,plot,regress_flag) - find outlier trials
- nt_tsregress [z,idx]=nt_tsregress(x,y,shifts,xw,yw,keep,threshold) - time-shift regression

This function is called by:

- nt_find_outlier_trials [idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,plot,regress_flag) - find outlier trials

## SOURCE CODE

    0001 function [idx,d]=nt_find_outlier_trials(x,criterion,disp_flag,regress_flag)
    0002 %[idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,plot,regress_flag) - find outlier trials
    0003 %
    0004 %  idx: indices of trials to keep
    0005 %  d: relative deviations from mean
    0006 %
    0007 %  x: data (time * channels * trials)
    0008 %  criterion: keep trials less than criterion from mean
    0009 %  disp: if true plot trial deviations before and after
    0010 %  regress_flag: if true regress out mean, rather than subtract
    0011 %
    0012 %  For example criterion=2 rejects trials that deviate from the mean by
    0013 %  more than twice the average deviation from the mean.
    0014 %
    0015
    0016 if nargin<2; criterion=inf; end
    0017 if nargin<3; disp_flag=1; end
    0018 if nargin<4; regress_flag=0; end
    0019 if ndims(x)>3; error('x should be 2D or 3D'); end
    0020
    0021 if ndims(x)==3;
    0022     [m,n,o]=size(x);
    0023     x=reshape(x,m*n,o);
    0024 else
    0025     [~,o]=size(x);
    0026 end
    0027
    0028 mn=mean(x,2);
    0029 if regress_flag
    0030     mn=nt_tsregress(x,mean(x,2));  % regression
    0031 else
    0032     mn=repmat(mn(:),1,o);       % mean
    0033 end
    0034 d=x-mn; % difference from mean
    0035 dd=zeros(1,size(d,2));
    0036 for k=1:size(d,2); dd(k)=sum(d(:,k).^2); end
    0037 d=dd; clear dd;
    0038 d=d/(sum(x(:).^2)/o);
    0039
    0040 idx=find(d<criterion(1));
    0041
    0042 if nargout==0;
    0043     % just plot deviations
    0044     plot(d,'.-');
    0045     xlabel('trial'); ylabel('normalized deviation from mean');
    0046     clear idx d mn idx_unsorted
    0047 else
    0048     if disp_flag
    0049         % plot deviations before & after outlier removal
    0050         figure(100); clf
    0051         nt_banner('outlier trials');
    0052
    0053         subplot 121;
    0054         plot(d,'.-'); hold on;
    0055         plot(setdiff(1:o,idx), d(setdiff(1:o,idx)), '.r');
    0056         xlabel('trial'); ylabel('normalized deviation from mean'); title(['before, ',num2str(numel(d))]);
    0057         drawnow
    0058
    0059         subplot 122;
    0060         [~,dd]=nt_find_outlier_trials(x(:,idx),0,[]);
    0061         plot(dd,'.-');
    0062         xlabel('trial'); ylabel('normalized deviation from mean'); title(['after, ',num2str(numel(idx))]);
    0063         drawnow
    0064     end
    0065
    0066 end
    0067
    0068 criterion=criterion(2:end);
    0069 if ~isempty(criterion)
    0070     idx=nt_find_outlier_trials(x(:,idx),criterion,disp_flag,regress_flag);
    0071     idx = idx(idx2); % otherwise, idx doesn?t correspond to original matrix anymore
    0072 end