-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpupil_interpolate.m
86 lines (63 loc) · 2.27 KB
/
pupil_interpolate.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
function clean = pupil_interpolate(padata,varargin)
%filters blinks based on algorithm focusing on differences between
%timepoints (i.e., not based on what EyeLink records).
%INPUTS
%padata is a matrix where each row is a trial, or a structure from EyeLink with 'pa' as a field
%optional argument (1/0) indicates whether to plot the raw and cleaned
%data. This is ignored if number of trials exceeds 25 (too many plots!)
%mask is a binary matrix/vector where 1 indicates that an artifact was found at that time
warning('off','MATLAB:chckxy:IgnoreNaN');
%can specify whether the smoothed data replaces everything, or just fills
%in the gaps (default)
if nargin>1
replace = varargin{1};
else
replace = 0;
end
if isa(padata,'struct')
dostruct = 1;
eyeStruct = padata; %make a backup
padata = eyestruct2mat(padata);
% %turn all the data into a matrix
% temp={eyeStruct.pa}';
% maxsamples=max(cellfun(@length,temp));
% padata=nan(length(eyeStruct),maxsamples);
%
% for i=1:length(eyeStruct)
% padata(i,1:length(eyeStruct(i).pa)) = eyeStruct(i).pa;
% end
else
dostruct = 0;
end
clean = nan(size(padata));
%can be used for single trial or group of trials
for row = 1:size(padata,1)
startdata = find(~isnan(padata(row,:)),1,'first');
enddata = find(~isnan(padata(row,:)),1,'last');
pa = padata(row,1:enddata);
if mean(~isnan(padata(row,startdata:enddata))) >=.30 && length(startdata:enddata)>5
temp = pa(startdata:enddata);
sm = splinefit(1:length(temp),temp,25);
sm = ppval(sm,1:length(temp));
if replace
clean(row,startdata:enddata)=sm;
else
temp(isnan(temp)) = sm(isnan(temp));
clean(row,startdata:enddata) = temp;
end
else
clean(row,1:length(pa)) = pa;
end
end
%if it's a structure, put everything back where it's supposed to go!
if dostruct==1
for i=1:length(eyeStruct)
eyeStruct(i).pa = clean(i,1:length(eyeStruct(i).pa));
end
% cleanMat = clean; %save the matrix, it might be useful
clean = eyeStruct; %but return a matrix as the first output
% else
% cleanMat = []; %redundant if we don't have a structure
end
warning('on','all');
end