-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPPClassificationTest.m
144 lines (109 loc) · 6.64 KB
/
CPPClassificationTest.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
clc;
clear all;
close all;
expdata = 'Multispeed_Walk_AB.mat';
tstart_matlab = 4000 + 1;
tend = 7000;
[gait_plain, act_index_plain] = SSE_classification_plain(expdata,tstart_matlab,tend);
%%
tstart_CPP = tstart_matlab - 1;
database_path = "../Data/DataBase_CSV";
experiment_path = "../Data/Experiment_CSV/Multispeed_Walk_AB.csv";
command_list = [database_path, experiment_path, num2str(tstart_CPP), num2str(tend)];
[~,~] = system("../build/SSEtrick " + join(command_list, " ") + " ../CPPResult/Test_Trick.csv");
assert(system("../build/SSEtrick " + join(command_list, " ") + " ../CPPResult/Test_Trick.csv")==0);
assert(system("../build/SSEtrickEigen " + join(command_list, " ") + " ../CPPResult/Test_Trick_Eigen.csv")==0);
assert(system("../build/SSEplain " + join(command_list, " ") + " ../CPPResult/Test_Plain.csv")==0);
assert(system("../build/SSEplainEigen " + join(command_list, " ") + " ../CPPResult/Test_Plain_Eigen.csv")==0);
%%
CPPResult_Trick = readmatrix("../CPPResult/Test_Trick.csv");
CPPResult_Plain = readmatrix("../CPPResult/Test_Plain.csv");
CPPResult_Trick_Eigen = readmatrix("../CPPResult/Test_Trick_Eigen.csv");
CPPResult_Plain_Eigen = readmatrix("../CPPResult/Test_Plain_Eigen.csv");
fprintf("Result check for non-Eigen and Eigen version of Trick method: %d\n",...
all(all(CPPResult_Trick - CPPResult_Trick_Eigen == 0)));
fprintf("Result check for non-Eigen and Eigen version of Plain method: %d\n",...
all(all(CPPResult_Plain - CPPResult_Plain_Eigen == 0)));
%%
figure(1)
clf;
tiledlayout(2,1);
nexttile;
hold on
plot(gait_plain,'LineWidth',10,'Color',[0.5 0.5 0.5 0.5],'DisplayName',"SSE Plain")
plot(CPPResult_Plain(:,1),'-','LineWidth',2,'DisplayName',"CPP Plain");
plot(CPPResult_Trick(:,1),'--','LineWidth',2,'DisplayName',"CPP Trick");
nexttile;
hold on
plot(act_index_plain,'LineWidth',10,'Color',[0.5 0.5 0.5 0.5],'DisplayName',"SSE Plain")
plot(CPPResult_Plain(:,2),'-','LineWidth',2,'DisplayName',"CPP Plain");
plot(CPPResult_Trick(:,2),'--','LineWidth',2,'DisplayName',"CPP Trick");
legend
%%
function [phase_prediction, activity_index] = SSE_classification_plain(experimentdata,tstart,tend)
Slow_AB = ReadYaml('Slow_AB_Reference.yaml');
Med_AB = ReadYaml('Med_AB_Reference.yaml');
Fast_AB = ReadYaml('Fast_AB_Reference.yaml');
RA_AB = ReadYaml('RA_AB_Reference.yaml');
RD_AB = ReadYaml('RD_AB_Reference.yaml');
SA_AB = ReadYaml('SA_AB_Reference.yaml');
SD_AB = ReadYaml('SD_AB_Reference.yaml');
acts = {'Slow_AB'; 'Med_AB'; 'Fast_AB'; 'RA_AB'; 'RD_AB'; 'SA_AB'; 'SD_AB'};
reslts = {'Slow'; 'Med'; 'Fast'; 'RA'; 'RD'; 'SA'; 'SD'};
activities = ["Walking 0.6 m/s", "Walking 0.8 m/s", "Walking 1.0 m/s", "Ramp Ascent", "Ramp Descent", "Stair Ascent", "Stair Descent"];
subs_kernal = fieldnames(Slow_AB);
subs_data = {'RThighAng'; 'LThighAng'; 'RShankAng'; 'LShankAng'};
% Creating a struct to conviniently house all kernals:
for a=1:numel(acts)
for s=1:numel(subs_kernal)
kernal.(acts{a}).(subs_kernal{s}) = cell2mat(eval([(acts{a}), '.', (subs_kernal{s})]))';
double_kernal.(acts{a}).(subs_kernal{s}) = [kernal.(acts{a}).(subs_kernal{s}); kernal.(acts{a}).(subs_kernal{s})];
end
end
% Creating a struct for the incoming data:
collected_data = load(experimentdata);
for s=1:numel(subs_data)
full_data_set.(subs_kernal{s+1}) = collected_data.FullStudy.(subs_data{s});
end
% Initialize a bunch of stuff here to be zeros before the loop starts:
time_considered = tstart:tend; % 1:length(full_data_set.RThigh)
for s = 2:numel(subs_kernal)
data_sub_window.(subs_kernal{s}) = zeros(600, 1); % this is just a moving subset of the incoming data, initialized to be all zeros in the beginning
for a = 1:numel(reslts)
% These are (time x n) dimension matrices. Each cell corresponds to
% an SSE comparison between kernal and data. As you move columns,
% you are sliding the kernal relative to the data. As you move
% rows, you change time instances. SSE comparisons are the sum of
% the SSE from each RThigh, LThigh, RShank, LShank.
results.(reslts{a}).SSE_vector.sum = zeros(length(time_considered), length(kernal.(acts{a}).(subs_kernal{s})));
end
end
% Run the time based for loop here:
% for t = 1:length(full_data_set.LShank)
for t = 1:length(time_considered)
% Pretend like new data is coming in to the sub window for each leg segment:
for s = 2:numel(subs_kernal)
data_sub_window.(subs_kernal{s}) = [data_sub_window.(subs_kernal{s}); full_data_set.(subs_kernal{s})(time_considered(t))];
data_sub_window.(subs_kernal{s})(1) = [];
end
for a = 1:3 %:numel(reslts) % For each kernal
data_window_kernal_size.(reslts{a}).RThigh = data_sub_window.RThigh(end-length(kernal.(acts{a}).RThigh)+1:end);
data_window_kernal_size.(reslts{a}).LThigh = data_sub_window.LThigh(end-length(kernal.(acts{a}).LThigh)+1:end);
data_window_kernal_size.(reslts{a}).RShank = data_sub_window.RShank(end-length(kernal.(acts{a}).RShank)+1:end);
data_window_kernal_size.(reslts{a}).LShank = data_sub_window.LShank(end-length(kernal.(acts{a}).LShank)+1:end);
for n = 1:length(kernal.(acts{a}).percent) % For each sliding configuration
results.(reslts{a}).SSE_vector.sum(t, n) = sum(((data_sub_window.RThigh(end-length(kernal.(acts{a}).RThigh)+1:end)) - (double_kernal.(acts{a}).RThigh(n:n-1+length(kernal.(acts{a}).RThigh)))).^2) + ...
sum(((data_sub_window.LThigh(end-length(kernal.(acts{a}).LThigh)+1:end)) - (double_kernal.(acts{a}).LThigh(n:n-1+length(kernal.(acts{a}).LThigh)))).^2) + ...
sum(((data_sub_window.RShank(end-length(kernal.(acts{a}).RShank)+1:end)) - (double_kernal.(acts{a}).RShank(n:n-1+length(kernal.(acts{a}).RShank)))).^2) + ...
sum(((data_sub_window.LShank(end-length(kernal.(acts{a}).LShank)+1:end)) - (double_kernal.(acts{a}).LShank(n:n-1+length(kernal.(acts{a}).LShank)))).^2);
end
results.(reslts{a}).min_SSE(t) = min(results.(reslts{a}).SSE_vector.sum(t, :));
end
[~, results.activity_index(t)] = min([results.Slow.min_SSE(t), results.Med.min_SSE(t), results.Fast.min_SSE(t)]); %, results.RA.min_SSE, results.RD.min_SSE, results.SA.min_SSE, results.SD.min_SSE]);
[~, results.gait_index(t)] = min(results.(reslts{results.activity_index(t)}).SSE_vector.sum(t, :));
results.phase_prediction(t) = results.gait_index(t)/length(kernal.(acts{results.activity_index(t)}).percent);
results.activity_prediction(t) = activities(results.activity_index(t));
end
phase_prediction = results.phase_prediction;
activity_index = results.activity_index;
end