-
Notifications
You must be signed in to change notification settings - Fork 35
/
sload.m
1429 lines (1281 loc) · 52.8 KB
/
sload.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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [signal,H] = sload(FILENAME,varargin)
% SLOAD loads signal data of various data formats
% [signal,header] = sload(FILENAME)
% [signal,header] = sload(FILENAME,CHAN)
% read selected channels in list CHAN
% CHAN=0 [default], reads all channels
%
% [signal,header] = sload(dir('f*.emg') ... )
% [signal,header] = sload('f*.emg' ...)
% loads all files 'f*.emg'
%
% [signal,header] = sload(..., PropertyName1,PropertyValue1,...)
% PropertyName(s) PropertyValue
% 'UCAL' - data uncalibrated (not scaled)
% 'OVERFLOWDETECTION' 'On' [default]
% 'Off' no overflow detection
% 'OUTPUT' 'single' single precision data [default: 'double']
% 'NUMBER_OF_NAN_IN_BREAK' N inserts N NaN's between two concatanated segments
% default: N=100
% 'SampleRate' Fs target sampling rate (supports resampling)
% 'EOG_CORRECTION' 'On' uses two-channel regression analysis - if possible
% 'Off' no correction of EOG artifacts [default]
% 'CNT32', '32bit' force CNT 32bit format
% 'BDF' N decodes BDF status channel into eventtable
% using Mode = N. For details see HELP BDF2BIOSIG_EVENTS
%
% The list of supported formats is available here:
% http://pub.ist.ac.at/~schloegl/biosig/TESTED
%
% SLOAD loads all the data (of the selected channels)
% at once. In case of large data files, This can be
% a problem. Instead, You can use
% HDR = sopen(FILENAME,'r');
% [s,HDR]=sread(HDR,duration_segment1);
% [s,HDR]=sread(HDR,duration_segment2);
% ....
% [s,HDR]=sread(HDR,duration_segmentM);
% HDR = sclose(HDR);
%
% see also: SVIEW, SOPEN, SREAD, SCLOSE, SAVE2BKR, TLOAD
%
% In order to increase the speed, install mexSLOAD.mex from biosig4c++
%
% Reference(s):
% $Id$
% Copyright (C) 1997-2007,2008,2009,2010,2011 by Alois Schloegl
% This is part of the BIOSIG-toolbox http://biosig.sf.net/
%
% BioSig is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% BioSig is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with BioSig. If not, see <http://www.gnu.org/licenses/>.
if length(varargin)<2;
MODE = '';
else
MODE = varargin{2};
end;
CHAN = 0;
STATE.CNT32 = 0;
STATE.UCAL = 0;
STATE.OVERFLOWDETECTION = 1;
STATE.NUMBER_OF_NAN_IN_BREAK = 100; %% default value, always include when several files are concatanated
STATE.FLAG_NUM_NAN = 0; %% flags whether explicit argument NUMBER_OF_NAN_IN_BREAK is used, only then NaNs are introduced between sweeps(segments) within single files
STATE.EOG_CORRECTION = 0 ;
STATE.BDF = [] ;
STATE.OUTPUT = 'double';
Fs = NaN;
k = 1;
while (k<=length(varargin))
if isnumeric(varargin{k})
if (k==1), CHAN=varargin{k}; end;
elseif ~isempty(strfind(varargin{k},'UCAL'))
MODE = varargin{k};
if strcmpi(varargin{k+1},'on');
STATE.UCAL = 1;
k=k+1;
elseif strcmpi(varargin{k+1},'off');
STATE.UCAL = 0;
k=k+1;
else
STATE.UCAL = 1;
end;
elseif ~isempty(strfind(varargin{k},'OVERFLOWDETECTION:OFF'))
MODE = varargin{k};
STATE.OVERFLOWDETECTION = 0;
elseif strcmpi(varargin{k},'OVERFLOWDETECTION')
if strcmpi(varargin{k+1},'on');
STATE.OVERFLOWDETECTION = 1;
elseif strcmpi(varargin{k+1},'off');
STATE.OVERFLOWDETECTION = 0;
elseif isnumeric(varargin{k+1})
STATE.OVERFLOWDETECTION = varargin{k+1};
end;
k=k+1;
elseif ~isempty(strfind(varargin{k},'OUTPUT:SINGLE'))
MODE = varargin{k};
STATE.OUTPUT = 'single';
elseif strcmpi(varargin{k},'OUTPUT')
STATE.OUTPUT = varargin{k+1};
elseif strcmpi(varargin{k},'EOG_CORRECTION')
if strcmpi(varargin{k+1},'on');
STATE.EOG_CORRECTION = 1;
elseif strcmpi(varargin{k+1},'off');
STATE.EOG_CORRECTION = 0;
elseif isnumeric(varargin{k+1})
STATE.EOG_CORRECTION = varargin{k+1};
end;
k=k+1;
elseif strcmpi(varargin{k},'Number_of_nan_in_break')
STATE.NUMBER_OF_NAN_IN_BREAK = varargin{k+1};
STATE.FLAG_NUM_NAN = 1;
k=k+1;
elseif strcmpi(varargin{k},'SampleRate')
Fs = varargin{k+1};
k=k+1;
elseif strcmpi(varargin{k},'BDF')
STATE.BDF = varargin{k+1};
k=k+1;
elseif strcmpi(varargin{k},'CNT32') || strcmpi(varargin{k},'32bit')
STATE.CNT32 = 1;
elseif exist(varargin{k},'file') && (k==1)
CHAN = varargin{k};
end;
k=k+1;
end;
%%% resolve wildcards %%%
if ischar(FILENAME)
if any(FILENAME=='*')
p = fileparts(FILENAME);
f = dir(FILENAME);
EOGix = zeros(1,length(f));
for k = 1:length(f);
f(k).name = fullfile(p,f(k).name);
[p,g,e] = fileparts(f(k).name);
lg = length(g);
if (lg>2) && strcmp(upper(g(lg+(-2:0))),'EOG')
EOGix(k) = 1;
end
end;
FILENAME={f([find(EOGix),find(~EOGix)]).name};
end;
end;
if nargout>0,
signal = [];
end;
if (iscell(FILENAME) && (length(FILENAME)==1)),
FILENAME = FILENAME{k};
end;
if ((iscell(FILENAME) || isstruct(FILENAME))),
signal = [];
for k = 1:length(FILENAME),
if iscell(FILENAME(k))
f = FILENAME{k};
else
f = FILENAME(k);
end
[s,h] = sload(f,varargin{:});
if k==1,
H = h;
signal = s;
H.SegLen = [0,size(s,1)];
%H.EVENT.POS = [H.EVENT.POS; 0];
%H.EVENT.TYP = [H.EVENT.TYP; hex2dec('7ffe')];
%if isfield(H.EVENT,'CHN');
% H.EVENT.CHN = [H.EVENT.CHN; 0];
%end;
%if isfield(H.EVENT,'DUR');
% H.EVENT.DUR = [H.EVENT.DUR; size(s,1)];
%end;
%if isfield(H.EVENT,'Desc'); % TFM-Excel-Beat-to-Beat
% H.EVENT.Desc = [H.EVENT.Desc; {'New Segment'}; h.EVENT.Desc];
%end;
else
H.FILE(k) = h.FILE;
H.T0(k,1:6) = h.T0;
if ~isnan(h.SampleRate) && (H.SampleRate ~= h.SampleRate),
fprintf(2,'Warning SLOAD: sampling rates of multiple files differ %i!=%i.\n',H.SampleRate, h.SampleRate);
end;
if size(s,2)==size(signal,2), %(H.NS == h.NS)
signal = [signal; repmat(NaN,STATE.NUMBER_OF_NAN_IN_BREAK,size(s,2)); s];
H.SegLen = [H.SegLen,size(signal,1)];
else
error('ERROR SLOAD: incompatible channel numbers %i!=%i of multiple files\n',H.NS,h.NS);
end;
if ~isempty(h.EVENT.POS),
H.EVENT.POS = [H.EVENT.POS; size(signal,1); h.EVENT.POS+size(signal,1)-size(s,1)];
H.EVENT.TYP = [H.EVENT.TYP; hex2dec('7ffe'); h.EVENT.TYP];
if isfield(H.EVENT,'CHN');
H.EVENT.CHN = [H.EVENT.CHN; 0; h.EVENT.CHN];
end;
if isfield(H.EVENT,'DUR');
H.EVENT.DUR = [H.EVENT.DUR; size(s,1); h.EVENT.DUR];
end;
if isfield(H.EVENT,'Desc'); % TFM-Excel-Beat-to-Beat
H.EVENT.Desc = [H.EVENT.Desc; {'New Segment'}; h.EVENT.Desc];
end;
end;
if ~isequal(H.Label,h.Label) || ~isequal(H.PhysDimCode,h.PhysDimCode)
warning('Labels and PhysDim of multiple files differ!\n');
for k2 = 1:length(H.InChanSelect),
k1 = H.InChanSelect(k2);
if ~strcmp(H.Label{k1},h.Label{k1}) || (H.PhysDimCode(k1)~=h.PhysDimCode(k1)),
warning('#%02i: %s | %s | (%i)%s | (%i)%s\n',k1, H.Label{k1},h.Label{k1}, H.PhysDimCode(k1),H.PhysDim{k1},h.PhysDimCode(k1),h.PhysDim{k1});
end;
end;
end;
if ~isequal(H.Calib,h.Calib) && H.FLAG.UCAL,
error('SLOAD: concatanating uncalibrated data with different scaling factors does not make sense!');
end;
if isfield(H,'TriggerOffset'),
if H.TriggerOffset ~= h.TriggerOffset,
fprintf(2,'Warning SLOAD: Triggeroffset %f does not fit.%f \n',H.TriggerOffset,h.TriggerOffset);
end;
end;
if isfield(h,'TRIG'),
if ~isfield(H,'TRIG'),
H.TRIG = [];
end;
H.TRIG = [H.TRIG(:); h.TRIG(:)+size(signal,1)-size(s,1)];
else h.TRIG=[];
end;
if isfield(h,'Classlabel'),
if isfield(h,'ArtifactSelection'),
if (any(h.ArtifactSelection>1) || (length(h.ArtifactSelection) < length(h.Classlabel)))
sel = zeros(size(h.Classlabel));
sel(h.ArtifactSelection) = 1;
else
sel = h.ArtifactSelection(:);
end;
else
h.ArtifactSelection = repmat(logical(0),length(h.Classlabel),1);
end;
if isfield(H,'ArtifactSelection')
H.ArtifactSelection = [H.ArtifactSelection; h.ArtifactSelection(:)];
else
H.ArtifactSelection = [repmat(logical(0),length(H.Classlabel),1); h.ArtifactSelection(:)];
end;
H.Classlabel = [H.Classlabel(:); h.Classlabel(:)];
elseif isfield(H,'Classlabel') && (length(h.TRIG)>0),
warning(sprintf('field classlabel missing in %s. This might corrupt the resulting classlabels!',h.FileName));
end;
clear s;
end;
end;
% fprintf(1,'SLOAD: data segments are concatenated with NaNs in between.\n');
return;
end;
%%% end of multi-file section
%%%% start of single file section
%%%%%%%%%% --------- EOG CORRECTION -------------- %%%%%%
if STATE.EOG_CORRECTION,
try
h = get_regress_eog(FILENAME,'REG fft16');
catch,
fprintf(H.FILE.stderr,'Error: SLOAD (EOG_CORRECTION): %s\n',lasterr);
H.FLAG.EOG_CORRECTION = 0;
end;
end;
FlagLoaded = 0;
if exist('mexSLOAD','file')==3,
try
valid_rerefmx = 1;
if ischar(CHAN)
HDR = sopen(CHAN,'r'); HDR=sclose(HDR);
if isfield(HDR.Calib),
ReRefMx = HDR.Calib;
else
valid_rerefmx = 0;
end;
elseif all(size(CHAN)>1) || any(floor(CHAN)~=CHAN) || any(CHAN<0) || (any(CHAN==0) && (numel(CHAN)>1));
ReRefMx = CHAN;
CHAN = find(any(CHAN,2));
elseif all(CHAN>0) && all(floor(CHAN)==CHAN),
[tmp,ix]= sort(CHAN);
ReRefMx = sparse(CHAN,1:length(CHAN),1);
else
ReRefMx = [];
valid_rerefmx=0;
end
if STATE.EOG_CORRECTION,
ReRefMx = h.r0*ReRefMx;
valid_rerefmx=1;
end
if STATE.OVERFLOWDETECTION,
arg1 = 'OVERFLOWDETECTION:ON';
else
arg1 = 'OVERFLOWDETECTION:OFF';
end
if STATE.UCAL,
arg2 = {'UCAL:ON'};
else
arg2 = {'UCAL:OFF'};
end
if STATE.CNT32
arg2 = {arg2,'CNT32'};
end
if ~valid_rerefmx,
[signal,HDR] = mexSLOAD(FILENAME,0,arg1,arg2{:});
if isfield(HDR,'ErrNum') && (HDR.ErrNum>0),
fprintf(1,'%s\n',HDR.ErrMsg);
H = HDR;
x = HDR.nonsense; % hack: triggers CATCH branch in TRY-CATCH-END;
end
if isfield(HDR.FLAG,'ROW_BASED_CHANNELS') && HDR.FLAG.ROW_BASED_CHANNELS, signal = signal.'; end;
FlagLoaded = isfield(HDR,'NS');
HDR.InChanSelect = 1:HDR.NS;
else
InChanSelect = find(any(ReRefMx,2));
[signal,HDR] = mexSLOAD(FILENAME,InChanSelect,arg1,arg2{:});
if isfield(HDR,'ErrNum') && HDR.ErrNum==3,
%% file not found - fopen failed
H = HDR;
return;
end
if isfield(HDR.FLAG,'ROW_BASED_CHANNELS') && HDR.FLAG.ROW_BASED_CHANNELS, signal = signal.'; end;
FlagLoaded = isfield(HDR,'NS');
HDR.InChanSelect = InChanSelect(InChanSelect <= HDR.NS);
signal = signal*ReRefMx(HDR.InChanSelect,:); %% can be sparse if just a single channel is loaded
signal = full(signal); %% make sure signal is not sparse
end;
HDR = bdf2biosig_events(HDR, STATE.BDF);
HDR.T0 = datevec(HDR.T0);
HDR.Patient.Birthday = datevec(HDR.Patient.Birthday);
HDR.Calib = [HDR.Off(:)';diag(HDR.Cal)];
[HDR.FILE.Path,HDR.FILE.Name,HDR.FILE.Ext] = fileparts(FILENAME);
HDR.FileName = FILENAME;
HDR = leadidcodexyz(HDR);
H=HDR;
H.FLAG.EOG_CORRECTION = STATE.EOG_CORRECTION;
if isfield(HDR,'Patient') && isfield(HDR.Patient,'Weight') && isfield(HDR.Patient,'Height')
%% Body Mass Index
HDR.Patient.BMI = HDR.Patient.Weight * HDR.Patient.Height^-2 * 1e4;
%% Body Surface Area
% DuBois D, DuBois EF. A formula to estimate the approximate surface area if height and weight be known. Arch Intern Medicine. 1916; 17:863-71.
% Wang Y, Moss J, Thisted R. Predictors of body surface area. J Clin Anesth. 1992; 4(1):4-10.
HDR.Patient.BSA = 0.007184 * HDR.Patient.Weight^0.425 * HDR.Patient.Height^0.725;
end;
if strcmp(H.TYPE,'GDF');
% Classlabels according to
% http://biosig.cvs.sourceforge.net/*checkout*/biosig/biosig/doc/eventcodes.txt
% sort event table before extracting HDR.Classlabel and HDR.TRIG
[H.EVENT.POS,ix] = sort(H.EVENT.POS);
H.EVENT.TYP = H.EVENT.TYP(ix);
if isfield(H.EVENT,'CHN')
H.EVENT.CHN = H.EVENT.CHN(ix);
end;
if isfield(H.EVENT,'DUR')
H.EVENT.DUR = H.EVENT.DUR(ix);
end;
if (length(H.EVENT.TYP)>0)
ix = (H.EVENT.TYP>hex2dec('0300')) & (H.EVENT.TYP<hex2dec('030d'));
ix = ix | ((H.EVENT.TYP>=hex2dec('0320')) & (H.EVENT.TYP<=hex2dec('037f')));
ix = ix | (H.EVENT.TYP==hex2dec('030f')); % unknown/undefined cue
H.Classlabel = mod(H.EVENT.TYP(ix),256);
H.Classlabel(H.Classlabel==15) = NaN; % unknown/undefined cue
end;
% Trigger information and Artifact Selection
ix = find(H.EVENT.TYP==hex2dec('0300'));
H.TRIG = H.EVENT.POS(ix);
ArtifactSelection = repmat(logical(0),length(ix),1);
for k = 1:length(ix),
ix2 = find(H.EVENT.POS(ix(k))==H.EVENT.POS);
if any(H.EVENT.TYP(ix2)==hex2dec('03ff'))
ArtifactSelection(k) = logical(1);
end;
end;
if any(ArtifactSelection), % define only if necessary
H.ArtifactSelection = ArtifactSelection;
end;
% apply channel selections to EVENT table
if valid_rerefmx && ~isempty(H.EVENT.POS) && (isfield(H.EVENT,'CHN')), % only if channels are selected.
sel = (H.EVENT.CHN(:)==0); % memory allocation, select all general events
for k = find(~sel'), % select channel specific elements
sel(k) = any(H.EVENT.CHN(k)==InChanSelect);
end;
H.EVENT.POS = H.EVENT.POS(sel);
H.EVENT.TYP = H.EVENT.TYP(sel);
H.EVENT.DUR = H.EVENT.DUR(sel); % if EVENT.CHN available, also EVENT.DUR is defined.
H.EVENT.CHN = H.EVENT.CHN(sel);
% assigning new channel number
a = zeros(1,HDR.NS);
for k = 1:length(InChanSelect), % select channel specific elements
a(InChanSelect(k)) = k; % assigning to new channel number.
end;
ix = H.EVENT.CHN>0;
H.EVENT.CHN(ix) = a(H.EVENT.CHN(ix)); % assigning new channel number
end;
elseif strcmp(H.TYPE,'BDF');
H.BDF.ANNONS = zeros(HDR.NRec*HDR.SPR,1);
ix = HDR.EVENT.TYP==hex2dec('7ffe');
ix1 = find(~ix);
POS = HDR.EVENT.POS(ix1);
TYP = HDR.EVENT.TYP(ix1);
[s,ix2] = sort([POS; HDR.NRec*HDR.SPR+1]);
for k = 1:length(s)-1,
H.BDF.ANNONS(s(k):s(k+1)-1) = TYP(ix2(k));
end;
H.BDF.ANNONS(HDR.EVENT.POS(ix)) = H.BDF.ANNONS(HDR.EVENT.POS(ix)) | hex2dec('10000');
elseif strcmp(H.TYPE,'BKR');
H.Classlabel = [];
H.TRIG = [];
tmp=fullfile(H.FILE.Path,[H.FILE.Name,'.mat']);
if ~exist(tmp,'file'),
tmp=fullfile(H.FILE.Path,[H.FILE.Name,'.MAT']);
end
x = [];
if exist(tmp,'file'),
x = load('-mat',tmp);
end;
if isfield(x,'header'),
H.MAT = x.header;
if isfield(x.header,'Setup'),
if isfield(x.header.Setup,'Bits'),
H.Bits = x.header.Setup.Bits;
[datatyp, limits, datatypes] = gdfdatatype(H.Bits+255);
% THRESHOLD for Overflow detection
if ~isfield(H,'THRESHOLD')
H.THRESHOLD = repmat(limits, H.NS, 1);
end;
end;
end;
if isfield(x.header,'Result') && isfield(x.header.Result,'Classlabel'),
H.Classlabel = x.header.Result.Classlabel;
end;
if isfield(x.header,'Paradigm')
if isempty(H.Classlabel) && isfield(x.header.Paradigm,'Classlabel')
H.Classlabel = x.header.Paradigm.Classlabel;
end;
H.BCI.Paradigm = x.header.Paradigm;
if isfield(H.BCI.Paradigm,'TriggerOnset');
H.TriggerOffset = H.BCI.Paradigm.TriggerOnset;
elseif isfield(H.BCI.Paradigm,'TriggerTiming');
% H.BCI.Paradigm.TriggerTiming,
H.TriggerOffset = H.BCI.Paradigm.TriggerTiming;
fprintf(2,'Warning BKROPEN: Paradigm.TriggerOnset is unknown. Paradigm.TriggerTiming= %f ms is used instead\n',H.TriggerOffset);
end;
end;
if isfield(x.header,'PhysioRec'), % R. Leeb's data
H.Label = cellstr(x.header.PhysioRec);
end;
if isfield(x.header,'BKRHeader'), % R. Scherer Data
if isfield(x.header.BKRHeader,'TO'),
H.T0 = x.header.BKRHeader.TO;
end;
if isfield(x.header.BKRHeader,'Label'),
H.Label = cellstr(x.header.BKRHeader.Label);
ns = H.NS-length(H.Label);
if ns == 1;
H.Label = strvcat(H.Label,'TRIGGER');
elseif ns > 1;
H.Label = strvcat(H.Label,char(repmat('n.a.',ns,1)));
end;
end;
end;
if isfield(x.header,'Model'), % More
if isfield(x.header.Model,'AnalogInput'),
for k = 1:length(x.header.Model.AnalogInput),
H.Filter.HighPass(k) = x.header.Model.AnalogInput{k}{5};
H.Filter.LowPass(k) = x.header.Model.AnalogInput{k}{6};
H.Filter.Notch(k) = strcmpi(x.header.Model.AnalogInput{k}{7},'on');
H.MAT.Cal(k) = x.header.Model.AnalogInput{k}{3};
end
end;
end;
if ~isempty(strmatch('TRIGGER',H.Label))
H.AS.TRIGCHAN = H.NS; %strmatch('TRIGGER',H.Label);
end;
end;
if 1; ~isfield(H,'Classlabel');
tmp=fullfile(H.FILE.Path,[H.FILE.Name,'.par']);
if ~exist(tmp,'file'),
tmp=fullfile(H.FILE.Path,[H.FILE.Name,'.PAR']);
end
if exist(tmp,'file'),
H.Classlabel = load(tmp);
end;
end;
%%% Artifact Selection files
tmp1=fullfile(H.FILE.Path,[H.FILE.Name,'.sel']);
if ~exist(tmp1,'file'),
tmp1=fullfile(H.FILE.Path,[H.FILE.Name,'.SEL']);
end
tmp2 = fullfile(H.FILE.Path,[H.FILE.Name,'_artifact.mat']);
SW = (exist(tmp1,'file')>0) + 2*(exist(tmp2,'file')>0);
if SW == 0,
elseif SW == 1,
if exist('OCTAVE_VERSION','builtin')
H.ArtifactSelection = load('-ascii',tmp1);
else
H.ArtifactSelection = load(tmp1);
end;
elseif SW == 2,
if exist('OCTAVE_VERSION','builtin')
tmp = load('-mat',tmp2);
else
tmp = load(tmp2);
end;
H.ArtifactSelection = tmp.artifact(:);
elseif SW == 3,
fprintf(H.FILE.stderr,'Warning BKROPEN: more than one ArtifactSelection files. File %s is used.\n',tmp1);
if exist('OCTAVE_VERSION')>5
H.ArtifactSelection = load('-ascii',tmp1);
else
H.ArtifactSelection = load(tmp1);
end;
end;
if isfield(H,'ArtifactSelection'),
if any(H.ArtifactSelection>1) || (length(H.ArtifactSelection)<length(H.Classlabel))
sel = zeros(size(H.Classlabel));
sel(H.ArtifactSelection) = 1;
H.ArtifactSelection = sel(:);
end;
H.ArtifactSelection = H.ArtifactSelection(:);
end;
if isfield(H.AS,'TRIGCHAN') % & isempty(H.EVENT.POS)
if H.AS.TRIGCHAN <= H.NS, %size(H.data,2),
H.THRESHOLD(H.AS.TRIGCHAN,1:2) = [-1-2^15,2^15]; % do not apply overflow detection for Trigger channel
data = mexSLOAD(H.FileName,H.AS.TRIGCHAN,'UCAL:ON','OVERFLOWDETECTION:OFF');
TRIGon = gettrigger(data);
%TRIGoff = gettrigger(-double(H.data(:,H.AS.TRIGCHAN)));
if isfield(H,'TriggerOffset')
TRIGon = TRIGon - round(H.TriggerOffset/1000*H.SampleRate);
% TRIGoff = TRIGoff - round(H.TriggerOffset/1000*H.SampleRate);
end;
end;
H.TRIG = TRIGon(:);
H.EVENT.POS = TRIGon(:); %[TRIGon(:); TRIGoff(:)];
H.EVENT.TYP = repmat(hex2dec('0300'),numel(TRIGon),1); %repmat(hex2dec('8300'),numel(TRIGoff),1)];
end;
if length(H.TRIG)~=length(H.Classlabel),
% hack to deal with BCI22 data
fprintf(2,'Warning BKROPEN: Number of triggers (%i) and number of Classlabels (%i) do not fit\n',length(H.TRIG),length(H.Classlabel));
H.TRIG = [];
H.Classlabel = [];
H.ArtifactSelection = [];
end;
%% end of BKR
elseif strcmp(H.TYPE,'BrainVision');
try
H = bv2biosig_events(H);
catch
%warning('bv2biosig_events not executed');
end;
elseif strcmp(H.TYPE,'EDF') && isfield(H.EVENT,'Desc');
if length(H.EVENT.TYP)==length(H.EVENT.Desc)
[H.EVENT.CodeDesc, CodeIndex, H.EVENT.TYP] = unique(H.EVENT.Desc);
end;
%% end of BKR
elseif strcmp(H.TYPE,'BrainVision');
HDR = sopen(fullfile(H.FILE.Path,[H.FILE.Name,'.vmrk']));
HDR = sclose(HDR);
H.EVENT = HDR.EVENT;
elseif strcmp(H.TYPE,'PDP');
signal = repmat(NaN,max(HDR.EVENT.POS),HDR.NS);
end;
if isempty(signal);
signal = repmat(NaN,round(max(HDR.EVENT.POS)*HDR.SampleRate/HDR.EVENT.SampleRate),HDR.NS);
for k = 1:HDR.NS,
ix = find(HDR.EVENT.CHN==k);
signal(round(HDR.EVENT.POS(ix)*HDR.SampleRate/HDR.EVENT.SampleRate),k)=HDR.EVENT.DUR(ix);
end;
end;
H.CHANTYP = repmat(' ',1,H.NS);
for k=1:H.NS,
if ~isempty(strfind(lower(H.Label{k}),'eeg')) H.CHANTYP(k) = 'E';
elseif ~isempty(strfind(lower(H.Label{k}),'meg')) H.CHANTYP(k) = 'E';
elseif ~isempty(strfind(lower(H.Label{k}),'emg')) H.CHANTYP(k) = 'M';
elseif ~isempty(strfind(lower(H.Label{k}),'eog')) H.CHANTYP(k) = 'O';
elseif ~isempty(strfind(lower(H.Label{k}),'ecg')) H.CHANTYP(k) = 'C';
elseif ~isempty(strfind(lower(H.Label{k}),'air')) H.CHANTYP(k) = 'R';
elseif ~isempty(strfind(lower(H.Label{k}),'trig')) H.CHANTYP(k) = 'T';
end;
end;
catch
%fprintf(1,lasterr);
fprintf(1, 'SLOAD: mexSLOAD failed - the slower M-function is used.\n');
end;
else
global FLAG_HINT_mexSLOAD;
if isempty(FLAG_HINT_mexSLOAD)
fprintf(1, 'Hint: the performance of SLOAD can be improved with mexSLOAD.mex which is part of biosig4c++.\n');
FLAG_HINT_mexSLOAD = 1; % turn off hint
end;
end;
if ~FlagLoaded,
H = getfiletype(FILENAME);
if isempty(H)
fprintf(2,'Warning SLOAD: no file found\n');
return;
else
% FILENAME can be fn.name struct, or HDR struct.
FILENAME = H.FileName;
end;
H.FLAG.UCAL = STATE.UCAL;
H.FLAG.OVERFLOWDETECTION = STATE.OVERFLOWDETECTION;
H.FLAG.EOG_CORRECTION = STATE.EOG_CORRECTION;
H.FLAG.OUTPUT = STATE.OUTPUT;
if strncmp(H.TYPE,'IMAGE:',5)
[H] = iopen(H);
if H.FILE.OPEN,
signal = iread(H);
H.FILE.OPEN = 0;
fclose(H.FILE.FID);
end;
return;
end;
%%%%%%%%%%%%%%% --------- Load single file ------------%%%%%%%%%%%
H = sopen(H,'r',CHAN,MODE);
if 0, ~isnan(H.NS),
%------ ignore 'NaC'-channels
NS = size(H.Calib,2);
SelMx = speye(NS);
ch = 1:NS; %ch(strmatch('NaC',H.Label))=[];
if length(ch)<NS,
fprintf(2,'Warning SLOAD: Some NaC channels have been removed %s\n',H.FileName);
end;
SelMx = SelMx(:,ch);
H.Calib = H.Calib*SelMx;
% generate correction matrix
if H.FLAG.EOG_CORRECTION,
H.Calib = H.Calib*h.REGRESS.r0;
if ~isequal(H.Label(ch),h.Label)
fprintf(2, 'Warning SLOAD (EOG correction): Channel labels in %s do not fit with arti* recording!\n',H.FileName);
end;
elseif STATE.EOG_CORRECTION,
fprintf(2, 'Warning: EOG correction not supported for this file (%s)!\n',H.FileName);
end;
%----- generate HDR.Calib -----
if all(size(CHAN)>1) || any(floor(CHAN)~=CHAN) || any(CHAN<0) || (any(CHAN==0) && (numel(CHAN)>1));
ReRefMx = CHAN;
CHAN = find(any(CHAN,2));
elseif all(CHAN>0) && all(floor(CHAN)==CHAN),
if any(diff(CHAN)<=0),
% fprintf(HDR.FILE.FID,'Warning SOPEN: CHAN-argument not sorted - header information like Labels might not correspond to data.\n');
end;
ReRefMx = sparse(CHAN,1:length(CHAN),1,H.NS,length(CHAN));
else %if (CHAN==0)
ReRefMx = [];
end
if ~isempty(ReRefMx),
%[size(H.Calib),size(SelMx),size(h.REGRESS.r0),size(ReRefMx)]
ReRefMx = [ReRefMx(1:min(size(ReRefMx,1),size(H.Calib,2)),:); zeros(max(0,size(H.Calib,2)-size(ReRefMx,1)),size(ReRefMx,2))];
H.Calib = H.Calib*ReRefMx;
end;
H.InChanSelect = find(any(H.Calib(2:end,:),2));
H.Calib = H.Calib([1;1+H.InChanSelect(:)],:);
end
if 0,
elseif isfield(H,'data') && all(diag(H.Calib(2:end,1:end))==1) && ~H.FLAG.OVERFLOWDETECTION,
% only a single copy of the data
% important for large data sets, close to the available memory
signal = H.data;
H = rmfield(H,'data');
if (CHAN>0)
signal = signal(:,CHAN);
end;
elseif any(strmatch(H.TYPE,{'native','TFM_EXCEL_Beat_to_Beat','EEProbe-CNT','EEProbe-AVR'}));
[signal,H] = sread(H);
H = sclose(H);
elseif (H.FILE.OPEN > 0)
[signal,H] = sread(H);
H = sclose(H);
elseif (H.FILE.OPEN > 0)
signal = repmat(NaN,H.SPR*H.NRec,size(H.Calib,2));
k1 = 0;
while ~seof(H),
[s,H] = sread(H,100);
if 1,
k2 = size(s,1);
signal(k1+1:k1+k2,:)=s;
k1 = k1+k2;
else
%fprintf('%i/%i\n',stell(H),H.SPR*H.NRec);
signal=[signal;s];
end;
end;
H = sclose(H);
elseif strncmp(H.TYPE,'EVENT',5)
signal = H.EVENT;
return;
elseif strncmp(H.TYPE,'ELPOS',5)
signal = H.ELEC.XYZ;
return;
elseif strcmp(H.TYPE,'DAQ')
fprintf(1,'Loading a matlab DAQ data file - this can take a while.\n');
tic;
[signal, tmp, H.DAQ.T0, H.DAQ.events, DAQ.info] = daqread(H.FileName);
fprintf(1,'Loading DAQ file finished after %.0f s.\n',toc);
[H.SPR,H.NS] = size(signal);
H.SampleRate = DAQ.info.ObjInfo.SampleRate;
sz = size(signal);
if length(sz)==2, sz=[1,sz]; end;
H.NRec = sz(1);
H.Dur = sz(2)/H.SampleRate;
H.NS = sz(3);
H.FLAG.TRIGGERED = H.NRec>1;
H.FLAG.UCAL = 1;
H.PhysDim = {DAQ.info.ObjInfo.Channel.Units};
H.DAQ = DAQ.info.ObjInfo.Channel;
H.Cal = diff(cat(1,DAQ.info.ObjInfo.Channel.InputRange),[],2).*(2.^(-DAQ.info.HwInfo.Bits));
H.Off = cat(1,DAQ.info.ObjInfo.Channel.NativeOffset);
H.Calib = sparse([H.Off';eye(H.NS)]*diag(H.Cal));
if CHAN<1,
CHAN = 1:H.NS;
end;
if ~H.FLAG.UCAL,
Calib = H.Calib; % Octave can not index sparse matrices within a struct
signal = [ones(size(signal,1),1),signal]*Calib(:,CHAN);
end;
elseif 0, strcmp(H.TYPE,'BIFF'),
try,
[H.TFM.S,H.TFM.E] = xlsread(H.FileName,'Beat-To-Beat');
if size(H.TFM.S,1)+1==size(H.TFM.E,1),
H.TFM.S = [repmat(NaN,1,size(H.TFM.S,2));H.TFM.S];
end;
H.TYPE = 'TFM_EXCEL_Beat_to_Beat';
catch
try
[H.TFM.S,H.TFM.E] = xlsread(H.FileName,'Beat-to-Beat');
H.TYPE = 'TFM_EXCEL_Beat_to_Beat';
catch
0;
end;
end;
if strcmp(H.TYPE, 'TFM_EXCEL_Beat_to_Beat');
if ~isempty(strfind(H.TFM.E{3,1},'---'))
H.TFM.S(3,:) = [];
H.TFM.E(3,:) = [];
end;
H.Label = strvcat(H.TFM.E{4,:});
H.PhysDim = strvcat(H.TFM.E{5,:});
H.TFM.S = H.TFM.S(6:end,:);
H.TFM.E = H.TFM.E(6:end,:);
ix = find(isnan(H.TFM.S(:,2)) & ~isnan(H.TFM.S(:,1)));
H.EVENT.Desc = H.TFM.E(ix,2);
H.EVENT.POS = ix;
if any(CHAN),
H.TFM.S = H.TFM.S(:,CHAN);
H.TFM.E = H.TFM.E(:,CHAN);
end;
[H.SPR,H.NS] = size(H.TFM.S);
H.NRec = 1;
H.THRESHOLD = repmat([0,NaN],H.NS,1);
signal = H.TFM.S;
signal(signal==0) = NaN;
end;
elseif strcmp(H.TYPE,'MatrixMarket'),
H.FILE.FID = fopen(H.FileName,'rt','ieee-le');
line = fgetl(H.FILE.FID);
H.FLAG.Coordinate = ~isempty(strfind(line,'coordinate'));
H.FLAG.Array = ~isempty(strfind(line,'array'));
H.FLAG.Complex = ~isempty(strfind(line,'complex'));
H.FLAG.Real = ~isempty(strfind(line,'real'));
H.FLAG.Integer = ~isempty(strfind(line,'integer'));
H.FLAG.Pattern = ~isempty(strfind(line,'pattern'));
H.FLAG.General = ~isempty(strfind(line,'general'));
H.FLAG.Symmetric = ~isempty(strfind(line,' symmetric'));
H.FLAG.SkewSymmetric = ~isempty(strfind(line,'skew-symmetric'));
H.FLAG.Hermitian = ~isempty(strfind(lower(line),'hermitian'));
while strncmp(line,'%',1)
line = fgetl(H.FILE.FID);
end;
[tmp,status] = str2double(line);
if any(status)
fprintf(H.FILE.stderr,'SLOAD (MM): invalid size %s\n',line);
else
H.MATRIX.Size = tmp;
end;
if length(H.MATRIX.Size)==3,
H.Length = tmp(3);
signal = sparse([],[],[],tmp(1),tmp(2),tmp(3));
for k = 1:H.Length,
line = fgetl(H.FILE.FID);
[tmp,status] = str2double(line);
if any(status)
fprintf(H.FILE.stderr,'SLOAD (MM): invalid size %s\n',line);
elseif length(tmp)==4,
val = tmp(3) + i*tmp(4);
elseif length(tmp)==3,
val = tmp(3);
elseif length(tmp)==2,
val = 1;
else
fprintf(H.FILE.stderr,'SLOAD (MM): invalid size %s\n',line);
end;
if H.FLAG.General,
signal(tmp(1),tmp(2)) = val;
elseif H.FLAG.Symmetric,
signal(tmp(1),tmp(2)) = val;
signal(tmp(2),tmp(1)) = val;
elseif H.FLAG.SkewSymmetric,
signal(tmp(1),tmp(2)) = val;
signal(tmp(2),tmp(1)) =-val;
elseif H.FLAG.Hermitian,
signal(tmp(1),tmp(2)) = val;
signal(tmp(2),tmp(1)) = conj(val);
else
fprintf(H.FILE.stderr,'SLOAD (MM): invalid size %s\n',line);
end;
end;
elseif length(H.MATRIX.Size)==2
H.Length = prod(tmp);
signal = zeros(H.MATRIX.Size);
if H.FLAG.General==1,
[IX,IY]=find(ones(H.MATRIX.Size));
else
[IX,IY]=find(cumsum(eye(H.MATRIX.Size)));
end;
for k = 1:H.Length,
line = fgetl(H.FILE.FID);
[tmp,status] = str2double(line);
if any(status)
error('SLOAD (MM)');
elseif length(tmp)==2,
val=tmp(1) + i*tmp(2);
elseif length(tmp)==1,
val=tmp(1);
else
fprintf(H.FILE.stderr,'SLOAD (MM): invalid size %s\n',line);
end;
signal(IX(k),IY(k)) = val;
if H.FLAG.Symmetric,
signal(IY(k),IX(k)) = val;
elseif H.FLAG.SkewSymmetric,
signal(IY(k),IX(k)) =-val;
elseif H.FLAG.Hermitian,
signal(IY(k),IX(k)) = conj(val);
else
fprintf(H.FILE.stderr,'SLOAD (MM): invalid size %s\n',line);
end;
end;
end;
fclose(H.FILE.FID);
elseif strcmp(H.TYPE,'OFF'),
H.FILE.FID = fopen(H.FileName,'rt','ieee-le');
line1 = fgetl(H.FILE.FID);
line2 = fgetl(H.FILE.FID);
while ~feof(H.FILE.FID) && (line2(1)=='#'),
line2 = fgetl(H.FILE.FID);
end;
[tmp,status] = str2double(line2);
if status || (size(tmp,2)~=3),
fclose(H.FILE.FID);
error('SOPEN (OFF)');
else
H.VertexCount = tmp(1);
H.FaceCount = tmp(2);
H.EdgeCount = tmp(3);
end
H.Vertex = repmat(NaN,H.VertexCount,3);
for k = 1:H.VertexCount,
line = '';
while isempty(line) || strncmp(line,'#',1)
line = fgetl(H.FILE.FID);
end;
len = min(length(line),min(find(line=='#')));
tmp = str2double(line(1:len));
H.Vertex(k,:) = tmp(1:H.ND);
end;
% H.Face = repmat(NaN,H.FaceCount,3);
for k = 1:H.FaceCount,
line = '';
while isempty(line) || strncmp(line,'#',1)
line = fgetl(H.FILE.FID);
end;
len = min(length(line),min(find(line=='#')));
tmp = str2double(line(1:len));
H.Ngon(k) = tmp(1);
H.Face{k} = tmp(2:tmp(1)+1) + 1;
end;
if all(H.Ngon(1)==H.Ngon),
H.Face = cat(1,H.Face{:});
end;
fclose(H.FILE.FID);
elseif strcmp(H.TYPE,'POLY'),
H.FILE.FID = fopen(H.FileName,'rt','ieee-le');
K = 0;
while ~feof(H.FILE.FID)
line = fgetl(H.FILE.FID);
if isempty(line),
elseif line(1)=='#',
else
K = K + 1;
end;
end;
fclose(H.FILE.FID);
elseif strcmp(H.TYPE,'SMF'),