-
Notifications
You must be signed in to change notification settings - Fork 2
/
ALS_calculate_pitch_metric_duration.m
123 lines (103 loc) · 4.52 KB
/
ALS_calculate_pitch_metric_duration.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
function duration = ALS_calculate_pitch_metric_duration(data, option, min_angle, top)
% data ~ angle (pitch or roll)
% option = 1 - flexion only
% option = 2 - extension only
% option = 3 - flexion and extension
% min_angle = angle of flexion or extension
% top = number of fastest moves (default = 5);
% duration = average activity duration
fs = 10;
data_diff = diff(data);
data_diff(data_diff < 0.5 & data_diff > -0.5) = 0;
data_sign = sign(data_diff);
duration = [];
if option == 1 % flexion only
% Flexion & supination
[running_length, move_duration, starting_index] = RunLength(data_sign);
move_duration(running_length<1)=[];
starting_index(running_length<1)=[];
running_length(running_length<1)=[];
MinMax = nan(numel(running_length),4);
for i = 1:numel(running_length)
MinMax(i,:) = [data(starting_index(i)), data(starting_index(i)+move_duration(i)), starting_index(i), move_duration(i)];
end
% clear too small angles
% MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle - margin | abs(MinMax(:, 2) - MinMax(:, 1)) > min_angle + margin, :) = [];
MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle, :) = [];
if ~isempty(MinMax)
duration_temp = sort(MinMax(:,4), 'ascend');
if size(MinMax, 1) >= top
duration_temp = duration_temp(1:top);
end
else
duration_temp = NaN;
end
duration = [duration; duration_temp];
elseif option == 2 % extension only
% Extension & pronation
[running_length, move_duration, starting_index] = RunLength(data_sign);
move_duration(running_length>-1)=[];
starting_index(running_length>-1)=[];
running_length(running_length>-1)=[];
MinMax = nan(numel(running_length),4);
for j = 1:numel(running_length)
MinMax(j,:) = [data(starting_index(j)), data(starting_index(j)+move_duration(j)), starting_index(j), move_duration(j)];
end
% clear too small or too large angles
% MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle - margin | abs(MinMax(:, 2) - MinMax(:, 1)) > min_angle + margin, :) = [];
MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle, :) = [];
if ~isempty(MinMax)
duration_temp = sort(MinMax(:,4), 'ascend');
if size(MinMax, 1) >= top
duration_temp = duration_temp(1:top);
end
else
duration_temp = NaN;
end
duration = [duration; duration_temp];
elseif option == 3 % flexion and extension / supination and pronation
% Flexion / supination
[running_length, move_duration, starting_index] = RunLength(data_sign);
move_duration(running_length<1)=[];
starting_index(running_length<1)=[];
running_length(running_length<1)=[];
MinMax = nan(numel(running_length),4);
for i = 1:numel(running_length)
MinMax(i,:) = [data(starting_index(i)), data(starting_index(i)+move_duration(i)), starting_index(i), move_duration(i)];
end
% clear too small or too large angles
% MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle - margin | abs(MinMax(:, 2) - MinMax(:, 1)) > min_angle + margin, :) = [];
MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle, :) = [];
if ~isempty(MinMax)
duration_temp = sort(MinMax(:,4), 'ascend');
if size(MinMax, 1) >= top
duration_temp = duration_temp(1:top);
else
end
else
duration_temp = NaN;
end
duration = [duration; duration_temp];
% Extension / pronation
[running_length, move_duration, starting_index] = RunLength(data_sign);
move_duration(running_length>-1)=[];
starting_index(running_length>-1)=[];
running_length(running_length>-1)=[];
MinMax = nan(numel(running_length),4);
for j = 1:numel(running_length)
MinMax(j,:) = [data(starting_index(j)), data(starting_index(j)+move_duration(j)), starting_index(j), move_duration(j)];
end
% clear too small or too large angles
% MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle - margin | abs(MinMax(:, 2) - MinMax(:, 1)) > min_angle + margin, :) = [];
MinMax(abs(MinMax(:, 2) - MinMax(:, 1)) < min_angle, :) = [];
if ~isempty(MinMax)
duration_temp = sort(MinMax(:,4), 'ascend');
if size(MinMax, 1) >= top
duration_temp = duration_temp(1:top);
end
else
duration_temp = NaN;
end
duration = [duration; duration_temp];
end
duration = mean(duration, 'omitnan')/fs;