-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpolate_blinks_nonlinear_v01.m
70 lines (49 loc) · 2.51 KB
/
interpolate_blinks_nonlinear_v01.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
function [d, blink_indx] = interpolate_blinks_nonlinear_v01(d, blink, samplingrate, blinkWindow)
% function [d, blink_indx] = interpolate_blinks_nonlinear_v01(d, blink, samplingrate, blinkWindow)
%
% Interpolate blinks in 1D pupil size measures.
%
% d: vector of pupil size measures
% blink: how is a blink defined in the data, usually 0, this is not an index vector! Typically, everything is interpolated when the signal is 0.
% samplingrate: samplingrate of the eyetracker
% blinkWindow: how many seconds should be removed before and after a blink?
% blink_indx: Where are blinks?
% The function depends on MergeBrackets.m
%
% Author: Antonius Wiehler <[email protected]>
% Original: 2017-01-11
% Modified: 2018-09-13
% mirror signal to avoid border artefacts - if the data starts or ends with a blink, the non linear interpolation can go wild.
d = [flipud(d); d; flipud(d)];
sampleLength = 1 ./ samplingrate; % how long is one sample in seconds?
blink_samples = ceil(blinkWindow ./ sampleLength); % how many samples do we have to remove before and after a blink?
blink_indx = d == blink;
blink_position = [0; blink_indx; 0]; % where is the pupil diameter a blink?
blink_start = find(diff(blink_position) == 1); % where do the blinks start?
blink_end = find(diff(blink_position) == -1) -1; % where do blinks end?
blink_start = blink_start - blink_samples; % add window at beginning of blink
blink_end = blink_end + blink_samples; % add window at end of blink
% keep only legal values
blink_start(blink_start < 1) = 1;
blink_end(blink_end > length(d)) = length(d);
[blink_start, blink_end] = MergeBrackets(blink_start, blink_end); % Merge overlapping blinks (blinks can be overlapping due to the additional window
X = (1 : length(d))'; % intext for interpolation
for i_b = 1 : length(blink_start) % loop through blinks
X(blink_start(i_b) : blink_end(i_b)) = nan;
end
xi = find(isnan(X)); % which samples need to be interpolated?
d(isnan(X)) = []; % remove to be interpolated data
X(isnan(X)) = []; % remove to be interpolated data
di = interp1(X, d, xi, 'pchip'); % interpolate
% figure;plot(X, d, '.'); hold on; plot(xi, di, '.'); ylim([0 40]); % plot to evaluate the interpolation
% join real and interpolated data and sort
X = [X; xi];
[X, sort_i] = sort(X);
d = [d; di];
d = d(sort_i);
% unmirror the signal again
ld = length(d);
third = ceil(ld ./ 3); % for odd number of bit-stream length
d = d(third+1 : third.*2);
blink_indx = blink_indx(third+1 : third.*2);
end