Home > NoiseTools > nt_epoch.m

nt_epoch

PURPOSE ^

y=nt_epochify(x,idx,bounds) - extract epochs based on trigger indices

SYNOPSIS ^

function y=nt_epochify(x,idx,bounds)

DESCRIPTION ^

y=nt_epochify(x,idx,bounds) - extract epochs based on trigger indices

  y: 3D array of epoched data (time*channels*trials)

  x: raw data (time*channels)
  idx: array of trigger indices (can be fractionary)
  bounds: (samples) start and stop of epoch
  
  Space indices at intervals of epochsize (can be fractionary):
       y=nt_epochify(x,[],epochsize);
  
  NoiseTools.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y=nt_epochify(x,idx,bounds)
0002 %y=nt_epochify(x,idx,bounds) - extract epochs based on trigger indices
0003 %
0004 %  y: 3D array of epoched data (time*channels*trials)
0005 %
0006 %  x: raw data (time*channels)
0007 %  idx: array of trigger indices (can be fractionary)
0008 %  bounds: (samples) start and stop of epoch
0009 %
0010 %  Space indices at intervals of epochsize (can be fractionary):
0011 %       y=nt_epochify(x,[],epochsize);
0012 %
0013 %  NoiseTools.
0014 nt_greetings;
0015 
0016 if nargin<3; error('!'); end
0017 if isempty(idx)
0018     % space indices at regular intervals of epoch size
0019     if numel(bounds)>1; error('!'); end
0020     idx=1 : bounds : size(x,1)-bounds;
0021     bounds=ceil(bounds);
0022 end
0023 if numel(bounds)==1 % align epoch start with index
0024     bounds=[0 bounds]; 
0025 end
0026 
0027 % check that indices fit, discard those that don't
0028 idx=idx(:); 
0029 lowestIdx=-bounds(1); % lowest
0030 highestIdx=size(x,1)-bounds(2); 
0031 if max(idx)>highestIdx
0032     warning(num2str(numel(idx>highestIdx)));
0033       disp('indices beyond end of data');
0034     idx=idx(idx<=highestIdx);
0035 end
0036 if min(idx)<lowestIdx
0037     warning(num2str(numel(idx<lowestIdx)));
0038       disp('indices beyond end of data');
0039     idx=idx(idx>=lowestIdx);
0040 end
0041   
0042 % split data into trials
0043 nsamples=bounds(2)-bounds(1)+1;
0044 if idx == round(idx)
0045     % integer positions
0046     y=zeros(nsamples, size(x,2), numel(idx));
0047     for k=1:numel(idx);
0048         y(:,:,k)=x(tidx(k)+(bounds(1):bounds(2)),:);
0049     end
0050 else
0051     % fractionnary positions
0052     warning('noninteger indices, using interpolation'); 
0053     sidx=repmat(idx, [1,nsamples]) + repmat((1:nsamples),[numel(idx,1),1]); 
0054     sidx=sidx(:);
0055     yy=interp1(1:size(x,1),x,sidx+bounds(1));
0056     y=nt_fold(yy,bounds(2)-bounds(1)+1);
0057 end
0058

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005