Debian Codegraph @

biosig4c++/t210/sopen_matio.c

raw symbols SCIP
1 /*
2
3 Copyright (C) 2021 Alois Schloegl <[email protected]>
4
5 This file is part of the "BioSig for C/C++" repository
6 (biosig4c++) at http://biosig.sf.net/
7
8 BioSig is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 3
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef WITH_MATIO
31 #include <matio.h>
32 #endif
33 #include "../biosig.h"
34
35
36 int sopen_matlab(HDRTYPE* hdr) {
37 #ifdef WITH_MATIO
38 /*
39 file hdr->FileName is already opened and hdr->HeadLen bytes are read
40 These are available from hdr->AS.Header.
41
42 ToDo: populate hdr
43 sanity checks
44 memory leaks
45 */
46 ifclose(hdr);
47 //size_t count = hdr->HeadLen;
48 // TODO: identify which type/origin of MATLAB
49
50 fprintf(stdout, "Trying to read Matlab data using MATIO v%i.%i.%i\n", MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION, MATIO_RELEASE_LEVEL);
51
52 mat_t *matfile = Mat_Open(hdr->FileName, MAT_ACC_RDONLY);
53 matvar_t *EEG=NULL, *pnts=NULL, *nbchan=NULL, *trials=NULL, *srate=NULL, *data=NULL, *chanlocs=NULL, *event=NULL;
54 if (matfile != NULL) {
55 EEG = Mat_VarRead(matfile, "EEG" );
56 if (EEG != NULL) {
57 Mat_VarReadDataAll(matfile, EEG );
58 pnts = Mat_VarGetStructField(EEG, "pnts", MAT_BY_NAME, 0);
59 nbchan = Mat_VarGetStructField(EEG, "nbchan", MAT_BY_NAME, 0);
60 trials = Mat_VarGetStructField(EEG, "trials", MAT_BY_NAME, 0);
61 srate = Mat_VarGetStructField(EEG, "srate", MAT_BY_NAME, 0);
62 data = Mat_VarGetStructField(EEG, "data", MAT_BY_NAME, 0);
63 chanlocs = Mat_VarGetStructField(EEG, "chanlocs", MAT_BY_NAME, 0);
64 event = Mat_VarGetStructField(EEG, "event", MAT_BY_NAME, 0);
65
66 hdr->NS = *(double*)(nbchan->data);
67 hdr->SPR = *(double*)(pnts->data);
68 hdr->NRec= *(double*)(trials->data);
69 hdr->SampleRate = *(double*)(srate->data);
70
71 /* TODO CB
72 hdr->NRec = ;
73 hdr->SPR = ;
74 hdr->T0 = 0; // Unknown;
75 */
76 uint16_t gdftyp = 16; // 16: float; 17: double
77 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
78 size_t k;
79 for (k=0; k<hdr->NS; k++) {
80 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
81 sprintf(hc->Label,"#%2d",k+1);
82 hc->SPR = hdr->SPR;
83 /* TODO CB
84 hc->GDFTYP = gdftyp;
85 hc->Transducer[0] = '\0';
86 hc->LowPass = ;
87 hc->HighPass = ;
88 hc->Notch = ; // unknown
89 hc->PhysMax = ;
90 hc->DigMax = ;
91 hc->PhysMin = ;
92 hc->DigMin = ;
93 hc->Cal = 1.0;
94 hc->Off = 0.0;
95 hc->OnOff = 1;
96 hc->PhysDimCode = 4275; // uV
97 hc->LeadIdCode = 0;
98 hc->bi = k*GDFTYP_BITS[gdftyp]>>3; // TODO AS
99 */
100 }
101
102 size_t sz = hdr->NS*hdr->SPR*hdr->NRec*GDFTYP_BITS[gdftyp]>>3;
103 hdr->AS.rawdata = realloc(hdr->AS.rawdata, sz);
104 /* TODO CB
105 memcpy(hdr->AS.rawdata,...,sz);
106 */
107 hdr->EVENT.N = 0; // TODO CB
108 hdr->EVENT.POS = (uint32_t*) realloc(hdr->EVENT.POS, hdr->EVENT.N*sizeof(*hdr->EVENT.POS));
109 hdr->EVENT.TYP = (uint16_t*) realloc(hdr->EVENT.TYP, hdr->EVENT.N*sizeof(*hdr->EVENT.TYP));
110 hdr->EVENT.DUR = (uint32_t*) realloc(hdr->EVENT.DUR, hdr->EVENT.N*sizeof(*hdr->EVENT.DUR));
111 hdr->EVENT.CHN = (uint16_t*) realloc(hdr->EVENT.CHN, hdr->EVENT.N*sizeof(*hdr->EVENT.CHN));
112 for (k=0; k<hdr->EVENT.N; k++) {
113 /* TODO CB
114 hdr->EVENT.TYP[k] =
115 FreeTextEvent(hdr, k, annotation)
116 hdr->EVENT.POS[k] =
117 hdr->EVENT.CHN[k] = 0;
118 hdr->EVENT.DUR[k] = 0;
119 */
120 }
121
122 hdr->AS.bpb = hdr->NS*2;
123 hdr->FLAG.OVERFLOWDETECTION = 0; // BKR does not support automated overflow and saturation detection
124
125
126 Mat_VarPrint(pnts, 1);
127 Mat_VarPrint(nbchan, 1);
128 Mat_VarPrint(trials, 1);
129 Mat_VarPrint(srate, 1);
130 //Mat_VarPrint(data, 1);
131 //Mat_VarPrint(chanlocs, 1);
132 //Mat_VarPrint(event, 1);
133
134
135 Mat_VarFree(EEG);
136 }
137
138 Mat_Close(matfile);
139 }
140
141 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Error reading MATLAB file");
142 return(-1);
143
144 #else // WITH_MATIO
145 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "SOPEN(MATIO): - matlab format not supported - libbiosig need to be recompiled with libmatio support.");
146 ifclose(hdr);
147 return(-1);
148 #endif // WITH_MATIO
149 }
150