Home > GVSToolbox > codeSaccadesDist.m

codeSaccadesDist

PURPOSE ^

identify saccades

SYNOPSIS ^

function [ sacStruct ] = codeSaccadesDist( eyeTrack, velThreshold, stopThreshold)

DESCRIPTION ^

 identify saccades
 July 11, 2011
 Notes: This function codes the saccades
 
 syntax: [sacStruct ]  = codeSaccades( eyeTrack, velThreshold, stopThreshold )
 
 Inputs: 
       eyeTrack: vector  of complex (x+i*y) coordinates.  -1-i1  to identify
           missing data. Data should be scaled according to the image dimensions.  
           See function 'scaleEyeTrack'.
       velThreshold: min Euclidean pixel distance to travel in one sample unit time to
          be considered a saccade
       stopThreshold: max distance to travel in one sample to be considered a stop 
  
 Outputs:
       saccadeVector: vector having 0 where no fixations, then 1, 2 ,3,
           and so on for the fixations in the sequence
       startSacPos: in format [ x x x ... x; y y y ... y]
       endSacPos: in format [ x x x ... x; y y y ... y]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % identify saccades
0002 % July 11, 2011
0003 % Notes: This function codes the saccades
0004 %
0005 % syntax: [sacStruct ]  = codeSaccades( eyeTrack, velThreshold, stopThreshold )
0006 %
0007 % Inputs:
0008 %       eyeTrack: vector  of complex (x+i*y) coordinates.  -1-i1  to identify
0009 %           missing data. Data should be scaled according to the image dimensions.
0010 %           See function 'scaleEyeTrack'.
0011 %       velThreshold: min Euclidean pixel distance to travel in one sample unit time to
0012 %          be considered a saccade
0013 %       stopThreshold: max distance to travel in one sample to be considered a stop
0014 %
0015 % Outputs:
0016 %       saccadeVector: vector having 0 where no fixations, then 1, 2 ,3,
0017 %           and so on for the fixations in the sequence
0018 %       startSacPos: in format [ x x x ... x; y y y ... y]
0019 %       endSacPos: in format [ x x x ... x; y y y ... y]
0020 %
0021 
0022 function [  sacStruct  ] = codeSaccadesDist( eyeTrack, velThreshold, stopThreshold)
0023 
0024     if nargin < 2 || isempty( velThreshold)
0025         velThreshold = 20;
0026     end
0027     if nargin < 3 || isempty( stopThreshold )
0028         stopThreshold = 8;
0029     end
0030 
0031     numSamples =  length( eyeTrack);
0032     saccadeVec = zeros(numSamples ,1);
0033     startSacPos = zeros( 2, numSamples );
0034     endSacPos = zeros( 2, numSamples );
0035     startPos = 1; 
0036     peakVelIdx = 1; %make sure it starts, will become -1 if none
0037     k1 = 1;
0038     % find all saccades based on a velocity threshold
0039     while (peakVelIdx > 0) && (startPos < numSamples ) 
0040             [ sacIdxStart sacIdxEnd peakVelIdx] = findSaccade(  eyeTrack, velThreshold, stopThreshold, startPos );
0041             if (peakVelIdx > 0)
0042                 startSacPos(:,k1) = [ real( eyeTrack(sacIdxStart)); imag( eyeTrack(sacIdxStart))  ];
0043                 endSacPos(:,k1) = [ real( eyeTrack(sacIdxEnd)); imag( eyeTrack(sacIdxEnd))  ];                    
0044                 saccadeVec(sacIdxStart:sacIdxEnd) = k1;
0045                 startPos = sacIdxEnd+1;
0046                 k1 = k1+1;
0047             end                       
0048     end
0049     
0050     startSacPos(:,k1:end) = []; %clean up unused parts
0051     endSacPos(:,k1:end) = [];
0052     
0053     
0054     sacStruct.saccadeVec = saccadeVec;
0055     sacStruct.startSacPos = startSacPos;
0056     sacStruct.endSacPos = endSacPos;
0057       
0058     
0059 end
0060 
0061 %---------------------------------------------------
0062  %--------------------------------------------------------
0063  
0064 function [ sacIdxStart, sacIdxEnd, peakVelIdx] = findSaccade(  eyeTrack, velThreshold, stopThreshold, startPos )
0065 
0066 % % velThreshold = 20;
0067 % % stopThreshold = 5;
0068 % % eyeTrack = eyePos;
0069 % % startPos = [];
0070 
0071    
0072     %--------------- initialize -----------------------
0073     windowSize = 2;
0074     minTracksToConsider = 2;
0075     numSamples = length( eyeTrack);
0076     dist = -1*ones( numSamples,1);
0077     eyeTrack =  [ real( eyeTrack(:)) imag(eyeTrack(:)) ]'; % in [ x x ... x; y y ... y ]
0078     maxPos = size( eyeTrack,2) - windowSize+1;
0079 
0080     peakVelIdx = -1;
0081     sacIdxStart = startPos;
0082     sacIdxEnd = -1;
0083     
0084     % specify search start position
0085     if nargin < 4 || isempty( startPos )   
0086         i1 = 0;
0087     else
0088         i1 =  startPos-1;
0089     end
0090 
0091     %--------------------------------------------------------
0092     %-----------find saccade based on velocity threshold ----------
0093     saccadeFound = 0;
0094     while (i1 < maxPos) 
0095         %increment, until start with not missing if necessary
0096          i1 =  i1+1;     
0097         while (i1 < maxPos) && (eyeTrack(1,i1)==-1) 
0098             i1 =  i1+1; 
0099         end
0100         % select window of windowSize samples, expand if
0101         % too much missing data or if last sample is missing
0102         i2 = 0; %for expanding window
0103         
0104         
0105         window = eyeTrack(:, i1:(i1+windowSize-1));        
0106         missIdx = find( window(1,:) == -1 );
0107         window( :, missIdx ) = [];
0108         % old way
0109 %         window( window==-1) = [];
0110                        
0111         window = reshape( window, 2,[]);
0112         
0113         
0114         while ( (size( window,2) < minTracksToConsider ) || ...
0115                     (eyeTrack(1, i1+windowSize-1+i2)==-1)  ) ...
0116                      && (i1+i2)<maxPos  % do not expand too far
0117             i2 = i2+1;
0118             window = eyeTrack(:, i1:(i1+windowSize-1+i2));
0119             missIdx = find( window(1,:) == -1 );
0120             window( :, missIdx ) = [];
0121         % old way
0122 %             window( window==-1) = [];
0123         end
0124 
0125         % find start position, and end position of saccade based on a
0126         % threshold velocity for starting slow, middle fast, end slow
0127         window = reshape( window, 2,[]);  
0128         if size( window,2) >= minTracksToConsider %handle missing data
0129             % distance traveled per time unit
0130             dist(i1) = norm( [window(:,end) - window(:,1)], 'fro')/(windowSize+i2-1); 
0131             if (dist(i1) < stopThreshold) && ( saccadeFound < 1) %start condition
0132                 sacIdxStart = i1;
0133             end
0134             if (dist(i1) > velThreshold) && (sacIdxStart >0 ) %threshold condition
0135                 saccadeFound = i1;
0136             end
0137             if (dist(i1) < stopThreshold) && ( saccadeFound > 1) %end condition
0138                 if sacIdxEnd < 0 % set initial slowdown
0139                     sacIdxEnd = i1;                
0140                 end
0141                 if dist(i1) > dist( sacIdxEnd )  % find end point (when start speeding up again)
0142                     [ peakVel peakVelIdx   ] = max( dist(sacIdxStart:sacIdxEnd));
0143                     peakVelIdx = peakVelIdx+ sacIdxStart-1;                    
0144                     break;
0145                 else % gaze still slowing down
0146                     sacIdxEnd = i1;
0147                 end
0148             end            
0149         end            
0150     end % end while
0151 
0152     % don't count saccade if no end point
0153     if ( saccadeFound > 1) && sacIdxEnd ==-1        
0154         peakVelIdx =-1;
0155         sacIdxStart = -1;
0156     end
0157 
0158 end
0159

Generated on Tue 01-Jul-2014 12:35:04 by m2html © 2005