0001 function [idx,d]=nt_find_outlier_trials(x,criterion,disp_flag,regress_flag)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
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));
0031 else
0032 mn=repmat(mn(:),1,o);
0033 end
0034 d=x-mn;
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
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
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);
0072 end