final_experiment_epoch_blocks.m 3.32 KB
Newer Older
mposthuma's avatar
mposthuma committed
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
%% Epoch data and sacdet
%  Epoching to only take relevant parts of data
l     = dir('prep*.mat');
fn    = l(1).name;

fs          = 120;
duration    = 3;
samples     = fs*duration - 1;

for iB = 1:length(Data.timestamps)
   
   % Empty traces
   E.AzChairEpoched   	= [];
   E.ElChairEpoched    = [];
   E.AzGazeEpoched     = [];
   E.ElGazeEpoched     = [];
   E.AzEyeEpoched      = [];
   E.ElEyeEpoched      = [];
   E.AzHeadEpoched     = [];
   E.ElHeadEpoched     = [];
   
   % Interpolate Gaze
   lsl_opti  	= Data.timestamps(iB).optitrack;
   Data.timestamps(iB).epoch_interp       = 0:1/120:lsl_opti(end)-lsl_opti(1);
   Data.position(iB).gaze_interp(:,1)     = interp1(lsl_opti-lsl_opti(iB), Data.position(iB).gaze(:,1), Data.timestamps(iB).epoch_interp,'pchip')';
   Data.position(iB).gaze_interp(:,2)  	= interp1(lsl_opti-lsl_opti(iB), Data.position(iB).gaze(:,2), Data.timestamps(iB).epoch_interp,'pchip')';
   
   % Interpolate Chair
   CUT_OFF = 203;
   Data.position(iB).chair_interp(:,1)    = interp1(Data.timestamps(iB).chair, Data.position(iB).chair, Data.timestamps(iB).epoch_interp,'pchip')';
   Data.position(iB).chair_interp(Data.timestamps(iB).epoch_interp>CUT_OFF,1)  = zeros(1,sum(Data.timestamps(iB).epoch_interp>CUT_OFF));              % Correct for extrapolation == 0;
   Data.position(iB).chair_interp(:,2)    = zeros(size(Data.position(iB).chair_interp(:,1)));
   
   % Interpolate Eye
   Data.position(iB).eye_interp(:,1)      = interp1(lsl_opti-lsl_opti(iB), Data.position(iB).pupillabs(:,1), Data.timestamps(iB).epoch_interp,'pchip')';
   Data.position(iB).eye_interp(:,2)      = interp1(lsl_opti-lsl_opti(iB), Data.position(iB).pupillabs(:,2), Data.timestamps(iB).epoch_interp,'pchip')';
   
   % Interpolate Head
   Data.position(iB).head_interp(:,1)      = interp1(lsl_opti-lsl_opti(iB), Data.position(iB).optitrack(:,1), Data.timestamps(iB).epoch_interp,'pchip')';
   Data.position(iB).head_interp(:,2)      = interp1(lsl_opti-lsl_opti(iB), Data.position(iB).optitrack(:,2), Data.timestamps(iB).epoch_interp,'pchip')';
   
   
   % Select stimuli indices
   nstim       = length(Data.stimuli(iB).azimuth);
   ntriggers   = length(Data.timestamps(iB).stimuli);
   ind         = 1;
   ext         = 2;

   for iS = 1:nstim
      % epoch for stimuli
      start             = Data.timestamps(iB).stimuli(ind) - lsl_opti(1);
      [~,idx]           = min(abs(Data.timestamps(iB).epoch_interp-start));
      
      % Gaze
      E.AzGazeEpoched  	= [E.AzGazeEpoched, Data.position(iB).gaze_interp(idx:idx+samples,1)'];
      E.ElGazeEpoched  	= [E.ElGazeEpoched, Data.position(iB).gaze_interp(idx:idx+samples,2)'];
      
      % Chair
      E.AzChairEpoched  = [E.AzChairEpoched, Data.position(iB).chair_interp(idx:idx+samples,1)'];
      E.ElChairEpoched 	= [E.ElChairEpoched, Data.position(iB).chair_interp(idx:idx+samples,2)'];
      
      % Eye
      E.AzEyeEpoched   	= [E.AzEyeEpoched, Data.position(iB).eye_interp(idx:idx+samples,1)'];
      E.ElEyeEpoched   	= [E.ElEyeEpoched, Data.position(iB).eye_interp(idx:idx+samples,2)'];

      % Head
      E.AzHeadEpoched   = [E.AzHeadEpoched, Data.position(iB).head_interp(idx:idx+samples,1)'];
      E.ElHeadEpoched   = [E.ElHeadEpoched, Data.position(iB).head_interp(idx:idx+samples,2)'];
      
      ind = ind + ext;
   end
   Data.epoch(iB) = E;
end


fn = strrep(fn,'preprocessed','epoched');
save(fn,'Data');