Debian Codegraph @

biosig4c++/t210/sopen_cadwell_read.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
31 #include "../biosig.h"
32
33 /******************************************************************************
34 read CADWELL file formats (EAS, EZ3, ARC)
35 ******************************************************************************/
36 void sopen_cadwell_read(HDRTYPE* hdr) {
37
38 if (VERBOSE_LEVEL > 8) fprintf(stdout,"%s (line %d) %s(hdr)\n", __FILE__, __LINE__, __func__);
39
40 if (hdr->TYPE==EAS) {
41 /* 12 bit ADC ?
42 200 Hz
43 77*800 samples
44 EEGData section has a periodicity of 202*2 (404 bytes)
45 800samples*16channels*2byte=25600 = 0x6400)
46 */
47 hdr->NS = 16; // so far all example files had 16 channels
48 hdr->SPR = 1;
49 hdr->NRec = 0;
50 hdr->SampleRate = 250;
51 unsigned lengthHeader0 = leu32p(hdr->AS.Header + 0x30);
52 unsigned lengthHeader1 = leu32p(hdr->AS.Header + 0x34); // this is varying amoung data sets - meaning unknown
53 assert(lengthHeader0==0x0400);
54
55 for (int k=0; k*0x20 < lengthHeader1; k++) {
56 char *sectName = hdr->AS.Header + 0x6c + k*0x20;
57 size_t sectPos= leu32p(hdr->AS.Header + 0x7c + k*0x20);
58 size_t sectN1 = leu32p(hdr->AS.Header + 0x80 + k*0x20);
59 size_t sectN2 = leu32p(hdr->AS.Header + 0x84 + k*0x20);
60 size_t sectN3 = leu32p(hdr->AS.Header + 0x88 + k*0x20);
61
62 if (!sectPos
63 || memcmp("SctHdr\0\0", hdr->AS.Header+sectPos, 8)
64 || memcmp(hdr->AS.Header+sectPos+8, sectName, 16))
65 {
66 if (VERBOSE_LEVEL > 8) fprintf(stdout,"%s (line %d): break loop (0x%x %s)\n",__FILE__,__LINE__, sectPos, sectName);
67 break;
68 }
69
70 uint64_t curSec, nextSec;
71 int flag=1;
72 do {
73 curSec = leu64p(hdr->AS.Header + sectPos + 24);
74 nextSec = leu64p(hdr->AS.Header + sectPos + 32);
75
76 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d): 0x%08x %s 0x%08lx 0x%08lx \n",__FILE__,__LINE__, sectPos, sectName, curSec, nextSec);
77 if (flag && !strcmp(sectName,"EEGData")) {
78 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d): 0x%08x %s 0x%08lx 0x%08lx \n",__FILE__,__LINE__, sectPos, sectName, curSec, nextSec);
79 FILE *fid2=fopen("tmp.bin","w");
80 fwrite(hdr->AS.Header + curSec+120, 1, nextSec-curSec-120,fid2);
81 fclose(fid2);
82 FILE *fid=fopen("tmp.asc","w");
83 for (size_t k0=curSec+8*15; k0 < nextSec; k0+=2) {
84 fprintf(fid,"%d\n",bei16p(hdr->AS.Header + curSec + k0+1));
85 }
86 fclose(fid);
87 flag = 0;
88 }
89 sectPos = nextSec;
90 } while (nextSec != (size_t)-1L);
91 }
92 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format EAS(Cadwell): unsupported ");
93 }
94
95 else if (hdr->TYPE==EZ3) {
96 hdr->VERSION = strtod((char*)hdr->AS.Header+21, NULL);
97 // 16 bit ADC ?
98 // 250 Hz ?
99
100 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d)0x%8x\n",__FILE__,__LINE__,hdr->HeadLen);
101
102 uint32_t posH1 = leu32p(hdr->AS.Header + 0x10);
103 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d)0x%8x\n",__FILE__,__LINE__,posH1);
104
105 uint32_t posH1b = leu32p(hdr->AS.Header + 0x20);
106 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d)0x%8x\n",__FILE__,__LINE__,posH1b);
107
108 uint32_t posH2 = leu32p(hdr->AS.Header + 0x38);
109 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d)0x%8x\n",__FILE__,__LINE__,posH2);
110
111 uint32_t posH2b = leu32p(hdr->AS.Header + posH1 + 0x38);
112
113 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d) 0x%08x 0x%08x 0x%08x 0x%08x \n",__FILE__,__LINE__,posH1,posH1b,posH2,posH2b);
114
115 assert(posH1==posH1b);
116 assert(posH2==posH2b);
117
118 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d)\n",__FILE__,__LINE__);
119
120 // start date/time
121 {
122 char *tmp = hdr->AS.Header + 0x5c;
123 struct tm t;
124 t.tm_year = strtol(tmp,&tmp,10)-1900;
125 t.tm_mon = strtol(tmp+1,&tmp,10)-1;
126 t.tm_mday = strtol(tmp+1,&tmp,10);
127 t.tm_hour = strtol(tmp+1,&tmp,10);
128 t.tm_min = strtol(tmp+1,&tmp,10);
129 t.tm_sec = strtol(tmp+1,&tmp,10);
130 hdr->T0 = tm_time2gdf_time(&t);
131 }
132
133 uint32_t pos0 = posH1+0x40;
134 for (size_t k = posH1+0x40; k < posH2; k += 0x30) {
135 char *tmp = hdr->AS.Header + k + 1;
136 uint32_t pos = leu32p(hdr->AS.Header + k + 0x28);
137 if (tmp[0]=='\0') break;
138
139 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s (line %d): Label=<%s> %10d(0x%08x) [sz=%d]\n",__FILE__,__LINE__, tmp, pos, pos, pos-pos0);
140
141 if (pos<pos0) break;
142
143 uint32_t V16=leu32p(hdr->AS.Header+pos+16); // related to size current block, seems to match next pos-pos0-64, V16+pos+64 is next pos.
144 uint32_t V20=leu32p(hdr->AS.Header+pos+20); // always(?) 0
145 uint32_t V24=leu32p(hdr->AS.Header+pos+24); // related to block type, or position, seems to be a decimal number, 0,1,27000,151000,329000
146 uint32_t V32=leu32p(hdr->AS.Header+pos+32); // import for EEG001 blocks ?
147 char* next = hdr->AS.Header+pos+40; // string, refers to name of next block ??
148 uint32_t nextpos = leu32p(hdr->AS.Header + k + 0x28 + 0x30);
149 uint32_t V64=leu32p(hdr->AS.Header+pos+64); // maybe some size information of a subblock ?
150 uint32_t V68=leu32p(hdr->AS.Header+pos+68); // ?
151 uint32_t V72=leu32p(hdr->AS.Header+pos+72); // ?
152 uint32_t V76=leu32p(hdr->AS.Header+pos+76); // ? seems to refer to next block
153 uint32_t V80=leu32p(hdr->AS.Header+pos+80); // ? seems to refer to next block
154 uint32_t V324=leu32p(hdr->AS.Header+pos+324); // seems to be the same as V24, at least when V64 is large enough
155
156 uint32_t V236=leu32p(hdr->AS.Header+pos+236); // seems to be important for EEG001 blocks ?
157 uint32_t V237=leu32p(hdr->AS.Header+pos+237); // seems to be important for EEG001 blocks ?
158 uint32_t V244=leu32p(hdr->AS.Header+pos+244); // seems to be important for EEG001 blocks ?
159 uint32_t V245=leu32p(hdr->AS.Header+pos+245); // seems to be important for EEG001 blocks ?
160 uint32_t V260=leu32p(hdr->AS.Header+pos+260); // seems to be important for EEG001 blocks ?
161 uint32_t V261=leu32p(hdr->AS.Header+pos+261); // seems to be important for EEG001 blocks ?
162 uint32_t V332=leu32p(hdr->AS.Header+pos+332); // seems to be important for EEG001 blocks ?
163
164 char TMP[1024];
165 strcpy(TMP, "cadwell.debug.");
166 strcat(TMP, next);
167 FILE* fid = fopen(TMP,"a"); fprintf(fid,"%d\n",pos); fclose(fid);
168 pos0=pos;
169
170 if (VERBOSE_LEVEL > 7) {
171 if (memcmp(hdr->AS.Header+pos,"EasyDCWYAAA\0@\0\0\0",16) || leu32p(hdr->AS.Header+pos+20) )
172 fprintf(stdout,"%s (line %d): unexpected header info <%s><%s>0x08x\n",__FILE__,__LINE__, hdr->AS.Header+pos, hdr->AS.Header+pos+12, V20);
173
174 if (nextpos != V16+pos+64)
175 fprintf(stdout,"%s (line %d): unexpected header info %d!=%d\n",__FILE__,__LINE__, nextpos, V16+pos+64);
176
177 fprintf(stdout,"%s (line %d): unknown header info (block type, position ?) %8d(0x%08x) <%s><%s>\n",__FILE__,__LINE__, V24, V24, tmp, next);
178
179 if (( V64 > 328) && (V24 != V324))
180 fprintf(stdout,"%s (line %d): *(@+64) %ld(0x%16lx) *(@+324) %d(0x%08x)\n",__FILE__,__LINE__, V64, V64, V324, V324);
181 }
182
183 #if 0
184 if (!strncmp(tmp,"EEG001",7)) {
185 // it seems all EEG001 data blocks do contain these "magic" values
186 if (V64 != 0x02f3504dL)
187 fprintf(stdout,"%s (line %d): [%s] *(@+64) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V64, V64);
188 if (V68 != 0x4da8564a)
189 fprintf(stdout,"%s (line %d): [%s] *(@+68) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V68, V68);
190 if (V72 != 0x03b)
191 fprintf(stdout,"%s (line %d): [%s] *(@+72) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V72, V72);
192 }
193 else if (!strncmp(next,"EVENT001",9)) {
194 // it seems all EEG001 data blocks do contain these "magic" values
195 if (V64 != 0x100)
196 fprintf(stdout,"%s (line %d): [%s] *(@+64) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V64, V64);
197 if (V68 != 0xffffff00)
198 fprintf(stdout,"%s (line %d): [%s] *(@+68) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V68, V68);
199 if (V72 != 0x1ff)
200 fprintf(stdout,"%s (line %d): [%s] *(@+72) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V72, V72);
201 }
202 else {
203 // it seems all EEG001 data blocks do contain these "magic" values
204 if (V64 != 0x100)
205 fprintf(stdout,"%s (line %d): [%s] *(@+64) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V64, V64);
206 if (V68 != 0xffffff00)
207 fprintf(stdout,"%s (line %d): [%s] *(@+68) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V68, V68);
208 if (V72 != 0x01ff)
209 fprintf(stdout,"%s (line %d): [%s] *(@+72) %20d(0x%16x)\n",__FILE__,__LINE__, tmp, V72, V72);
210 }
211 #endif
212
213 if (!strncmp(next, "EEG001",7)) {
214 /* positions with differences in the range of [0..371]
215 16 17 24 25 32 237 238 245 261 262 324 325 332
216 */
217 if (V76 != 0x2addcb81L)
218 fprintf(stdout,"%s (line %d): [%s] *(@+76) %20d(0x%16x)\n",__FILE__,__LINE__, next, V76, V76);
219 if (V80 != 0x02)
220 fprintf(stdout,"%s (line %d): [%s] *(@+80) %20d(0x%16x)\n",__FILE__,__LINE__, next, V80, V80);
221
222 if (V32 != 1000)
223 fprintf(stdout,"%s (line %d): [%s] *(@+32) %20d(0x%16x)\n",__FILE__,__LINE__, next, V32, V32);
224
225 if (V237 != V24)
226 fprintf(stdout,"%s (line %d): [%s] *(@+237) %20d(0x%16x)\n",__FILE__,__LINE__, next, V237, V237);
227
228 if (V245 != 1000)
229 fprintf(stdout,"%s (line %d): [%s] *(@+245) %20d(0x%16x)\n",__FILE__,__LINE__, next, V245, V245);
230
231 if (V261 != V24)
232 fprintf(stdout,"%s (line %d): [%s] *(@+261) %20d(0x%16x)\n",__FILE__,__LINE__, next, V261, V261);
233
234 if (V324 != V24)
235 fprintf(stdout,"%s (line %d): [%s(?)] *(@+324) %20d(0x%16x)\n",__FILE__,__LINE__, next, V324, V324);
236
237 if (V332 != 250) // sampling rate, SPR ?
238 fprintf(stdout,"%s (line %d): [%s/SPR(?)] *(@+332) %20d(0x%16x)\n",__FILE__,__LINE__, next, V332, V332);
239
240 if (V332*4 != V32 || V32 != V245)
241 fprintf(stdout,"%s (line %d): [%s(?)] V32,V245,4xV332 do not match %d != %d != %d\n",__FILE__,__LINE__, next, V32, V245, V332);
242
243 /*
244 start at 372-872: block1
245 block2 615-1114:
246 block2 1433-
247 next interval seem seem to have variable lengths
248 (signed) int16
249 blocklengths 308 samples, 250 real samples + ?
250 */
251 strcpy(TMP, "cadwell.debug.eeg.txt");
252 FILE* fid = fopen(TMP,"a");
253 for (size_t kk=0; kk<V332; kk++) {
254 int16_t v1=lei16p(hdr->AS.Header+pos+371+kk*2);
255 int16_t v2=lei16p(hdr->AS.Header+pos+371+(V332*2+63*2)+kk*2);
256 int16_t v3=lei16p(hdr->AS.Header+pos+371+(V332*2+63*2)*2+kk*2);
257 fprintf(fid, "%d\t%d\t%d\n", v1,v2,v3);
258 }
259 fclose(fid);
260 }
261 else if (!strncmp(next,"EVENT001",9)) {
262 /* positions with differences in the range of [0..2051]
263 18 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 1217 1402
264 after 2052, almost all bytes a diffent
265 */
266 if (V76 != 0)
267 fprintf(stdout,"%s (line %d): EVENT *(@+76) %20d(0x%16x)\n",__FILE__,__LINE__, V76, V76);
268 if (V80 != 0x20c00)
269 fprintf(stdout,"%s (line %d): EVENT *(@+80) %20d(0x%16x)\n",__FILE__,__LINE__, V80, V80);
270 for (int kk=536; kk<556; kk+=4) {
271 uint32_t v = leu32p(hdr->AS.Header+pos+kk);
272 fprintf(stdout,"%s (line %d): [%s] *(@+%10d)\t%d(0x%16x)\n",__FILE__,__LINE__, next, kk, v, v);
273 }
274
275 if (hdr->AS.Header[pos+165]!='E' || hdr->AS.Header[pos+202]!='t' ) {
276 if (VERBOSE_LEVEL > 7)
277 fprintf(stdout,"%s (line %d): unexpected info in EVENT header <%c><%c>\n",__FILE__,__LINE__, hdr->AS.Header[pos+165],hdr->AS.Header[pos+202]);
278 }
279 }
280 else {
281 if (V76 != 0)
282 fprintf(stdout,"%s (line %d): [%s]->[%s] *(@+76) %20d(0x%16x)\n",__FILE__,__LINE__, tmp,next, V76, V76);
283 if (V80 != 0x20c00)
284 fprintf(stdout,"%s (line %d): [%s]->[%s] *(@+80) %20d(0x%16x)\n",__FILE__,__LINE__, tmp,next, V80, V80);
285 }
286 }
287 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format EZ3(Cadwell): unsupported ");
288
289 }
290 else if (hdr->TYPE==ARC) {
291 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format ARC(Cadwell): unsupported ");
292 }
293 }
294