-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnmf0510.m
298 lines (261 loc) · 9.5 KB
/
nnmf0510.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
%%
clear
close all
clc
set(0, 'defaulttextinterpreter', 'latex');
set(0, 'defaultlegendinterpreter', 'latex');
set(groot, 'defaultAxesTickLabelInterpreter', 'latex');
set(groot, 'defaultLegendInterpreter', 'latex')
%% Settings
path='D:\University\MSc\Thesis project\Gait Data\ProcessdataMat\';
sub=5;
% trial=1:5;
syn=5;
matName = 'P';
load([path,matName]);
% eval(['P',matName]);
option.devideByStdFlag = 1;
option.VAF_th = 0.9; % total VAF thereshold
option.vaf_th = 0.85; % VAF thereshold for each muscle
option.corr_th = 0.8;
option.rsq_th = 0.6;
option.RSQ_th = 0.9;
option.rsqCI_th = 0.99;
option.vafCI_th = 0.95;
option.n_bootstrap = 1000;
option.condition = 'none'; % VAF, vaf, corr, rsq, vafCI, rsqCI, none
option.type = 'emg'; % emg, kin or emgkin
option.side = 'r'; % r: right side, l: left side; b: both side
option.rep1 = 1;
option.rep2=1;
%% NNMF stage 1
tic
clc
% sub=5;
if option.side=='r'
trial=1:length(P(sub).Trajectory.Right);
else
trial=1:length(P(sub).Trajectory.Left);
end
for i_trial=trial
h_trial =P(sub);
[M, std_val] = preNNMF(h_trial,i_trial,option);
for i_syn = syn
disp([num2str(sub),', ',num2str(i_trial),', ',num2str(i_syn),', ' ])
[W_best, S_best] = MyNNMF(M, std_val, i_syn, option);
if option.side == 'r'
P(sub).EMG.Right(i_trial).Syn(i_syn).W_best = W_best;
P(sub).EMG.Right(i_trial).Syn(i_syn).S_best = S_best;
else
P(sub).EMG.Left(i_trial).Syn(i_syn).W_best = W_best;
P(sub).EMG.Left(i_trial).Syn(i_syn).S_best = S_best;
end
end
end
toc
% save all data to mat file
% eval([matName,'=data;']);
% save([path,matName],matName);
disp('nnmf done!')
%% structure clustering and selection for EMG
% close all
path='D:\University\MSc\Thesis project\Gait Data\Compair';
matName = 'P';
% sub=5;
% trial=1:5;
syn=5;
name=[matName,'_Sub',num2str(sub),'_syn',num2str(syn)];
% load([path,matName]);
% eval(['data=',matName]);
% trial = 1:size(data.test(test).trial,2)
cellData1 = {};
cellData2 = {};
%% Right Side
clc
% sub=6;
for i_trial = 1:length(P(sub).Trajectory.Right)
for i_syn = syn
purtFrame = P(sub).Events.Right(i_trial).PurtFrame;
interval = length(purtFrame)-1;
x1 = purtFrame(1):purtFrame(end);
x1=double(x1);
Y=[];
X=[];
for i=1:i_syn
y1 = P(sub).EMG.Right(i_trial).Syn(i_syn).S_best(i,:);
x1=double(x1);
y1=double(y1);
[x2, y2] = rescale11(x1,y1,purtFrame,[0:100/interval:100],1000);
Y=[Y;y2];
X=[X;x2];
end
cellData1{i_syn,i_trial} = P(sub).EMG.Right(i_trial).Syn(i_syn).W_best;
cellData2{i_syn,i_trial}.Y = Y;
cellData2{i_syn,i_trial}.X = X;
end
end
Rnew1 = cellData1(~cellfun(@isempty, cellData1)); % remove empty cells
Rnew1 = reshape(Rnew1,1,[]); %convert to 1-D cell
Rnew2 = cellData2(~cellfun(@isempty, cellData2)); % remove empty cells
Rnew2 = reshape(Rnew2,1,[]); %convert to 1-D cell
label =P(sub).EMG.Right(i_trial).groupName;
partitionLine = P(sub).EMG.Right(i_trial).groupPartitionLine;
% group = plotSyn(Rnew1,Rnew2,label,partitionLine);
N=max(syn);
group = kmean1(N,Rnew1,Rnew2,label,partitionLine,name);
eval([name,'=group']);
save(['D:\University\MSc\Thesis project\Gait Data\Compair\',name],name)
%% Left Side
clc
sub=5;
for i_trial = 1:length(P(sub).Trajectory.Left)
for i_syn = syn
purtFrame = P(sub).Events.Left(i_trial).PurtFrame;
interval = length(purtFrame)-1;
x1 = purtFrame(1):purtFrame(end);
x1=double(x1);
Y=[];
X=[];
for i=1:i_syn
y1 = P(sub).EMG.Left(i_trial).Syn(i_syn).S_best(i,:);
x1=double(x1);
y1=double(y1);
[x2, y2] = rescale11(x1,y1,purtFrame,[0:100/interval:100],1000);
Y=[Y;y2];
X=[X;x2];
end
cellData1{i_syn,i_trial} = P(sub).EMG.Left(i_trial).Syn(i_syn).W_best;
cellData2{i_syn,i_trial}.Y = Y;
cellData2{i_syn,i_trial}.X = X;
end
end
Rnew1 = cellData1(~cellfun(@isempty, cellData1)); % remove empty cells
Rnew1 = reshape(Rnew1,1,[]); %convert to 1-D cell
Rnew2 = cellData2(~cellfun(@isempty, cellData2)); % remove empty cells
Rnew2 = reshape(Rnew2,1,[]); %convert to 1-D cell
label =P(sub).EMG.Left(i_trial).groupName;
partitionLine = P(sub).EMG.Left(i_trial).groupPartitionLine;
% group = plotSyn(Rnew1,Rnew2,label,partitionLine);
N=max(syn);
group = kmean1(N,Rnew1,Rnew2,label,partitionLine,name);
% eval([name,'=group']);
% save(['Data\compare\',name],name)
%% compare tool
clc
subject = {'P_Sub5_syn5','P_Sub6_syn5','P_Sub7_syn5'};
n = length(subject);
% rgb = maxdistcolor(2*n,@srgb_to_Lab);
cellData = {};
rep=[];
for i=1:n
matName = subject{i};
load(['D:\University\MSc\Thesis project\Gait Data\Compair\',matName]);
eval(['dataCmp=',matName]);
cellData{i}(:,1) = mean(dataCmp.Wtype{1},2);
rep(i,1) = size(dataCmp.Wtype{1},2);
for j=2:dataCmp.WtypeNo
cellData{i} = [cellData{i}, mean(dataCmp.Wtype{j},2)];
rep(i,j) = size(dataCmp.Wtype{j},2);
end
% for j=1:dataCmp.typeNo
% subplot(dataCmp.typeNo,n,(j-1)*n+i); hold on;
% rep = size(dataCmp.type{j},2);
% title(['rep:',num2str(rep)]);
% bar(mean(dataCmp.type{j},2),'FaceColor',rgb(i,:))
% errorbar(mean(dataCmp.type{j},2), std(dataCmp.type{j},0,2),'LineStyle','none','Color',rgb(n+i,:))
% set(gca,'XTick',[],'YTick',[],'ButtonDownFcn',@createnew_fig)
% end
end
label = P(sub).EMG.Left(i_trial).groupName;
partitionLine = P(sub).EMG.Left(i_trial).groupPartitionLine;
cmpSyn(cellData,rep,label,partitionLine);
%% plot W and S
test=5;
trial=2;
syn=4;
figure;
for i_test = test
i_test
cellData = {};
for i_trial = trial
for i_syn=1:syn
% plot W
subplot(syn,2,(i_syn-1)*2+1); hold on;
bar(data.test(i_test).trial(i_trial).emg.syn(syn).W_best(:,i_syn))
label = data.test(i_test).trial(i_trial).emg.groupName;
partitionLine = data.test(i_test).trial(i_trial).emg.groupPartitionLine;
for ii = 1:length(partitionLine)
plot(partitionLine(ii)*[1 1]+0.5 , [0 1],'color','black')
end
xticks(1:16)
xtickangle(45)
xticklabels(label)
% plot S
subplot(syn,2,(i_syn-1)*2+2); hold on;
% set(gca,'XTick',[],'YTick',[],'ButtonDownFcn',@createnew_fig)
purtFrame = floor(data.test(i_test).trial(i_trial).purtFrame/2)+1;
interval = length(purtFrame)-1;
x1 = purtFrame(1):purtFrame(end);
y1 = data.test(i_test).trial(i_trial).emg.syn(syn).S_best(i_syn,x1);
[x2, y2] = rescale11(x1,y1,purtFrame,[0:100/interval:100],1000);
plot(x2,y2)
l=[0:100/interval:100];
for i=1:length(purtFrame)
line([1 1]*l(i),[0 1],'color','black')
end
end
end
end
%% plot goodness of fit of trials
test=3;
trial=1:5;
syn=2:6;
for i_test = test
i_test
cellData = {};
for i_trial = trial
for i_syn=syn
ref = data.test(i_test).trial(i_trial).emg.M_R;
rec = data.test(i_test).trial(i_trial).emg.syn(i_syn).W_best * data.test(i_test).trial(i_trial).emg.syn(i_syn).S_best;
cellData{1,i_trial,i_syn} = ref;
cellData{2,i_trial,i_syn} = rec;
end
end
plotGoF(cellData,syn)
end
%% plot Reconstructed vs Orginal signal
i_test=6;
i_trial=1;
syn=2:5;
figure('units','normalized','outerposition',[0 0 1 1]);
for j=1:16
subplot(4,4,j); hold on;
set(gca,'XTick',[],'YTick',[],'ButtonDownFcn',@createnew_fig)
title(data.test(i_test).trial(i_trial).emg.chName{j})
ref = data.test(i_test).trial(i_trial).emg.M_R(j,:);
plot(ref,'DisplayName','Original')
for i_syn=syn
rec = data.test(i_test).trial(i_trial).emg.syn(i_syn).W_best * data.test(i_test).trial(i_trial).emg.syn(i_syn).S_best;
plot(rec(j,:),'DisplayName',['SynNo. ',num2str(i_syn)])
end
end
legend
%% plot kinematic angles
test=[8];
eval(horzcat('data=',filename{i_test},';'));
dz = data.kinematics(:,3)-data.kinematics(:,9);
dxy = sqrt((data.kinematics(:,1)-data.kinematics(:,7)).^2 + (data.kinematics(:,2)-data.kinematics(:,8)).^2) ;
angFront = atand(-dz ./ dxy);
angFront = angFront(data.purt_frame(1):end);
dz = data.kinematics(:,6)-data.kinematics(:,12);
dxy = sqrt((data.kinematics(:,4)-data.kinematics(:,10)).^2 + (data.kinematics(:,5)-data.kinematics(:,11)).^2) ;
angRight = atand(dz ./ dxy);
angRight = angRight(data.purt_frame(1):end);
for i_syn=1:syn_no
subplot(syn_no,2,2*i_syn); hold on;
plot(mean1((angFront ./ max(angFront))',2,1), 'DisplayName', 'Front angle');
plot(mean1((angRight ./ max(angRight))',2,1), 'DisplayName', 'Right angle');
ylim([-1 1])
% legend
end
%%