Repository navigation
Expand file tree
/
Copy pathshiftCounts.m
More file actions
56 lines (51 loc) · 1.82 KB
/
Copy pathshiftCounts.m
File metadata and controls
56 lines (51 loc) · 1.82 KB
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
function shift_amount = shiftCounts(data)
%shiftCounts When to shift counts
if numel(data) > 0
cc_diff = diff(data);
shift_down = find(cc_diff >= -1.1 & cc_diff <= -0.9);
shift_up = find(cc_diff >= 0.9 & cc_diff <= 1.1);
shift_amount = zeros(size(data));
% % ignore multiple shift_ups or downs in a row
% shift_up(find(diff(shift_up) == 1)+1) = [];
% shift_down(find(diff(shift_down) == 1)+1) = [];
% ignore shifts that follow a trend
if numel(shift_up) > 0
shift_up_ignore = false(size(shift_up));
for k = 1:numel(shift_up)
if shift_up(k) > 3 && shift_up(k) < numel(cc_diff)-6
if sum(isoutlier(cc_diff(shift_up(k)-3:shift_up(k)+6))) == 0
shift_up_ignore(k) = true;
end
end
end
shift_up = shift_up(~shift_up_ignore);
end
if numel(shift_down) > 0 && numel(shift_up) > 0
for k = 1:numel(shift_down)
shift_amount(shift_down(k) + 1:end) = shift_amount(shift_down(k) + 1:end) - 1;
end
for k = 1:numel(shift_up)
shift_amount(shift_up(k) + 1:end) = shift_amount(shift_up(k) + 1:end) + 1;
end
else
if numel(shift_down) > 0 % only shifts down
for k = 1:numel(shift_down)
shift_amount(shift_down(k) + 1:end) = shift_amount(shift_down(k) + 1:end) - 1;
end
end
if numel(shift_up) > 0 % only shifts up
for k = 1:numel(shift_up)
shift_amount(shift_up(k) + 1:end) = shift_amount(shift_up(k) + 1:end) + 1;
end
end
end
% total shift
if data(1) > 0.9 && data(1) < 1.1
shift_amount = shift_amount + 1;
end
if data(1) > -1.1 && data(1) < -0.9
shift_amount = shift_amount - 1;
end
else
shift_amount = [];
end