[idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,mn,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 mn: mean (default: calculate from data) 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. Use nt_find_outlier_trials instead to remove a fixed proportion of trials. If no output arguments are specified, plots 'd'.
0001 function [idx,d]=nt_find_outlier_trials2(x,criterion,mn,regress_flag) 0002 %[idx,d,mn,idx_unsorted]=nt_find_outlier_trials(x,criterion,mn,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 % mn: mean (default: calculate from data) 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 % Use nt_find_outlier_trials instead to remove a fixed proportion of trials. 0016 % 0017 % If no output arguments are specified, plots 'd'. 0018 % 0019 0020 if nargin<2; criterion=inf; end 0021 if nargin<3; mn=[]; end 0022 if nargin<4; regress_flag=0; end 0023 if ndims(x)~=3; error('x should be 3D'); end 0024 0025 if nargout==0; 0026 [idx,d]=nt_find_outlier_trials2(x,criterion,mn,regress_flag); 0027 plot(d,'.-'); 0028 xlabel('trial'); ylabel('normalized deviation from mean'); 0029 clear idx d mn idx_unsorted 0030 return 0031 end 0032 0033 [m,n,o]=size(x); 0034 x=reshape(x,m*n,o); 0035 0036 if isempty(mn); mn=mean(x,2); end 0037 if regress_flag 0038 mn=nt_tsregress(x,mean(x,2)); % regression 0039 else 0040 mn=repmat(mn(:),1,o); % mean 0041 end 0042 d=x-mn; % difference from mean 0043 if 0 0044 d=sum(d.^2); 0045 else % to save memory 0046 dd=zeros(1,size(d,2)); 0047 for k=1:size(d,2); dd(k)=sum(d(:,k).^2); end 0048 d=dd; clear dd; 0049 end 0050 d=d/(sum(x(:).^2)/o); 0051 0052 % 0053 % 0054 % if isempty(mn); mn=mean(x,2); end 0055 % if isnan(mn) 0056 % mn=nt_tsregress(x,mean(x,2)); % distance from regression 0057 % else 0058 % mn2=repmat(mn(:),1,o); % distance from mean 0059 % end 0060 % d=x-mn2; 0061 0062 0063 %d=mean(d.^2); 0064 %d=d/sqrt(mean(d.^2)); 0065 %d=mean(d.^2)/mean(mn.^2); 0066 0067 idx=find(d<criterion); 0068 0069 % [dd,idx]=sort(d,'ascend'); 0070 % idx=idx(find(dd<criterion)); 0071 % idx_unsorted=idx; 0072 % idx=sort(idx); % put them back in natural order 0073 % mn=mean(x(:,idx),2); 0074 0075 0076 if nargout==0; 0077 plot(d); 0078 end