-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannel.m
644 lines (570 loc) · 24.7 KB
/
Channel.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
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
classdef Channel < TMSiSAGA.HiddenHandle
%CHANNEL Class that represents a single channel on a device.
%
%CHANNEL Properties:
% number - The number used by the device to identify the channel
% type - The channel type like ExG, Uni, etc.
% format - The format of the channel, float, int32, uin32, etc.
% divider - The sample rate of this channel, is "<base sample rate device> / 2^<divider>". If -1 the channel is disabled.
% impedance_divider - The sample rate when in impedance mode, is "<base sample rate device> / 2^<divider>". If -1 the channel is disabled.
% bandwidth - The bandwidth required for the data in this channel.
% exponent - The exponent to apply to the data.
% unit_name - The unit name of the retrieved data.
% name - The fixed name of the channel.
% alternative_name - The current alternative name set on this channel.
% sensor_channel - An optional sensor channel that is connected to this channel.
%
%CHANNEL Methods:
% Channel - Constructor for the Channel object.
% isActive - Is the current channel active in sample, or impedance mode.
% enable - Enable the channel.
% disable - Disable the channel.
% setUnitname - Set the unit for this channel.
% setExponent - Set the exponent value for this channel.
% setAlternativeName - Set the alternative name for this channel.
% isRef - (Added by MM): Check if this is a REF channel.
% isExG - Check if this an ExG channel.
% isBip - Check if this an Bip channel.
% isAux - Check if this an Aux channel.
% isDig - Check if this an Dig channel.
% isDigstat - Check if this an Digstat channel.
% isSaw - Check if this a saw channel.
% isCounter - Check if this is the counter channel.
% isStatus - Check if this is the status channel.
% setSAGA - (Added by MM): A function to set the `saga` property, which is the `tag` property of the Device this channel is associated with.
% fromSAGA - (Added by MM): Checks if `saga` for this Channel matches the tag in the argument.
% transform - A function that transform the raw sample data.
%
%CHANNEL Example:
%
%
properties
% The number used by the device to identify the channel
number
% The channel type like ExG, Uni, etc.
type
% The format of the channel, float, int32, uin32, etc.
format
% The sample rate of this channel, is "<base sample rate device> / 2^<divider>". If -1 the channel is disabled.
divider
% The sample rate when in impedance mode, is "<base sample rate device> / 2^<divider>". If -1 the channel is disabled.
impedance_divider
% The bandwidth required for the data in this channel.
bandwidth
% The exponent to apply to the data.
exponent
% he unit name of the retrieved data.
unit_name
% The fixed name of the channel.
name
% The current alternative name set on this channel.
alternative_name
% An optional sensor channel that is connected to this channel.
sensor_channel
% SAGA-DR serial number associated with this channel.
sn (1,1) int64 = 0; % Serial number associated with SAGA
end
% Properties that must be set using class methods:
properties (GetAccess = public, SetAccess = protected)
% (Added MM) Which saga tag is this channel associated with?
saga (1,1) string = "X";
end
properties (Access = private)
% % The source device of this channel.
% device
% The type to which the has to be transformed before using.
to_type
% The stride that is required for the type conversion.
to_stride
end
methods
function obj = Channel(device, channel_info, tag, number)
%CHANNEL - Constructor for the Channel object.
%
% obj = Channel(device, channel_info)
% obj = Channel(device, channel_info, tag, number);
%
% Constructor for the Channel object. Requires a device, and the raw channel info structure
% from the TMSi device. It is not recommended to create a channel directly.
%
% obj [out] - Channel object.
% device [in] - The device for which the channel object is created.
% channel_info [in] - Basic channel information, should be gotten from the device.
% tag [in] - SAGA tag ("A" or "B"; default is "X")
if nargin < 3
tag = "X";
end
if nargin < 4
obj = repmat(obj, size(channel_info));
for ii = 1:numel(channel_info)
obj(ii) = TMSiSAGA.Channel(device, channel_info(ii), tag, ii - 1);
end
return;
end
obj.saga = tag;
obj.sn = device.data_recorder.serial_number;
obj.number = int64(number);
obj.type = int64(channel_info.ChannelType);
obj.format = int64(channel_info.ChannelFormat);
obj.divider = int64(channel_info.ChanDivider);
obj.impedance_divider = int64(channel_info.ImpDivider);
obj.bandwidth = int64(channel_info.ChannelBandWidth);
obj.exponent = int64(channel_info.Exp);
obj.unit_name = channel_info.UnitName;
obj.name = channel_info.DefChanName;
obj.alternative_name = channel_info.AltChanName;
if obj.alternative_name == ""
obj.alternative_name = obj.name;
end
obj.sensor_channel = [];
obj.to_type = 'double';
obj.to_stride = 0;
if bitand(obj.format, int64(hex2dec('FF00'))) == hex2dec('1100')
obj.to_type = 'single';
obj.to_stride = 0;
elseif bitand(obj.format, int64(hex2dec('0100')))
obj.to_type = ['int' num2str(bitand(obj.format, int64(hex2dec('00FF'))))];
obj.to_stride = int32(4 / (bitand(obj.format, int64(hex2dec('00FF'))) / 8));
else
obj.to_type = ['uint' num2str(bitand(obj.format, int64(hex2dec('00FF'))))];
obj.to_stride = int32(4 / (bitand(obj.format, int64(hex2dec('00FF'))) / 8));
end
end
function name = getName(obj)
%GET Get 'name' value
name = strings(size(obj));
for ii = 1:numel(obj)
name(ii) = string(sprintf('%s: %s', obj(ii).saga, obj(ii).name));
end
end
function is_active = isActive(obj, impedance_mode)
%ISACTIVE - A function that checks if the channel is active.
%
% is_active = isActive(obj, impedance_mode)
%
% This function checks if a channel is active in sampling mode or
% in impedance mode. Cannot check for both cases at a time.
%
% is_active [out] - Boolean that states whether channel is
% active.
% obj [in] - Channel object.
% impedance_mode [in] - (Optional) Set to TRUE if you want to
% check the active state of the channel when in impedance
% mode. Is FALSE by default.
if nargin < 2
impedance_mode = false;
end
if numel(obj) > 1
is_active = false(size(obj));
for ii = 1:numel(obj)
is_active(ii) = obj(ii).isActive(impedance_mode);
end
return;
end
if impedance_mode
is_active = obj.impedance_divider >= 0;
else
is_active = obj.divider >= 0;
end
end
function enable(obj)
%ENABLE - A function to enable the channel.
%
% enable(obj)
%
% This function will enable the channel and set the divider to the current
% divider for this channel type gotten from device.
%
% obj [in] - Channel object.
%
if numel(obj) > 1
for ii = 1:numel(obj)
enable(obj(ii));
end
return;
end
% if obj.device.is_sampling
% throw(MException('Channel:enable', 'Cannot enable/disable channel while device is sampling.'));
% end
obj.divider = 0;
% obj.divider = obj.device.dividers(obj.type);
% obj.device.out_of_sync = true;
end
function disable(obj)
%DISABLE - A function to disable the channel.
%
% disable(obj)
%
% This function will disable the channel and set the divider to -1.
%
% obj [in] - Channel object.
%
% if obj.device.is_sampling
% throw(MException('Channel:disable', 'Cannot enable/disable channel while device is sampling.'));
% end
obj.divider = -1;
% obj.device.out_of_sync = true;
end
function setDeviceTag(obj, saga)
%SETDEVICETAG - Sets the device tag associated with this channel.
%
% setDeviceTag(obj, saga);
%
% saga - "A" or "B" (some identifier)
for ii = 1:numel(obj)
obj(ii).saga = saga;
end
end
function setUnitName(obj, unit_name)
%SETUNITNAME - A function to set the unit name for this channel.
%
% setUnitName(obj, unit_name)
%
% This function will set the unit name for this channel. Primary use
% is to set the unit name when it is a sensor channel.
%
% obj [in] - Channel object.
% unit_name [in] - A string containing the unit name.
%
% if obj.device.is_sampling
% throw(MException('Channel:setUnitName', 'Cannot change unit name while device is sampling.'));
% end
obj.unit_name = unit_name;
end
function setExponent(obj, exponent)
%SETEXPONENT - A function to set the exponent value for this channel.
%
% setExponent(obj, exponent)
%
% This function will set the exponent for this channel. The raw data is
% divided by 10^exponent.
%
% obj [in] - Channel object.
% exponent [in] - A value containing the exponent.
%
% if obj.device.is_sampling
% throw(MException('Channel:setExponent', 'Cannot change exponent while device is sampling.'));
% end
obj.exponent = exponent;
end
function setAlternativeName(obj, alternative_name)
%SETALTERNATIVENAME - A function to set the alternative name of this channel.
%
% setAlternativeName(obj, alternative_name)
%
% This function will set the alternative name for this channel. By default
% the regular channel name is used.
%
% obj [in] - Channel object.
% alternative_name [in] - A string to be used as alternative name.
%
% if obj.device.is_sampling
% throw(MException('Channel:setAlternativeName', 'Cannot change alternative name while device is sampling.'));
% end
obj.alternative_name = alternative_name;
% obj.device.out_of_sync = true;
end
function is_true = isRef(obj)
%ISREF - A function to check if this channel is a REF channel.
%
% is_true = isRef(obj);
%
% is_true [out] - Boolean that states whether channel type is
% REF.
% obj [in] - Channel object.
%
% (2022-11-15 - added by MM)
is_true = false(size(obj));
for ii = 1:numel(obj)
is_true(ii) = contains(upper(obj(ii).name), 'REF');
end
end
function is_true = isExG(obj)
%ISEXG - A function to check if this channel is an ExG channel.
%
% is_true = isExG(obj)
%
% This function will check if the channel type is ExG (1).
%
% is_true [out] - Boolean that states whether channel type is
% ExG.
% obj [in] - Channel object.
%
is_true = [obj.type] == 1;
end
function is_true = isBip(obj)
%ISBIP - A function to check if this channel is an Bip channel.
%
% is_true = isBip(obj)
%
% This function will check if the channel type is Bip (2).
%
% is_true [out] - Boolean that states whether channel type is
% Bip.
% obj [in] - Channel object.
%
is_true = [obj.type] == 2;
end
function is_true = isAux(obj)
%ISAUX - A function to check if this channel is an Aux channel.
%
% is_true = isAux(obj)
%
% This function will check if the channel type is Aux (3).
%
% is_true [out] - Boolean that states whether channel type is
% Aux.
% obj [in] - Channel object.
%
is_true = [obj.type] == 3;
end
function is_true = isDig(obj)
%ISDIG - A function to check if this channel is an Dig channel.
%
% is_true = isDig(obj)
%
% This function will check if the channel type is Dig (4).
%
% is_true [out] - Boolean that states whether channel type is
% Dig.
% obj [in] - Channel object.
%
is_true = [obj.type] == 4;
end
function is_true = isDigstat(obj)
%ISDIGSTAT - A function to check if this channel is an Digstat channel.
%
% is_true = isDigstat(obj)
%
% This function will check if the channel type is Digstat (5).
%
% is_true [out] - Boolean that states whether channel type is
% Digstat.
% obj [in] - Channel object.
%
is_true = [obj.type] == 5;
end
function is_true = isSaw(obj)
%ISSAW - A function to check if this channel is an Saw channel.
%
% is_true = isSaw(obj)
%
% This function will check if the channel type is Saw (6).
%
% is_true [out] - Boolean that states whether channel type is
% Saw.
% obj [in] - Channel object.
%
is_true = [obj.type] == 6;
end
function is_true = isCounter(obj)
%ISCOUNTER - A function to check if this channel is the counter channel.
%
% is_true = isCounter(obj)
%
% This function checks if the name of the channel is COUNTER.
%
% is_true [out] - Boolean that states whether channel type is
% COUNTER.
% obj [in] - Channel object.
%
is_true = strcmp({obj.name}, 'COUNTER');
end
function is_true = isStatus(obj)
%ISSTATUS - A function to check if this channel is the status channel.
%
% is_true = isStatus(obj)
%
% This function checks if the name of the channel is STATUS.
%
% is_true [out] - Boolean that states whether channel type is
% STATUS.
% obj [in] - Channel object.
%
is_true = strcmp({obj.name}, 'STATUS');
end
function is_true = isTrigger(obj)
%ISTRIGGER - A function to check if this channel is the TRIGGER channel.
%
% is_true = isTrigger(obj)
%
% This function checks if the name of the channel is TRIGGER.
%
% is_true [out] - Boolean that states whether channel type is
% TRIGGER.
% obj [in] - Channel object.
%
is_true = contains({obj.name}, 'TRIGGER');
end
function setSAGA(obj, saga_tag)
%SETSAGA - A function to set the `saga` property, which is the `tag` property of the Device this channel is associated with.
%
% setSAGA(obj, saga_tag);
%
% Should set saga_tag as "A" or "B". Can assign to array of
% Channel objects, and if saga_tag is a string array each
% element corresponds to matched element of `obj`.
if nargin < 1
me = MException('MATLAB:notEnoughInputs', 'Not enough input arguments.');
aac = matlab.lang.correction.AppendArgumentsCorrection('"A"');
me = me.addCorrection(aac);
throw(me);
end
saga_tag = string(saga_tag);
if isscalar(saga_tag)
saga_tag = repmat(saga_tag, size(obj));
end
for ii = 1:numel(obj)
obj(ii).saga = saga_tag(ii);
end
end
function is_true = fromSAGA(obj, saga_tag)
%FROMSAGA - A function to check the `saga` property against the provided tag.
%
% fromSAGA(obj, saga_tag);
%
% Example:
% idx = channels.fromSAGA("A"); % Get mask for channels from
% % "A" SAGA (useful if running
% % acquisition from two SAGA
% % devices on same host PC).
if nargin < 1
me = MException('MATLAB:notEnoughInputs', 'Not enough input arguments.');
aac = matlab.lang.correction.AppendArgumentsCorrection('"A"');
me = me.addCorrection(aac);
throw(me);
end
saga_tag = string(saga_tag);
if isscalar(saga_tag)
saga_tag = repmat(saga_tag, size(obj));
end
is_true = false(size(obj));
for ii = 1:numel(obj)
is_true(ii) = strcmpi(obj(ii).saga, saga_tag(ii));
end
end
function result = transform(obj, samples)
%TRANSFORM - A function that transforms samples, based on channel settings.
%
% result = transform(obj, samples)
%
% This function will transform the raw samples from the device, into the
% right types and applies either sensor transform or the default exponent
% transform.
%
% result [out] - Transformed samples.
% obj [in] - Channel object.
% samples [in] - Raw samples obtained from the device.
%
if strcmp(obj.to_type, 'single')
result = double(samples);
else
result = typecast(samples, obj.to_type);
result = double(result(1:obj.to_stride:end));
end
if numel(obj.sensor_channel) ~= 0
result = obj.sensor_channel.transform(result);
else
result = result ./ (10^double(obj.exponent));
end
end
function channels = toCell(obj)
%TOCELL Returns cell array of channels from array of Channel objects.
%
% Syntax:
% channels = toCell(channelObjArray);
%
% Output:
% channels - Cell array with same size as input array. If
% input is a scalar Channel object, then the
% returned output is a 1x1 cell with that object
% in it. Corresponding elements of the returned
% cell array match to the original elements of
% the Channel object array.
%
% See also: Contents
channels = cell(size(obj));
for iCh = 1:numel(obj)
channels{iCh} = obj(iCh);
end
end
function channels = toStruct(obj, chan_nr_offset)
%TOSTRUCT Returns as a struct array
if nargin < 2
chan_nr_offset = 0;
end
channels = struct();
for i=1:numel(obj)
channels(i).ChanNr = obj(i).number + chan_nr_offset;
channels(i).ChanDivider = obj(i).divider;
channels(i).AltChanName = obj(i).alternative_name;
channels(i).name = obj(i).name;
channels(i).type = obj(i).type;
channels(i).sn = obj(i).sn;
channels(i).tag = obj(i).saga;
channels(i).unit_name = obj(i).unit_name;
end
end
end
methods (Static)
function channels = DefaultSet(setType, varargin)
%DEFAULTSET Returns default cell array of channels
%
% Syntax:
% channels = TMSiSAGA.Channel.DefaultSet();
% channels = TMSiSAGA.Channel.DefaultSet(setType);
% channels = TMSiSAGA.Channel.DefaultSet(setType,'Name',value,...);
%
% Inputs:
% setType - The channel-set type. See options in
% `enum.TMSiChannelSet`. If not given, then the
% default value of this is
% enum.TMSiChannelSet.NHP_EMG
% 'Name',value pairs:
% + 'ChannelSetsFolder' - Default is the package folder
% with sub-folder 'ChannelSets'
%
% See also: Contents, enum, enum.TMSiChannelSet
% For file i/o
base_file_folder = fileparts(mfilename('fullpath'));
ChannelSetsDefault = fullfile(base_file_folder, 'ChannelSets');
% Validation functions
setTypeValidator = @(in)isa(in, 'enum.TMSiChannelSet');
ChannelSetsFolderParamValidator = @(in)((isstring(in)||ischar(in)) && (exist(in,'dir')~=0) && (~isempty(dir(fullfile(in, '*.mat')))));
if (nargin < 2) || ~ismember('ChannelSetsFolder', varargin(1:3:end))
assert(ChannelSetsFolderParamValidator(ChannelSetsDefault), ...
'TMSiSAGA:Channel:EmptyChannelSetsFolder', ...
'No *.mat files found in default ChannelSetsFolder (%s).', ...
ChannelSetsDefault);
end
TagParamValidator = @(in)((isstring(in) || ischar(in)) && ismember(char(in), {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'}));
% Handle default
if (nargin < 1) || isempty(setType)
setType = enum.TMSiChannelSet.NHP_EMG;
elseif ischar(setType) || isstring(setType)
if ~ismember(char(setType), {'NHP_EMG', 'NHP'})
firstParam = char(setType);
setType = enum.TMSiChannelSet.NHP_EMG;
varargin = [firstParam, varargin];
else
setType = enum.TMSiChannelSet.(setType);
end
end
% Parse inputs
p = inputParser();
p.addRequired('setType', setTypeValidator);
p.addParameter('ChannelSetsFolder', ChannelSetsDefault, ChannelSetsFolderParamValidator);
p.addParameter('Tag', 'X', TagParamValidator);
p.parse(enum.TMSiChannelSet(setType), varargin{:});
pname = p.Results.ChannelSetsFolder;
switch p.Results.setType
case enum.TMSiChannelSet.NHP_EMG
ch = getfield(load(fullfile(pname, 'NHP_EMG.mat'), 'channels'), 'channels');
otherwise
error("Unhandled TMSiChannelSet enumeration: %s", string(p.Results.setType));
end
setSAGA(ch, p.Results.Tag);
channels = toCell(ch);
end
end
end