-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresultsAnalysisJan19.m
316 lines (281 loc) · 8.85 KB
/
resultsAnalysisJan19.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
% Results analysis jan 19 exercises
clc
clear all
close all
files = dir('../analysis/jan19/*.csv');
% Set some run parameters
plotSkipping = 30;
freq = 60; % Frequency for device and ART data from LabVIEW. Should be 60 Hz, but might not be perfect.
% Runtime parameters
accScale = 8192; % Conversion parameter for accelerometer
gyrScale = 16.4; % Conversion parameter for gyroscope
load('BITcalibration20170125.mat')
%% Load data and clip to proper start and end
afile = files(1).name;
data = csvread(strcat(files(1).folder,'/',afile));
data = data(2243:7125,:); % Clip data
% Parse data and decide to reject some error tracked points
checkZero = data(:,28:36) == 0; % Detect zero values
checkZero = any(checkZero,2); % Compress into a vector. 1 indicates a zero is present
data = data(~checkZero,:);
[time, acc, gyr, quat, omron, arm, forearm, wrist] = parseData(data);
plotJoints(arm,forearm,wrist,afile)
distanceWristArm = sqrt(sum((wrist-arm).^2,2)); % Distance values
X = [omron acc gyr quat];
y1 = distanceWristArm;
data1 = [y1 X];
accIn = acc./accScale;
gyrIn = gyr - gyrCal;
gyrIn = gyrIn./gyrScale;
pos1 = deadReckonMadgwickOscillationFunc(accIn,gyrIn,freq,0.1,0);
afile = files(2).name;
data = csvread(strcat(files(1).folder,'/',afile));
data = data(1149:6344,:);
checkZero = data(:,28:36) == 0; % Detect zero values
checkZero = any(checkZero,2); % Compress into a vector. 1 indicates a zero is present
data = data(~checkZero,:);
[time, acc, gyr, quat, omron, arm, forearm, wrist] = parseData(data);
plotJoints(arm,forearm,wrist,afile)
distanceWristArm = sqrt(sum((wrist-arm).^2,2)); % Distance values
X = [omron acc gyr quat];
y2 = distanceWristArm;
data2 = [y2 X];
accIn = acc./accScale;
gyrIn = gyr - gyrCal;
gyrIn = gyrIn./gyrScale;
pos2 = deadReckonMadgwickOscillationFunc(accIn,gyrIn,freq,0.1,0);
% Trial 3 inspection
afile = files(3).name;
data = csvread(strcat(files(1).folder,'/',afile));
data = data(1121:6179,:);
checkZero = data(:,28:36) == 0; % Detect zero values
checkZero = any(checkZero,2); % Compress into a vector. 1 indicates a zero is present
data = data(~checkZero,:);
[time, acc, gyr, quat, omron, arm, forearm, wrist] = parseData(data);
plotJoints(arm,forearm,wrist,afile)
distanceWristArm = sqrt(sum((wrist-arm).^2,2)); % Distance values
X = [omron acc gyr quat];
y3 = distanceWristArm;
data3 = [y3 X];
accIn = acc./accScale;
gyrIn = gyr - gyrCal;
gyrIn = gyrIn./gyrScale;
pos3 = deadReckonMadgwickOscillationFunc(accIn,gyrIn,freq,0.1,0);
% Combine our datasets
datas = {data1 data2 data3};
positions = {pos1 pos2 pos3};
M = length(datas);
% Check if data sets overlap sufficiently
figure()
hold on
plot(y1)
plot(y2)
plot(y3)
hold off
title('Compare wrist-arm distances in multiple trials')
xlabel('time')
ylabel('distance (mm)')
legend('1','2','3')
%% Dead Reckon with Madgwick Algorithm
% Data Prep (Always pass clean data to algorithm!)
% gyrCalStationary = [-2.172099087 1.585397653 2.456323338];
load('BITcalibration20170125.mat')
accIn = acc./accScale;
gyrIn = gyr - gyrCal;
gyrIn = gyrIn./gyrScale;
pos3 = deadReckonMadgwickOscillationFunc(accIn,gyrIn,freq,0.1,0);
figure()
subplot(1,2,1)
hold on
plot(pos3(:,1))
plot(pos3(:,2))
plot(pos3(:,3))
ylabel('Position (m)')
xlabel('Time (s)')
title('Dead Reckon Position')
hold off
subplot(1,2,2)
hold on
plot(wrist(:,1)/1e3)
plot(wrist(:,2)/1e3)
plot(wrist(:,3)/1e3)
ylabel('Position (m)')
xlabel('Time (s)')
title('Motion Tracker Wrist Position')
hold off
1
% Output data to file
% csvwrite('data1.csv',data)
% csvwrite('position1.csv',pos3)
%% Build Model - Regression Tree
% After creating model in Regression Learner
% Train and test on a single recording to see results
% [trainedModel, validationRMSE] = trainRegressionModel(data1);
% x_train = data1(:,2:size(data1,2));
% ypred = trainedModel.predictFcn(x_train);
% y_test = data1(:,1);
% trainRMSE = sqrt( sum((ypred - y_test).^2) ./ length(ypred));
% sprintf('Train RMSE error is %.2f',trainRMSE)
%
% figure()
% hold on
% plot(ypred)
% plot(data1(:,1))
% legend('predict','real')
% ylabel('Normalized Distance')
% xlabel('Time (s)')
% hold off
% title('Validation of model with training data')
%% Check against other dataset
% test = matNorm(data1);
y_test = data2(:,1);
% x_test = test(:,2:end);
ypred = trainedModel.predictFcn(data2(:,2:size(data2,2)));
figure()
hold on
plot(ypred)
plot(y_test)
legend('predict','real')
hold off
title('Check against test data (unseen)')
testRMSE = sqrt( sum((ypred - y_test).^2) ./ length(ypred));
testSpaceDistance = max(y_test) - min(y_test);
testErrorRelative = testRMSE / testSpaceDistance;
sprintf('Test RMSE error is %.2f',testRMSE)
%% Try with normalized data
setTrain = [data3 ;data2];
setTest = data1;
y_testmm = setTest(:,1); % Original value in mm
normStats = {mean(data1) max(data1) min(data1)};
setTrain = ((setTrain - normStats{1}) ./ (normStats{2} - normStats{3}));
setTest = ((setTest - normStats{1}) ./ (normStats{2} - normStats{3}));
x_train = setTrain(:,2:end);
y_train = setTrain(:,1);
x_test = setTest(:,2:end);
y_test = setTest(:,1);
% Regression Tree
[trainedModel, validationRMSE] = trainRegressionModel(setTrain);
y_pred = trainedModel.predictFcn(x_test);
y_predmm = y_pred .* (normStats{2}(1) - normStats{3}(1)) + normStats{1}(1); % Transform back into mm
testRMSE = sqrt( sum((y_pred - y_test).^2) ./ length(y_test)); % Normalized Error
testSpaceDistance = max(y_testmm) - min(y_testmm);
testErrorRelative = testRMSE / testSpaceDistance;
sprintf('Regression Tree. Training RMSE error is %.4f',validationRMSE)
sprintf('Regression Tree. Normalied Test RMSE error is %.4f',testRMSE)
errormm = y_predmm - y_testmm;
testRMSEmm = sqrt( sum((y_predmm - y_testmm).^2) ./ length(y_test)); % mm error
sprintf('Regression Tree. Test RMSE error is %.4f mm',testRMSEmm)
%% Support Vector Machine
Mdl = fitrsvm(x_train,y_train);
Mdl.ConvergenceInfo.Converged % Check model convergence
y_predSVR = Mdl.predict(x_test);
y_predSVRmm = y_predSVR .* (normStats{2}(1) - normStats{3}(1)) + normStats{1}(1);
errorSVR = y_predSVRmm - y_testmm;
testRMSEmm = sqrt( sum((errorSVR).^2) ./ length(y_test)); % mm error
sprintf('SVR. Test RMSE error is %.4f mm. Mean %.2f',testRMSEmm,mean(abs(errorSVR)))
figure()
hold on
plot(y_predSVRmm)
plot(y_testmm)
hold off
legend('Predict','Real')
title('SVR')
%% Loop Through data and apply fine tree regression learning
% for i=1:M
% % Construct our data sets
% testData = datas{i};
% ytestmm = testData(:,1); % Test data in mm
% trainData = [];
% for j = 1:M
% if j ~= i
% trainData = [trainData; datas{j}];
% end
% end
% % Train model
% normStats = {mean(trainData) max(trainData) min(trainData)};
% trainData = (trainData - mean(trainData)) ./ (max(trainData) - min(trainData));
% testData = (testData - mean(trainData)) ./ (max(trainData) - min(trainData));
% [trainedModel, validationRMSE] = trainRegressionModel(data1);
%
%
% % Test Model
% ypred = trainedModel.predictFcn(testData(:,2:end));
% % Convert prediction back into mm values
% ypredmm = ypred * (max(trainData) - min(trainData)) + mean(trainData);
%
% % Test error calculation
% testError = ypredmm - ytestmm;
%
% figure()
% hold on
% plot(ypredmm)
% plot(ytestmm)
% legend('prediction','real')
% title('Result plot')
% hold off
%
%
% validationRMSE
% % MSE calculation
% mean(sum(testError.^2))
%
% end
%% End of Script
%% Functions
function distance = dist3D(A,B)
% Distance between two matrices. Checks for zero values
end
function dataOut = matNorm(data)
% Columnwise norm of matrix
dataOut = (data - mean(data)) ./ (max(data) - min(data));
end
function plotJoints(arm,forearm,wrist,caption)
% Plots ART joint data
if nargin > 3
pltTitle = caption;
else
pltTitle = '3D plot';
end
circSize = 1;
figure()
hold on
scatter3(arm(:,1),arm(:,2),arm(:,3),circSize,'r')
scatter3(forearm(:,1),forearm(:,2),forearm(:,3),circSize,'b')
scatter3(wrist(:,1),wrist(:,2),wrist(:,3),circSize,'g')
hold off
xlabel('x')
ylabel('y')
zlabel('z')
legend('arm','forearm','wrist')
title(pltTitle)
end
function [time, acc, gyr, quat, omron, arm, forearm, wrist] = parseData(data)
% Plots ART and proto IMU/omron data
time = data(:,1);
omron = data(:,2:17);
acc = data(:,18:20);
gyr = data(:,21:23);
quat = data(:,24:27);
arm = data(:,28:30);
forearm = data(:,31:33);
wrist = data(:,34:36);
end
function plotArmPosition(wrist,forearm,arm)
figure()
hold on
plot(wrist(:,1))
plot(wrist(:,2))
plot(wrist(:,3))
plot(forearm(:,1))
plot(forearm(:,2))
plot(forearm(:,3))
plot(arm(:,1))
plot(arm(:,2))
plot(arm(:,3))
plot(distanceWristArm,'--or')
hold off
title('Compare tracker positions')
xlabel('time')
ylabel('Position (mm)')
legend('w1','w2','w3','f1','f2','f3','a1','a2','a3','wrist dist')
end