-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP6_LeftRightForce.m
384 lines (297 loc) · 11.2 KB
/
P6_LeftRightForce.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
function TrialData = P6_LeftRightForce(TrialData,Fthres,HSPlate,VTaxis,order,fc,pltflag)
%This function splits the force data into the Left and Right Foot for the
%HMRL treadmill (front-back split)
% TrialData = data structure for processing, must include processed
% forces
% HSPlate = index of which plate is the HS plate (1 when facing forward)
% Mnames = marker names of markers on the right and left foot (one marker
% per foot, order right left)
% order = order for filter (need to refilter to smooth out the
% transitions)
% fc = cutoff frequency for filter
% pltflag = flag for plotting (default is zero)
% Note this code requires gait events to be found prior to use, the code
% then only fills in the time when a valid stride has been found if there
% is no valid stride the force, cop and moment are equal to zero. As a
% result this function can only be used for walking, when no flight phase
% occurs.
%Originally Written By: Aaron N. Best (Apr 2023)
%Update Record:
%% Check if plot flag is provided
if nargin == 2
pltflag = 0;
end
%% Pull out the force data
if HSPlate == 1 %front plate is the HS plate
HS_F = TrialData.ForceData.Plate1.Force(:,:);
HS_CoP = TrialData.ForceData.Plate1.CoP(:,:);
HS_M = TrialData.ForceData.Plate1.Moment(:,:);
TO_F = TrialData.ForceData.Plate2.Force(:,:);
TO_CoP = TrialData.ForceData.Plate2.CoP(:,:);
TO_M = TrialData.ForceData.Plate2.Moment(:,:);
else %back plate is the HS plate
HS_F = TrialData.ForceData.Plate2.Force(:,:);
HS_CoP = TrialData.ForceData.Plate2.CoP(:,:);
HS_M = TrialData.ForceData.Plate2.Moment(:,:);
TO_F = TrialData.ForceData.Plate1.Force(:,:);
TO_CoP = TrialData.ForceData.Plate1.CoP(:,:);
TO_M = TrialData.ForceData.Plate1.Moment(:,:);
end
GaitEvents = (TrialData.ForceData.Plate1.FrameRate/TrialData.MarkerData.FrameRate)*TrialData.GaitEvents;
R_transition = NaN(length(GaitEvents(:,1)),2);
L_transition = NaN(length(GaitEvents(:,1)),2);
%% Split the force into right and left
R_Force = zeros(size(HS_F));
R_CoP = zeros(size(HS_F));
R_Moment = zeros(size(HS_F));
L_Force = zeros(size(HS_F));
L_CoP = zeros(size(HS_F));
L_Moment = zeros(size(HS_F));
RTOonplate = 0;
LHSonplate = 0;
for i = 1:length(GaitEvents(:,1))
start = GaitEvents(i,1);
fin = GaitEvents(i,5);
%A stride would have only been located if this was the case
RHSonplate = 1;
LTOonplate = 1;
for j = start:fin
%Phase 1 - Right on HS and Left on TO
if RHSonplate == 1 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 1
R_Force(j,:) = HS_F(j,:);
R_Moment(j,:) = HS_M(j,:);
R_CoP(j,:) = HS_CoP(j,:);
L_Force(j,:) = TO_F(j,:);
L_Moment(j,:) = TO_M(j,:);
L_CoP(j,:) = TO_CoP(j,:);
end
%Detect lift off of left foot
if HS_F(j,VTaxis) > Fthres && TO_F(j,VTaxis) < Fthres &&...
RHSonplate == 1 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 1
LTOonplate = 0;
end
%Phase 2 - Right on front plate, left in flight
if RHSonplate == 1 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 0
R_Force(j,:) = HS_F(j,:);
R_Moment(j,:) = HS_M(j,:);
R_CoP(j,:) = HS_CoP(j,:);
L_Force(j,:) = [0 0 0];
L_Moment(j,:) = [0 0 0];
L_CoP(j,:) = [0 0 0];
end
%Detect transition of the right foot to being on both plates
if HS_F(j,VTaxis) > Fthres && TO_F(j,VTaxis) > Fthres &&...
RHSonplate == 1 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 0
RTOonplate = 1;
R_transition(i,1) = j;
end
%Phase 3 - Right foot on both plates
if RHSonplate == 1 && RTOonplate == 1 &&...
LHSonplate == 0 && LTOonplate == 0
R_Force(j,:) = HS_F(j,:) + TO_F(j,:);
R_Moment(j,:) = HS_M(j,:) + TO_M(j,:);
R_CoP(j,:) = (norm(HS_F(j,:))*HS_CoP(j,:) + norm(TO_F(j,:))*TO_CoP(j,:))/(norm(HS_F(j,:)) + norm(TO_F(j,:))) ;
L_Force(j,:) = [0 0 0];
L_Moment(j,:) = [0 0 0];
L_CoP(j,:) = [0 0 0];
end
%Detect the right foot slipping onto the back plate
if HS_F(j,VTaxis) < Fthres && TO_F(j,VTaxis) > Fthres &&...
RHSonplate == 1 && RTOonplate == 1 &&...
LHSonplate == 0 && LTOonplate == 0
RHSonplate = 0;
R_transition(i,2) = j;
end
%Phase 4 - Right foot only on back plate
if RHSonplate == 0 && RTOonplate == 1 &&...
LHSonplate == 0 && LTOonplate == 0
R_Force(j,:) = TO_F(j,:);
R_Moment(j,:) = TO_M(j,:);
R_CoP(j,:) = TO_CoP(j,:);
L_Force(j,:) = [0 0 0];
L_Moment(j,:) = [0 0 0];
L_CoP(j,:) = [0 0 0];
end
%Detect the left foot landing back on the HS plate
if HS_F(j,VTaxis) > Fthres && TO_F(j,VTaxis) > Fthres &&...
RHSonplate == 0 && RTOonplate == 1 &&...
LHSonplate == 0 && LTOonplate == 0
LHSonplate = 1;
end
%Phase 5 - Right on TO plate and left on HS plate
if RHSonplate == 0 && RTOonplate == 1 &&...
LHSonplate == 1 && LTOonplate == 0
R_Force(j,:) = TO_F(j,:);
R_Moment(j,:) = TO_M(j,:);
R_CoP(j,:) = TO_CoP(j,:);
L_Force(j,:) = HS_F(j,:);
L_Moment(j,:) = HS_M(j,:);
L_CoP(j,:) = HS_CoP(j,:);
end
%Detect the right foot lifting off
if HS_F(j,VTaxis) > Fthres && TO_F(j,VTaxis) < Fthres &&...
RHSonplate == 0 && RTOonplate == 1 &&...
LHSonplate == 1 && LTOonplate == 0
RTOonplate = 0;
end
%Phase 6 - Left foot just on HS plate
if RHSonplate == 0 && RTOonplate == 0 &&...
LHSonplate == 1 && LTOonplate == 0
R_Force(j,:) = [0 0 0];
R_Moment(j,:) = [0 0 0];
R_CoP(j,:) = [0 0 0];
L_Force(j,:) = HS_F(j,:);
L_Moment(j,:) = HS_M(j,:);
L_CoP(j,:) = HS_CoP(j,:) ;
end
%Detect the left foot being on both plate
if HS_F(j,VTaxis) > Fthres && TO_F(j,VTaxis) > Fthres &&...
RHSonplate == 0 && RTOonplate == 0 &&...
LHSonplate == 1 && LTOonplate == 0
LTOonplate = 1;
L_transition(i,1) = j;
end
%Phase 7 - Left foot on both plates
if RHSonplate == 0 && RTOonplate == 0 &&...
LHSonplate == 1 && LTOonplate == 1
R_Force(j,:) = [0 0 0];
R_Moment(j,:) = [0 0 0];
R_CoP(j,:) = [0 0 0];
L_Force(j,:) = HS_F(j,:) + TO_F(j,:);
L_Moment(j,:) = HS_M(j,:) + TO_M(j,:);
L_CoP(j,:) = (norm(HS_F(j,:))*HS_CoP(j,:) + norm(TO_F(j,:))*TO_CoP(j,:))/(norm(HS_F(j,:)) + norm(TO_F(j,:))) ;
end
%Detect the left foot being fully on the back plate
if HS_F(j,VTaxis) < Fthres && TO_F(j,VTaxis) > Fthres &&...
RHSonplate == 0 && RTOonplate == 0 &&...
LHSonplate == 1 && LTOonplate == 1
LHSonplate = 0;
L_transition(i,2) = j;
end
%Phase 8 - Left foot only on the back plate
if RHSonplate == 0 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 1
R_Force(j,:) = [0 0 0];
R_Moment(j,:) = [0 0 0];
R_CoP(j,:) = [0 0 0];
L_Force(j,:) = TO_F(j,:);
L_Moment(j,:) = TO_M(j,:);
L_CoP(j,:) = TO_CoP(j,:) ;
end
%Detect if the right foot has landed back again
if HS_F(j,VTaxis) > Fthres && TO_F(j,VTaxis) > Fthres &&...
RHSonplate == 0 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 1
RHSonplate = 1;
end
%Phase 1 - Right on HS and Left on TO
if RHSonplate == 1 && RTOonplate == 0 &&...
LHSonplate == 0 && LTOonplate == 1
R_Force(j,:) = HS_F(j,:);
R_Moment(j,:) = HS_M(j,:);
R_CoP(j,:) = HS_CoP(j,:);
L_Force(j,:) = TO_F(j,:);
L_Moment(j,:) = TO_M(j,:);
L_CoP(j,:) = TO_CoP(j,:);
end
end
end
%% Refilter to smoot out the forces
%Find continous force sections
sets = [GaitEvents(1,1) NaN];
for i = 2:length(GaitEvents(:,1))
endcycle = GaitEvents(i-1,5);
begincycle = GaitEvents(i,1);
if endcycle-begincycle == 0
%do nothing
else
sets(end,2) = endcycle;
sets = [sets;begincycle NaN];
end
end
sets(end,2) = length(R_Force(:,1));
%Filter Setup
fs = TrialData.ForceData.Plate1.FrameRate;
wn = (fc/0.802)/(fs/2);
[b,a] = butter(order,wn);
%Filter the continuous section
for i = 1:length(sets(:,1))
R_Force(sets(i,1):sets(i,2),:) = filtfilt(b,a,R_Force(sets(i,1):sets(i,2),:));
R_Moment(sets(i,1):sets(i,2),:) = filtfilt(b,a,R_Moment(sets(i,1):sets(i,2),:));
R_CoP(sets(i,1):sets(i,2),:) = filtfilt(b,a,R_CoP(sets(i,1):sets(i,2),:));
L_Force(sets(i,1):sets(i,2),:) = filtfilt(b,a,L_Force(sets(i,1):sets(i,2),:));
L_Moment(sets(i,1):sets(i,2),:) = filtfilt(b,a,L_Moment(sets(i,1):sets(i,2),:));
L_CoP(sets(i,1):sets(i,2),:) = filtfilt(b,a,L_CoP(sets(i,1):sets(i,2),:));
end
%% Check the remove forces if the foot should be in flight phase
for i = 1:length(GaitEvents(:,1))
L_Flight = [GaitEvents(i,2), GaitEvents(i,3)];
R_Flight = [GaitEvents(i,4),GaitEvents(i,5)];
%Check left foot
for j = L_Flight(1):L_Flight(2)
if L_Force(j,VTaxis) ~= 0
L_Force(j,:) = [0 0 0];
L_Moment(j,:) = [0 0 0];
L_CoP(j,:) = [0 0 0];
end
end
%Check Right foot
for j = R_Flight(1):R_Flight(2)
if R_Force(j,VTaxis) ~= 0
R_Force(j,:) = [0 0 0];
R_Moment(j,:) = [0 0 0];
R_CoP(j,:) = [0 0 0];
end
end
end
%% Optional Plotting
if pltflag == 1
figure;
subplot(2,1,1);
hold on;
title('Right Force');
plot(R_Force(:,:));
for i = 1:length(GaitEvents)
line([GaitEvents(i,1) GaitEvents(i,1)], [-100 800],'Color','k');
line([GaitEvents(i,5) GaitEvents(i,5)], [-100 800],'Color','r');
end
legend('x','y','z');
hold off;
subplot(2,1,2);
hold on;
title('Left Force');
plot(L_Force(:,:));
for i = 1:length(GaitEvents)
line([GaitEvents(i,1) GaitEvents(i,1)], [-100 800],'Color','k');
line([GaitEvents(i,5) GaitEvents(i,5)], [-100 800],'Color','r');
end
legend('x','y','z');
hold off;
figure;
subplot(2,1,1);
hold on;
plot(R_CoP(:,1));
plot(L_CoP(:,1));
legend('Right','Left');
title('AP CoP');
hold off;
subplot(2,1,2);
hold on;
plot(R_CoP(:,3));
plot(L_CoP(:,3));
legend('Right','Left');
title('ML CoP');
hold off;
end
%% Pack into TrialData Structure
TrialData.ForceData.RightFoot.Force = R_Force;
TrialData.ForceData.RightFoot.CoP = R_CoP;
TrialData.ForceData.RightFoot.Moment = R_Moment;
TrialData.ForceData.LeftFoot.Force = L_Force;
TrialData.ForceData.LeftFoot.CoP = L_CoP;
TrialData.ForceData.LeftFoot.Moment = L_Moment;
end