-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEmgKinData.m
More file actions
733 lines (581 loc) · 25.7 KB
/
EmgKinData.m
File metadata and controls
733 lines (581 loc) · 25.7 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
% EmgKinData: class to load, preprocess,and plot EMG and kinematics data
%
% obj = EmgKinData(varargin)
%
% list of methods
% -----------------------------------------------------------------
% EmgKinData class constructor
% subtract subtract tonic or baseline level to EMG data
% resample average emgs over time interval dt and adjust kin time accordingly
% filter filter data
% getDefFilterOpt define the defaults of filter function
% selectCh select channels
% merge merge kin and emg data in one data matrix
% shift introduce a delay between emg and kin data
% split divide data into kin and emg objects
% average average data across trials
% getDefAverageOpt define the defaults of average function
% normalize normalize data across trials
% getDefNormalizeOpt define the defaults of normalize function
% getDefSubtractOpt define the defaults of subtract function
% getData get data matrix
% getNsamp get number of samples
% plot plot EMG and KIN data
% getDefPlotOpt define the defaults of plot function
% duration compute duration of data for each trials
% datalim return mim and max value of data
% tsamp returns data mean sampling interval (rounded to us)
% timerange get time range of data of each trial
%
% Synergy Analyzer Toolbox for MATLAB: https://github.com/SynergyAnalyzer/SynergyAnalyzerToolbox.git
% License: GNU GPL v3
%
classdef EmgKinData
properties
data;
emg;
kin;
time;
trialId = [];
chlabels = {};
nonnegch = [];
order;
delay;
normalized;
end
methods
%------------------------------------------------------------------
function obj = EmgKinData(varargin)
if nargin<1
return
end
if isstruct(varargin{1}) && isfield(varargin{1},'data') && isfield(varargin{1},'time')
%data,trialId,chlabels,order
emgkin = varargin{1};
ntrial = length(emgkin);
for i=1:ntrial
obj(i).data = emgkin(i).data;
obj(i).time = emgkin(i).time;
if nargin>1 && isequal(length(varargin{2}),ntrial)
trialId = varargin{2};
obj(i).trialId = trialId(i);
else
obj(i).trialId = i;
end
nch = size(obj(i).data,1);
if nargin>2 && iscell(varargin{3}) && isequal(length(varargin{3}),nch)
chlabels = varargin{3};
obj(i).chlabels = chlabels;
else
for j=1:nch
chlabels{j} = sprintf('emg%02i',j);
end
obj(i).chlabels = chlabels;
end
if nargin>3
obj(i).order = varargin{4};
else
obj(i).order = 0;
end
if nargin>4
obj(i).nonnegch = varargin{5};
else
obj(i).nonnegch = [1:18]; %%%%%% TO BE CHECKED
end
end
elseif isa(varargin{1},'EmgData') && isa(varargin{2},'KinData')
%emg,kin,trialId,chlabels,order
emg = varargin{1};
kin = varargin{2};
if length(emg) ~= length(kin)
warning('Emg and Kin data must have same number of trials')
return
end
ntrial = length(emg);
if nargin>4 && not(isempty(varargin{5}))
delay = varargin{5};
else
delay = 0;
end
if nargin>3 && not(isempty(varargin{4}))
order = varargin{4};
else
order = 0;
end
for i=1:ntrial
obj(i).order = order;
obj(i).normalized = 0;
if nargin>2 && isequal(length(varargin{3}),ntrial)
trialId = varargin{3};
obj(i).trialId = trialId(i);
else
obj(i).trialId = i;
end
obj(i).emg = emg(i);
obj(i).kin = kin(i);
obj(i) = shift(obj(i),delay);
obj(i) = merge(obj(i));
nch = size(emg(i).data,1) + size(kin(i).data,1);
nemgch = size(emg(i).data,1);
nkinch = size(kin(i).data,1);
for j=1:nemgch
chlabels{j} = emg(i).chlabels{j};
end
for j=(1+nch-nkinch):nch
jj = j - (nch-nkinch);
if order>1
chlabels{j} = sprintf('d^%i/dt^%i %s',order,order,kin(i).chlabels{jj});
elseif order>0
chlabels{j} = sprintf('d/dt % s', kin(i).chlabels{jj});
else
chlabels{j} = kin(i).chlabels{jj};
end
end
obj(i).chlabels = chlabels;
obj(i).nonnegch = [1:size(emg(i).data,1)];
end
end
end
%------------------------------------------------------------------
function obj = subtract(obj,opt)
% subtract tonic or baseline level to EMG data
% set options or use defaults
defopt = obj.getDefSubtractOpt;
if nargin>1 && isstruct(opt)
fname = fieldnames(opt);
for i=1:length(fname)
defopt.(fname{i}) = opt.(fname{i});
end
end
opt = defopt;
ntrial = length(obj);
e = [obj.emg];
e = subtract(e,opt);
for i=1:ntrial
obj(i).emg = e(i);
end
obj = merge(obj);
end
%------------------------------------------------------------------
function obj = resample(obj,dt)
% average emgs over time interval dt and adjust kin time
% accordingly
ntrial = length(obj);
for i=1:ntrial
obj(i).emg = resample(obj(i).emg,dt);
obj(i).kin = resample(obj(i).kin,dt);
end
obj = merge(obj);
end
%------------------------------------------------------------------
function obj = filter(obj,opt)
% set options or use defaults
defopt = obj.getDefFilterOpt;
if nargin>1 && isstruct(opt)
fname = fieldnames(opt);
for i=1:length(fname)
defopt.(fname{i}) = opt.(fname{i});
end
end
opt = defopt;
ntrial = length(obj);
for i=1:ntrial
obj(i).emg = filter(obj(i).emg,opt.emgFilter);
obj(i).kin = filter(obj(i).kin,opt.kinFilter);
end
obj = merge(obj);
end
%------------------------------------------------------------------
function opt = getDefFilterOpt(obj)
opt.emgFilter.type = 'fir1';
opt.emgFilter.par = [50 .04]; % 20 Hz @ 1KHz EMG sampling rate
opt.emgFilter.resample = 1;
opt.emgFilter.resample_period = .01; % resampling period [s]
opt.kinFilter.type = 'butter';
opt.kinFilter.par = [2 10/(100/2)]; %2 order 10Hz @ 100Hz sampling rate
end
%------------------------------------------------------------------
function obj = selectCh(obj,chind)
% select channels
nkin = length(obj);
for i=1:nkin
obj(i).data = obj(i).data(chind,:);
end
obj(i).chlabels = obj(i).chlabels(chind);
end
%------------------------------------------------------------------
function obj = merge(obj,order)
if (nargin<2 || isempty(order)) && isempty(obj(1).order)
order = 0;
elseif not(isempty(obj(1).order))
order = obj(1).order;
end
ntrial = length(obj);
for i=1:ntrial
obj(i).order = order;
e = obj(i).emg;
k = obj(i).kin;
if ~isequal(tsamp(e),tsamp(k))
if obj(i).trialId ==1, disp('size(emg,2) must be equal to length(kin), resampling'), end
e = filter(e);
dt = 0.01; %10 ms
e = resample(e,dt);
k = resample(k,dt);
end
if ~isequal(duration(e),duration(k))
if obj(i).trialId ==1, disp('size(emg,2) must be equal to length(kin), slicing'), end
ktr = timerange(k);
etr = timerange(e);
%find smallest common interval
if ktr(1)<0 && etr(1)<0
tr(1) = max([ktr(1) etr(1)]);
elseif ktr(1)<0 || etr(1)<0
tr(1) = max([ktr(1) etr(1)]);
elseif ktr(1)>0 && etr(1)>0
tr(1) = min([ktr(1) etr(1)]);
end
tr(2) = min([ktr(2) etr(2)]);
timek = k.time;
indk = find(timek>=tr(1) & timek<=tr(end)); % find samples contained into chosen interval
timee = e.time;
inde = find(timee>=tr(1) & timee<=tr(end)); % find samples contained into chosen interval
if not(isequal(length(indk),length(inde)))
[mm,im] = min(abs(timee-timek(indk(1)))); % find sample closest to first data sample
inde = im + [1:length(indk)] - 1;
end
k.data = k.data(:,indk);
k.time = k.time(indk);
e.data = e.data(:,inde);
e.time = e.time(inde);
end
if not(obj(i).normalized)
if order == 0
obj(i).data = [e.data; k.data];
elseif order == 1
obj(i).data = [e.data; velocity(k,k.time).data];
elseif order == 2
obj(i).data = [e.data; acceleration(k,k.time).data];
end
else
obj(i).data = [e.data; k.data];
end
obj(i).time = k.time;
end
end
%------------------------------------------------------------------
function obj = shift(obj,delay)
if (nargin<2 || isempty(delay)) && isempty(obj(1).delay)
delay = 0;
elseif not(isempty(obj(1).delay))
delay = obj(1).delay;
end
ntrial = length(obj);
for i=1:ntrial
obj(i).delay = delay;
e = obj(i).emg;
k = obj(i).kin;
k = shift(k,delay);
obj(i).kin = k;
end
end
%------------------------------------------------------------------
function obj = split(obj)
ntrial = length(obj);
for i=1:ntrial
nch = size(obj(i).data,1); % number of channels
emgch = obj(i).nonnegch;
nemgch = length(emgch);
kinch = [(nemgch+1):nch];
obj(i).emg.data = obj(i).data(emgch,:);
obj(i).kin.data = obj(i).data(kinch,:) ;
obj(i).emg.time = obj(i).time;
obj(i).kin.time = obj(i).time ;
obj(i).emg.chlabels = obj(i).chlabels(emgch);
obj(i).kin.chlabels = obj(i).chlabels(kinch);
end
end
%------------------------------------------------------------------
function objav = average(obj,opt)
% average EMG/KIN data across trials
% set options or use defaults
defopt = obj.getDefAverageOpt;
if nargin>1 && isstruct(opt)
fname = fieldnames(opt);
for i=1:length(fname)
defopt.(fname{i}) = opt.(fname{i});
end
end
opt = defopt;
e = [obj.emg];
k = [obj.kin];
eav = average(e,opt);
kav = average(k,opt);
objav = EmgKinData(eav,kav,[],obj(1).order,0); %set delay to zero as data are already delayed
end
%------------------------------------------------------------------
function opt = getDefAverageOpt(obj)
nkin = length(obj);
opt.gr = {[1:nkin]};
opt.tref = zeros(1,nkin);
tr = timerange(obj);
trc = [max(tr(:,1)) min(tr(:,2))];
if diff(trc)>0
opt.trange = trc;
else
opt.trange = [];
end
end
%------------------------------------------------------------------
function [obj,objnorm] = normalize(obj,opt)
%normalize data in emg/kin amplitude
%
% type action
% -------------------------------------------------------------
% 0 use normdata [nch,1] for normalization of each channel
% 1 normalize to max of any channel
% 2 normalize each channel to max in that channel
% 32 max of each abs(channel) and same range for emg [0 2] and trq [-1 1]
% set options or use defaults
defopt = obj.getDefNormalizeOpt;
if nargin>1 && isstruct(opt)
fname = fieldnames(opt);
for i=1:length(fname)
defopt.(fname{i}) = opt.(fname{i});
end
end
opt = defopt;
ntrial = length(obj);
% check for order
for i=1:ntrial
obj(i).normalized = 1;
if obj(i).order == 1
obj(i).kin.data = velocity(obj(i).kin,obj(i).kin.time).data;
elseif obj(i).order == 2
obj(i).kin.data = acceleration(obj(i).kin,obj(i).kin.time).data;
end
end
e = [obj.emg];
k = [obj.kin];
e = normalize(e,opt);
k = normalize(k,opt);
for i=1:ntrial
obj(i).emg = e(i);
obj(i).kin = k(i);
end
obj = merge(obj);
end
%------------------------------------------------------------------
function opt = getDefNormalizeOpt(obj)
nkin = length(obj);
opt.type = 32; % max absolute value of each channels
opt.isect = [1:nkin]; % sections to use for computing max
opt.normdata = [];
opt.nonnegch = [];
end
%------------------------------------------------------------------
function opt = getDefSubtractOpt(obj)
opt.type = 'tonic'; % subtract tonic activity to get phasic EMG data
opt.t_pre = [-.4 -.2]; % interval before onset for initial level
opt.t_post = [.2 .4]; % interval after end for final level
opt.t_onset = [];
opt.t_end = [];
opt.clip = 1; % clip to zero after subtraction
end
%------------------------------------------------------------------
function [data,datapar] = getData(obj,type,isect)
% get data matrix
nkin = length(obj);
if nargin<2, type = 'spatial'; end
if nargin<3, isect = [1:nkin]; end
isect = intersect([1:nkin],isect);
nsect = length(isect);
nch = size(obj(isect(1)).data,1);
switch type
case 'spatial'
% rows are channels, columns are time samples x trials
data = zeros(nch,obj.getNsamp(isect));
inds = cell(1,nsect);
isamp = 0;
for ii=1:nsect
i = isect(ii);
nsamp = length(obj(i).time);
data(:,isamp+[1:nsamp]) = obj(i).data;
inds{ii} = isamp+[1:nsamp];
isamp = isamp + nsamp;
end
%
% other options to be implemented
%
% case 'temporal'
% % rows are time samples, columns are channels x trials
% ntime = length(k(isect(1)).time);
% data = zeros(ntime,nch*nsect);
% inds = cell(1,nsect);
% if ~isequalinterval(k(isect))
% warning('data must have the same interval for temporal synergies')
% return
% end
%
% nsamp = nch;
% isamp = 0;
% for ii=1:nsect
% i = isect(ii);
% data(:,isamp+[1:nsamp]) = k(i).data';
% inds{ii} = isamp+[1:nsamp];
% isamp = isamp + nsamp;
% end
%
% case 'spatiotemporal'
% % rows are channels x time samples, columns trials
% ntime = length(k(isect(1)).time);
% data = zeros(ntime*nch,nsect);
% inds = cell(1,nsect);
% if ~isequalinterval(k(isect))
% warning('data must have the same interval for spatiotemporal synergies')
% return
% end
%
% nsamp = 1;
% isamp = 0;
% for ii=1:nsect
% i = isect(ii);
% data(:,isamp+[1:nsamp]) = k(i).data(:);
% inds{ii} = isamp+[1:nsamp];
% isamp = isamp + nsamp;
% end
end
datapar.nch = nch;
datapar.inds = inds;
datapar.nonnegch = obj(1).nonnegch;
datapar.chlabels = obj(1).chlabels;
end
%------------------------------------------------------------------
function nsamp = getNsamp(obj,isect)
% get number of samples
nkin = length(obj);
if nargin<2, isect = [1:nkin]; end
isect = intersect([1:nkin],isect);
nsamp = 0;
for i=isect
nsamp = nsamp + length(obj(i).time);
end
end
%------------------------------------------------------------------
function hha = plot(obj,opt)
% plot EMG/KIN data
% set options or use defaults
defopt = obj.getDefPlotOpt;
if nargin>1 && isstruct(opt)
fname = fieldnames(opt);
for i=1:length(fname)
defopt.(fname{i}) = opt.(fname{i});
end
end
opt = defopt;
opte = opt;
optk = opt;
obj = split(obj);
e = [obj.emg];
k = [obj.kin];
%
% figure and axes
%
if isempty(opt.axes)
opte.pos = opt.pos(1,:);
optk.pos = opt.pos(2,:);
else
opte.axes = opt.axes(1,:);
optk.axes = opt.axes(2,:);
end
if isempty(opt.figure)
hf = figure;
opte.figure = hf;
optk.figure = hf;
end
% color
if not(opt.rec)
opte.color = zeros(1,3);
optk.color = .8*ones(1,3);
end
hae = plot(e,opte);
optk.usetitle = 0;
hak = plot(k,optk);
if nargout>0, hha = [hae;hak]; end
end
%------------------------------------------------------------------
function opt = getDefPlotOpt(obj)
nkin = length(obj);
nnonnegch = length(obj(1).nonnegch);
nch = size(obj(1).data,1); % number of channels
% chsel
opt.emgsel = obj.nonnegch;
opt.kinsel = [(nnonnegch+1):nch]-nnonnegch;
% isect
opt.isect = [1:nkin];
% tref
opt.tref = [];
% events
opt.event_code = {};
opt.event_time = {};
opt.events_color = {};
opt.events_style = {};
% title
opt.usetitle = 1;
opt.chtitle = {};
% plotting options
opt.figure = [];
opt.axes = [];
opt.pos = [.08 .40 .9 .55; .08 .05 .9 .30];
opt.overlap = 0;
opt.spacing = .01;
opt.xlim = [];
opt.ylim = []; % for each individual trace
opt.chscale = 0;
opt.chscalelabel = '';
opt.fill = 0;
opt.color = 'k';
opt.rec = 0;
end
%------------------------------------------------------------------
function du = duration(obj)
% compute duration of data for each trials
nkin = length(obj);
for i=1:nkin
du(i) = range(obj(i).time);
end
end
%------------------------------------------------------------------
function val = datalim(obj)
%return mim and max value of data
nkin = length(obj);
for i=1:nkin
valmin(i) = min(obj(i).data(:));
valmax(i) = max(obj(i).data(:));
end
val = [min(valmin) max(valmax)];
end
%------------------------------------------------------------------
function t = tsamp(obj,prec)
%returns data mean sampling interval (rounded to us)
if nargin<2, prec=10^-6; end
nkin = length(obj);
for i=1:nkin
t(i) = mean(round(diff(obj(i).time)/prec))*prec;
end
end
%------------------------------------------------------------------
function tr = timerange(obj)
%get time range of data of each trial
nkin = length(obj);
for i=1:nkin
tr(i,:) = obj(i).time([1 end]);
end
end
end
%------------------------------------------------------------------
methods (Static)
end
end
%------------------------------------------------------------------
% subfunctions
%------------------------------------------------------------------