-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessRawEyeMovements.m
executable file
·413 lines (322 loc) · 13.9 KB
/
ProcessRawEyeMovements.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
function ProcessRawEyeMovements
% Process eye movement data for further processing. needed only once.
clearvars;
close all force;
toSaveData = 'E:\\Eye movement data for Whitening study\\processedData.mat';
folders{1} = 'E:\\Eye movement data for Whitening study\\Analysed Data\\Normals (Old)\\';
folders{2} = 'E:\\Eye movement data for Whitening study\\Analysed Data\\Normals (Young)\\';
folders{3} = 'E:\\Eye movement data for Whitening study\\Analysed Data\\Patients (UCB)\\';
folders{4} = 'E:\\Eye movement data for Whitening study\\Analysed Data\\Patients (UH)\\';
% some parameters
keyPhrases = {'480_hz_';'480_hz_';'540_hz_';'480_hz_'};
samplingRate = [480; 480; 540; 480];
pixelSizeDeg = 1/20;
% walk through the folders listed above
data = [];
for i=1:length(folders)
currentListing = dir(folders{i});
usefulFiles = zeros(size(currentListing));
% refine the list by filtering out the irrelevant files/folders
for j=1:length(currentListing)
if ~currentListing(j).isdir
if ~isempty(strfind(currentListing(j).name,keyPhrases(i)))
usefulFiles(j) = 1;
% while at it, load the relevant file
fileContents = load([folders{i} currentListing(j).name]);
% find saccades and plot results, make a selection as to
% whether or not use this file for further analysis
[isGood, saccades, rawPosition, rawTime, filteredPosition,...
stitchedPosition, stitchedTime]...
= PreProcess(fileContents, pixelSizeDeg,samplingRate(i));
if isGood == 1
thisTrial.saccades = saccades;
thisTrial.rawPosition = rawPosition;
thisTrial.rawTime = rawTime;
thisTrial.filteredPosition = filteredPosition;
thisTrial.stitchedPosition = stitchedPosition;
thisTrial.stitchedTime = stitchedTime;
filename = currentListing(j).name;
thisTrial.fullFileName = [folders{i} filename];
thisTrial.initials = filename(1:3);
if i>2
group = 3;
else
group = i;
end
thisTrial.group = group;
data = [data; thisTrial];
end
if isGood == -1
return;
end
end
end
end
end
clear currentListing;
save(toSaveData,'data');
function [isGood, saccades, rawPosition, rawTime, filteredPosition,...
stitchedPosition, stitchedTime] ...
= PreProcess(fileContents, pixelSizeDeg, sampleRate)
try
time = fileContents.timeaxis_secs;
position = fileContents.frameshifts_strips_spline * pixelSizeDeg;
% shift everything to zero time and position
time = time - time(1);
position(:,1) = position(:,1) - position(1,1);
position(:,2) = position(:,2) - position(1,2);
% fix the gaps in time
[time, position] = FixTemporalGaps(time, position, sampleRate);
% find out the number of strips per frame and do median filtering with a
% window of half this size.
numberOfStripsPerFrame = fileContents.numbervideoframes / fileContents.videoframerate;
mfposition(:,1) = medfilt1(position(:,1),round(numberOfStripsPerFrame/2));
mfposition(:,2) = medfilt1(position(:,2),round(numberOfStripsPerFrame/2));
% lowpass filtering eye positions
lpFilt = designfilt('lowpassfir','SampleRate',sampleRate,...
'PassbandFrequency',120, ...
'StopbandFrequency',150,'PassbandRipple',0.01, ...
'StopbandAttenuation',300,'DesignMethod','kaiserwin');
lpmfposition(:,1) = filtfilt(lpFilt,mfposition(:,1));
lpmfposition(:,2) = filtfilt(lpFilt,mfposition(:,2));
% notch filter
d = designfilt('bandstopiir','FilterOrder',2, ...
'HalfPowerFrequency1',59,'HalfPowerFrequency2',61, ...
'DesignMethod','butter','SampleRate',sampleRate);
bsmflpposition(:,1) = filtfilt(d,lpmfposition(:,1));
bsmflpposition(:,2) = filtfilt(d,lpmfposition(:,2));
% figure(21234);cla;
% [Y,f] = ComputeFFT(sampleRate,sqrt(position(:,1).^2 + position(:,2).^2));
% [Y,f] = ComputeFFT(sampleRate,sqrt(mfposition(:,1).^2 + mfposition(:,2).^2));
% [Y,f] = ComputeFFT(sampleRate,sqrt(lpmfposition(:,1).^2 + lpmfposition(:,2).^2));
% [Y,f] = ComputeFFT(sampleRate,sqrt(bsmflpposition(:,1).^2 + bsmflpposition(:,2).^2));
% now find saccades in the 2D velocity space
saccades = FindSaccades(time, bsmflpposition);
% get drifts
[stitchedPosition, stitchedTime] = FindDrifts(time,bsmflpposition,saccades, sampleRate);
filteredPosition = bsmflpposition;
rawPosition = position;
rawTime = time;
isGood = 1;
figure(12345);
subplot(4,1,4)
cla;
plot(stitchedTime,stitchedPosition(:,1),'-r',stitchedTime,stitchedPosition(:,2),'-b')
ylabel('Drift only')
% Construct a questdlg with two options
choice = questdlg('Do you want to use this trial', ...
'Is Good Menu', ...
'Yes','No','Exit','No');
% Handle response
switch choice
case 'Yes'
isGood = 1;
case 'No'
isGood = 0;
case 'Exit'
isGood = -1;
otherwise
isGood = -1;
end
if isempty(saccades)
isGood = 1;
end
catch preperr
isGood = 0;
saccades = [];
rawPosition = [];
rawTime = [];
stitchedPosition = [];
stitchedTime = [];
filteredPosition = [];
preperr.stack.name
preperr.stack.line
end
function [Y,f,P1] = ComputeFFT(Fs,position)
L = length(position); % Length of signal
Y = fft(position);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
figure(21234);
semilogy(f,P1); hold on;
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
function [newTime, newPosition] = FixTemporalGaps(time, position, sampleRate)
% this function finds the temporal gaps in the data and
% interpolates/stitches gap region appropriately. If the gap is tiny, e.g.,
% a few samples long, then we interpolate.. If longer than that, we stitch
% different parts.
n = 5; % samples
diffTime = diff(time);
indicesTBI = find((diffTime > ((1/sampleRate)+0.0001)) & (diffTime < (n*(1/sampleRate)+0.0001)));
% start interpolation
newTime = time;
newPosition = position;
offsetDueToInsertion = 0;
for i=1:length(indicesTBI)
tempTime = time(indicesTBI(i):indicesTBI(i)+1);
tempPosition = position(indicesTBI(i):indicesTBI(i)+1,:);
newTempTime = (tempTime(1):(1/sampleRate):tempTime(2)+0.0002)';
newTempPosition(:,1) = interp1(tempTime,tempPosition(:,1),newTempTime,'pchip');
newTempPosition(:,2) = interp1(tempTime,tempPosition(:,2),newTempTime,'pchip');
% now inset the interpolated regions back to time and position arrays
newTime = [ newTime(1:(indicesTBI(i)+offsetDueToInsertion-1)); ...
newTempTime; ...
newTime((indicesTBI(i)+offsetDueToInsertion+2):end)];
newPosition = [ newPosition(1:(indicesTBI(i)+offsetDueToInsertion-1),:); ...
newTempPosition; ...
newPosition((indicesTBI(i)+offsetDueToInsertion+2):end,:)];
offsetDueToInsertion = offsetDueToInsertion + length(newTempTime) - 2;
end
% now to-be-stitched ones
diffTime = diff(newTime);
indicesTBS = find(diffTime >= (n*(1/sampleRate)+0.0001));
for i=1:length(indicesTBS)
deltaT = -newTime(indicesTBS(i)+1) + newTime(indicesTBS(i)) + (1/sampleRate);
newTime(indicesTBS(i)+1:end) = newTime(indicesTBS(i)+1:end) + deltaT;
deltaPosition = -newPosition(indicesTBS(i)+1,:) + newPosition(indicesTBS(i),:);
newPosition(indicesTBS(i)+1:end,:) = newPosition(indicesTBS(i)+1:end,:) + repmat(deltaPosition,(length(newTime)-indicesTBS(i)),1);
end
% % double check if we got it right
% N = histcounts(diff(newTime));
% if N == (length(newTime)-1)
% fprintf('Temporal gaps have been fixed successfully!\n');
% else
% fprintf('There is something fishy with the time array. Check it out more carefully.\n')
% end
function saccades = FindSaccades(time, position, lambda, deltaStitch, nSample)
if nargin<3
lambda = 6;
deltaStitch = 10;
nSample = 8;
end
maxDuration = 0.15; % secs
regionLength = max(time)/2; % seconds
try
% compute velocity
velocity_hor = [0; diff(position(:,1))./diff(time)];
velocity_ver = [0; diff(position(:,2))./diff(time)];
% now detect saccades (from Englbert & Kliegl 2003)
threshold_hor = GetRegionalThreshold(velocity_hor,time,regionLength,lambda);
threshold_ver = GetRegionalThreshold(velocity_ver,time,regionLength,lambda);
aboveThreshold = (abs(velocity_hor) > threshold_hor) | (abs(velocity_ver) > threshold_ver);
% take the difference of indices computed above to find the onset and
% offset of the movement
dabove = [0; diff(aboveThreshold)];
onsets = find(dabove == 1);
offsets = find(dabove == -1);
if length(onsets) > length(offsets)
offsets = [offsets; length(velocity_hor)];
elseif length(onsets) < length(offsets)
offsets = offsets(1:end-1);
end
% if two consecutive saccades are closer than 10ms, merge them
tempOnsets = onsets;
tempOffsets = offsets;
% loop until there is no pair of consecutive saccades closer than
% deltaStitch ms
while true
for c=1:min(length(onsets),length(offsets))-1
if (onsets(c+1)-offsets(c))<deltaStitch
tempOnsets(c+1) = -1;
tempOffsets(c) = -1;
end
end
s_on = tempOnsets(tempOnsets ~= -1);
s_off = tempOffsets(tempOffsets ~= -1);
if sum((s_on(2:end)-s_off(1:end-1)) < deltaStitch)==0
break;
end
end
onsets = tempOnsets(tempOnsets ~= -1);
offsets = tempOffsets(tempOffsets ~= -1);
% remove too brief and too long saccades
tooBrief = (offsets - onsets) < nSample;
tooLong = (time(offsets) - time(onsets)) > maxDuration;
toBeDiscarded = [onsets(tooBrief | tooLong) offsets(tooBrief | tooLong)];
onsets(tooBrief | tooLong) = [];
offsets(tooBrief | tooLong) = [];
% duration of saccades
durations = time(offsets) - time(onsets);
% finally compute the amplitude of saccades
amplitudes = abs(position(offsets) - position(onsets));
saccades.onsets = onsets;
saccades.offsets = offsets;
saccades.amplitudes = amplitudes;
saccades.durations = durations;
saccades.toBeDiscarded = toBeDiscarded;
saccades.lambda = lambda;
saccades.deltaStitch = deltaStitch;
saccades.nSample = nSample;
h=figure(12345);
set(h,'units','normalized','outerposition',[.05 .1 .5 .8]);
subplot(4,1,1)
cla;
plot(velocity_hor,velocity_ver,'-k');
hold on;
for i=1:length(onsets)
plot(velocity_hor(onsets(i):offsets(i)),velocity_ver(onsets(i):offsets(i)),'-r');
end
title('2D velocity space')
subplot(4,1,2)
cla;
plot(time,velocity_hor,'-r',time,velocity_ver,'-b');hold on;
for i=1:length(onsets)
plot(time(onsets(i):offsets(i)),velocity_hor(onsets(i):offsets(i)),'or',time(onsets(i):offsets(i)),velocity_ver(onsets(i):offsets(i)),'ob');
end
ylabel('Velocity')
subplot(4,1,3)
cla;
plot(time,position(:,1),'-r',time,position(:,2),'-b');hold on;
for i=1:length(onsets)
plot(time(onsets(i):offsets(i)),position(onsets(i):offsets(i),1),'or',time(onsets(i):offsets(i)),position(onsets(i):offsets(i),2),'ob');
end
ylabel('Position')
catch findsacerr
saccades = [];
findsacerr.stack.name
findsacerr.stack.line
end
function threshold = GetRegionalThreshold(velocity,time,regionLength,lambda)
regions = 0:regionLength:max(time);
if regions(end) ~= max(time)
regions(end) = max(time);
end
threshold = zeros(size(velocity));
for i=1:(length(regions)-1)
indices = (regions(i)<=time) & (regions(i+1)>=time);
threshold(indices) = lambda*sqrt(median(velocity(indices).^2)-median(velocity(indices))^2);
end
function [stitchedPosition, stitchedTime] = FindDrifts(time,position,saccades,sampleRate)
% first remove the saccade regions from time and position arrays
onsets = saccades.onsets;
offsets = saccades.offsets;
for i=1:length(onsets)
time(onsets(i):offsets(i)) = NaN;
position(onsets(i):offsets(i),:) = NaN;
end
% also discard the toBeDiscarded
for i=1:length(saccades.toBeDiscarded)
time(saccades.toBeDiscarded(i,1):saccades.toBeDiscarded(i,2)) = NaN;
position(saccades.toBeDiscarded(i,1):saccades.toBeDiscarded(i,2),:) = NaN;
end
nanIndices = isnan(time);
time(nanIndices) = [];
position(nanIndices,:) = [];
% stitch across saccadic gaps and discarded regions to get drift only traces.. adjust the time
% array accordingly, as if nothing happened.
diffTime = diff(time);
indicesTBS = find(diffTime >= (1/sampleRate)+0.0001);
% now it's time to stitch across saccades
stitchedTime = time;
stitchedPosition = position;
for i=1:length(indicesTBS)
deltaT = -stitchedTime(indicesTBS(i)+1) + stitchedTime(indicesTBS(i)) + (1/sampleRate);
stitchedTime(indicesTBS(i)+1:end) = stitchedTime(indicesTBS(i)+1:end) + deltaT;
deltaPosition = -stitchedPosition(indicesTBS(i)+1,:) + stitchedPosition(indicesTBS(i),:);
stitchedPosition(indicesTBS(i)+1:end,:) = ...
stitchedPosition(indicesTBS(i)+1:end,:) + repmat(deltaPosition,(length(stitchedTime)-indicesTBS(i)),1);
end