-
Notifications
You must be signed in to change notification settings - Fork 0
/
strint_lh_step2_FC.m
301 lines (268 loc) · 7.82 KB
/
strint_lh_step2_FC.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
%% This loads in the .mat file generated in step 1
% will process the time-series force channel data
% Force data are filtered in step 1 per BKIN's c3d_filter, just need to
% resample the data to 1000ms and create within subject averages for the
% exposure blocks
clear all
close all
% Select the subject file
str = computer;
if strcmp(str,'MACI64') == 1
cd('/Volumes/mnl/Data/Adaptation/structural_interference/Post_Step_1_test');
fname = uigetfile('*lh.mat');
fname = fname(1:end-4);
fcTrials = xlsread('/Volumes/mnl/Data/Adaptation/structural_interference/Force_Channel_Trials_12_03_16.xlsx','Sheet1');
else
cd('Z:\Data\Adaptation\structural_interference\Post_Step_1_test\');
fname = uigetfile('*lh.mat');
fname = fname(1:end-4);
fcTrials = xlsread('Z:\Data\Adaptation\structural_interference\Force_Channel_Trials_12_03_16.xlsx','Sheet1');
end
load([fname '.mat']);
subID = fname(12:13);
group = fname(8:10);
numTrials = size(sortData,1); % Number of Trials
% toss out trials 41 and 42, these are transition trials between
% kinesthetic and kin+rotation
wrong_trial(41) = 1;
wrong_trial(42) = 1;
% Write a boolean for the force channel trials
channel_trial = zeros(1,numTrials);
channel_trial(fcTrials) = 1;
% Find the "Up" and "Down" trials
upBool = zeros(numTrials,1);
for i = 1:numTrials
upBool(i) = sortData(i).TRIAL.TP == 1 || sortData(i).TRIAL.TP == 3 || sortData(i).TRIAL.TP == 5;
end
upBool = upBool';
upTrials = find(upBool == 1); % Trial numbers of "up" targets
upTrials = upTrials';
downTrials = find(upBool == 0);
downTrials = downTrials';
numDataPoints = zeros(numTrials,1);
for i = 1:numTrials
numDataPoints(i) = size(sortData(i).Left_HandX,1); % Number of Data points in each trial
end
%% Plot FC data, then take averages for the blocks FOR FORCE SENSOR
forceTS = cell(numTrials,1);
% forceTS includes the force command data for ALL trials from movement
% onset to offset
for i = 1:numTrials
if wrong_trial(i) == 0
forceTS{i,1} = sortData(i).Left_FS_ForceX(onset(i):offset(i));
else
forceTS{i,1} = NaN;
end
end
% Resample so each trial lasts 1000ms (USING interp1)
forceTSRS = zeros(numTrials,1000);
for i = 1:numTrials
if wrong_trial(i) == 0
x = 1:1:length(forceTS{i,1});
y = forceTS{i,1};
xi = 1:length(x)/1000:length(x);
temp = interp1(x,y,xi);
if length(temp) == 1000
forceTSRS(i,:) = temp;
else
padlen = 1000 - length(temp); % Some really short trials (<500 ms) get extrapolated to only 998 or even 997 trials. This will find out how 'short' the extrap is, and pads with the appropriate number of the end of the trial.
pad = repmat(temp(end),1,padlen);
forceTSRS(i,:) = [temp pad]; % Trials that last <1000 ms only get extrapolated to 999 samples. This fills in the 1000th value with a repeat of the 999th.
end
else
forceTSRS(i,:) = NaN;
end
end
% Rectify
forceTSRS = abs(forceTSRS);
% Average the blocks
temp = zeros(numTrials,1000);
for i = 1:numTrials
if wrong_trial(i) == 0
temp(i,:) = forceTSRS(i,:); % makes a 222x1000 matrix containing the FC data
else
temp(i,:) = NaN;
end
end
bk1 = temp(fcTrials(13:19),:);
bk1mean = nanmean(bk1,1);
bk2 = temp(fcTrials(20:26),:);
bk2mean = nanmean(bk2,1);
bk3 = temp(fcTrials(27:33),:);
bk3mean = nanmean(bk3,1);
bk4 = temp(fcTrials(34:40),:);
bk4mean = nanmean(bk4,1);
clear temp
% Find the force time series data for only FC trials (export this one!)
forceFS = zeros(length(fcTrials),1000);
for i = 1:length(fcTrials)
forceFS(i,:) = forceTSRS(fcTrials(i),:);
end
clear temp
% figure
% subplot(2,4,1)
% for i = 13:19
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 1 - FS'])
%
% subplot(2,4,2)
% for i = 20:26
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 2'])
%
% subplot(2,4,3)
% for i = 27:33
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 3'])
%
% subplot(2,4,4)
% for i = 34:40
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 4'])
%
% subplot(2,4,5)
% plot(bk1mean)
%
% subplot(2,4,6)
% plot(bk2mean)
%
% subplot(2,4,7)
% plot(bk3mean)
%
% subplot(2,4,8)
% plot(bk4mean)
%% Plot FC data, then take averages for the blocks FOR CMD
forceTS = cell(numTrials,1);
% forceTS includes the force command data for ALL trials from movement
% onset to offset
for i = 1:numTrials
if wrong_trial(i) == 0
forceTS{i,1} = sortData(i).Left_Hand_ForceCMD_X(onset(i):offset(i));
else
forceTS{i,1} = NaN;
end
end
% Resample so each trial lasts 1000ms (USING interp1)
forceTSRS = zeros(numTrials,1000);
for i = 1:numTrials
if wrong_trial(i) == 0
x = 1:1:length(forceTS{i,1});
y = forceTS{i,1};
xi = 1:length(x)/1000:length(x);
temp = interp1(x,y,xi);
if length(temp) == 1000
forceTSRS(i,:) = temp;
else
padlen = 1000 - length(temp); % Some really short trials (<500 ms) get extrapolated to only 998 or even 997 trials. This will find out how 'short' the extrap is, and pads with the appropriate number of the end of the trial.
pad = repmat(temp(end),1,padlen);
forceTSRS(i,:) = [temp pad]; % Trials that last <1000 ms only get extrapolated to 999 samples. This fills in the 1000th value with a repeat of the 999th.
end
else
forceTSRS(i,:) = NaN;
end
end
% Rectify
forceTSRS = abs(forceTSRS);
% Average the blocks
temp = zeros(numTrials,1000);
for i = 1:numTrials
if wrong_trial(i) == 0
temp(i,:) = forceTSRS(i,:); % makes a 222x1000 matrix containing the FC data
else
temp(i,:) = NaN;
end
end
bk1 = temp(fcTrials(13:19),:);
bk1mean = nanmean(bk1,1);
bk2 = temp(fcTrials(20:26),:);
bk2mean = nanmean(bk2,1);
bk3 = temp(fcTrials(27:33),:);
bk3mean = nanmean(bk3,1);
bk4 = temp(fcTrials(34:40),:);
bk4mean = nanmean(bk4,1);
clear temp
% Find the force time series data for only FC trials (export this one!)
% Find the force time series data for only FC trials (export this one!)
forceCMD = zeros(length(fcTrials),1000);
for i = 1:length(fcTrials)
forceCMD(i,:) = forceTSRS(fcTrials(i),:);
end
clear temp
% figure
% subplot(2,4,1)
% for i = 13:19
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 1 - CMD'])
%
% subplot(2,4,2)
% for i = 20:26
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 2'])
%
% subplot(2,4,3)
% for i = 27:33
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 3'])
%
% subplot(2,4,4)
% for i = 34:40
% hold on
% if wrong_trial(fcTrials(i)) == 0
% plot(forceTSRS(fcTrials(i),:))
% end
% end
% title([subID,' ','EX block 4'])
%
% subplot(2,4,5)
% plot(bk1mean)
%
% subplot(2,4,6)
% plot(bk2mean)
%
% subplot(2,4,7)
% plot(bk3mean)
%
% subplot(2,4,8)
% plot(bk4mean)
temp = zeros(48,1);
temp(:,1) = str2num(subID);
subjectID = temp;
wrong_trial_export = wrong_trial(fcTrials);
upBool_export = upBool(fcTrials)';
%% Data Export
%switch Directory
if strcmp(str,'MACI64') == 1
cd(['/Volumes/mnl/Data/Adaptation/structural_interference/Post_Step_2_test_FC']);
else
cd(['Z:\Data\Adaptation\structural_interference\Post_Step_2_test_FC']);
end
save([fname(1:18) '_postStep2_lh_fc_FS' '.mat'],'subjectID', 'upBool_export', 'wrong_trial_export', 'forceFS')
save([fname(1:18) '_postStep2_lh_fc_CMD' '.mat'],'subjectID', 'upBool_export', 'wrong_trial_export', 'forceCMD')