Home > NoiseTools > nt_find_outlier_trials3.m

# nt_find_outlier_trials3

## PURPOSE

[idx,d]=nt_find_outlier_trials3(x,criterion,norm_flag) - find outlier trials

## SYNOPSIS

function [idx,d,cc]=nt_find_outlier_trials3(x,criterion,norm_flag)

## DESCRIPTION

    [idx,d]=nt_find_outlier_trials3(x,criterion,norm_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
      norm_flag: if true divide each frame by its RMS [default:0]

      This version compares covariance matrices of sample-normalized data.

      If no output arguments are specified, plots 'd'.

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_cov [c,tw]=nt_cov(x,shifts,w) - time shift covariance
- nt_find_outlier_trials3 [idx,d]=nt_find_outlier_trials3(x,criterion,norm_flag) - find outlier trials
- nt_fold y=fold(x,epochsize) - fold 2D to 3D
- nt_unfold y=nt_fold(x) - unfold 3D to 2D

This function is called by:

- nt_find_outlier_trials3 [idx,d]=nt_find_outlier_trials3(x,criterion,norm_flag) - find outlier trials

## SOURCE CODE

    0001 function [idx,d,cc]=nt_find_outlier_trials3(x,criterion,norm_flag)
    0002 %[idx,d]=nt_find_outlier_trials3(x,criterion,norm_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 %  norm_flag: if true divide each frame by its RMS [default:0]
    0010 %
    0011 %  This version compares covariance matrices of sample-normalized data.
    0012 %
    0013 %  If no output arguments are specified, plots 'd'.
    0014 %
    0015
    0016 if nargin<3||isempty(norm_flag); norm_flag=0; end
    0017 if nargin<2||isempty(criterion); criterion=inf; end
    0018 if ndims(x)~=3; error('x should be 3D'); end
    0019
    0020 if nargout==0;
    0021     [idx,d]=nt_find_outlier_trials3(x,criterion,norm_flag);
    0022     plot(d, '.-');
    0023     xlabel('trial'); ylabel('normalized deviation from mean');
    0024     clear idx d mn idx_unsorted
    0025     return
    0026 end
    0027
    0028 [m,n,o]=size(x);
    0029 x=nt_unfold(x);
    0030 if norm_flag
    0031     x=bsxfun(@times,x,1./(eps+sqrt(mean(x.^2,2)))); % normalize by dividing each sample by rms
    0032 end
    0033 x=nt_fold(x,m);
    0034
    0035 cc=zeros(n,n,o);
    0036 for iTrial=1:o
    0037     cc(:,:,iTrial)=nt_cov(x(:,:,iTrial));
    0038 end
    0039
    0040 cc=reshape(cc,n*n,o);
    0041 cc=bsxfun(@times,cc,1./(eps+sqrt(mean(cc.^2,1)))); % normalize by dividing each trial cv by rms
    0042
    0043 d=sqrt(mean( bsxfun(@minus,cc,mean(cc,2) ).^2 )) ./  sqrt(mean(mean(cc,2).^2));
    0044
    0045 %
    0046 %
    0047 % if isempty(mn); mn=mean(x,2); end
    0048 % if isnan(mn)
    0049 %     mn=nt_tsregress(x,mean(x,2));  % distance from regression
    0050 % else
    0051 %     mn2=repmat(mn(:),1,o);       % distance from mean
    0052 % end
    0053 % d=x-mn2;
    0054
    0055
    0056 %d=mean(d.^2);
    0057 %d=d/sqrt(mean(d.^2));
    0058 %d=mean(d.^2)/mean(mn.^2);
    0059
    0060 idx=find(d<criterion);
    0061
    0062 % [dd,idx]=sort(d,'ascend');
    0063 % idx=idx(find(dd<criterion));
    0064 % idx_unsorted=idx;
    0065 % idx=sort(idx); % put them back in natural order
    0066 % mn=mean(x(:,idx),2);
    0067
    0068