-
Notifications
You must be signed in to change notification settings - Fork 0
/
support_calcInterpolRatios.m
58 lines (42 loc) · 2.1 KB
/
support_calcInterpolRatios.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
function interpolRatios = support_calcInterpolRatios(ts, origSamplesTs, triggersForAlignment, srate, origSrate)
% TODO: calculate with better precision, also considering original srate?
interpolRatios = nan(length(triggersForAlignment),1);
for v = 1:length(triggersForAlignment)
% E.g. when we are making a response-aligned analysis, and the subject has no response in a trial (or in the next trial !)
if isnan(triggersForAlignment(v)) || ...
(v < length(triggersForAlignment) && isnan(triggersForAlignment(v+1)))
interpolRatios(v) = NaN;
continue;
end
actualFromSample = find(ts >= triggersForAlignment(v), 1, 'first');
if length(actualFromSample) ~= 1
actualFromSample = NaN
end
if v < length(triggersForAlignment)
actualToSample = find(ts <= triggersForAlignment(v+1), 1, 'last');
% invalid if the whole duration of this trial is before the
% beginning of the recording
if actualToSample == 1
actualTosample = NaN;
end
else
actualToSample = length(ts);
end
% % % actualFromSample
% % % actualToSample
% % % ~isnan(actualFromSample)
% % % ~isnan(actualToSample)
if ~isnan(actualFromSample) && ~isnan(actualToSample)
actualLen = actualToSample - actualFromSample + 1;
% IMPORTANT: we cannot beleive that the original timestamps and
% samples vectors really contained all the needed samples
if v < length(triggersForAlignment)
theorLen = ceil( (triggersForAlignment(v+1) - triggersForAlignment(v) +1 ) / 1000 /1000 * srate );
else
theorLen = ceil( (origSamplesTs(end) - triggersForAlignment(v) +1 ) / 1000 /1000 * srate );
end
interpolRatios(v) = (1-(actualLen / theorLen)) *100;
end
end
% check1 = length(ts) == find(ts >= triggersForAlignment(end), 1, 'last');
end