Debian Codegraph @

biosig4c++/t210/sopen_rhd2000_read.c

raw symbols SCIP
1 /*
2 Copyright (C) 2019-2021,2024-2026 Alois Schloegl <[email protected]>
3
4 This file is part of the "BioSig for C/C++" repository
5 (biosig4c++) at http://biosig.sf.net/
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 3
10 of the License, or (at your option) any later version.
11 */
12
13 /*
14 References:
15 [1] RHD2000 Application Note: Data File Formats, Intan TECHNOLOGIES, LLC
16 Downloaded 2020-01-14 from
17 http://www.intantech.com/files/Intan_RHD2000_data_file_formats.pdf
18 [2] RHS2000 Data File Formats - Intan Tech
19 http://www.intantech.com/files/Intan_RHS2000_data_file_formats.pdf
20 */
21
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <iconv.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28
29 #if !defined(__APPLE__) && defined (_LIBICONV_H)
30 #define iconv libiconv
31 #define iconv_open libiconv_open
32 #define iconv_close libiconv_close
33 #endif
34
35 #include "../gdftime.h"
36 #include "../biosig-dev.h"
37
38 #define min(a,b) (((a) < (b)) ? (a) : (b))
39 #define max(a,b) (((a) > (b)) ? (a) : (b))
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /*
46 get_qstring has the following functions and side effects:
47 *) check whether sufficient data of the header information has been read,
48 reads more data if needed,
49 *) advance position pointer pos to the end of the string;
50 *) convert QString into UTF-8 String in outbuf
51 */
52
53 void read_qstring(HDRTYPE* hdr, size_t *pos, char *outbuf, size_t outlen) {
54 uint32_t len = leu32p(hdr->AS.Header + (*pos));
55 *pos += 4;
56 if (len==(uint32_t)(-1)) return;
57
58 // after each qstring at most 28 bytes are loaded before the next check for a qstring
59 // This check is also needed when qstring is empty
60 size_t SIZE = *pos+len+100;
61 if (SIZE > hdr->HeadLen) {
62 SIZE = max(SIZE, 2*hdr->HeadLen); // always double size of header
63 void *ptr = realloc(hdr->AS.Header, SIZE);
64 if (ptr==NULL) {
65 biosigERROR(hdr, B4C_MEMORY_ALLOCATION_FAILED, "Format Intan RH[DS]2000 - memory allocation failed");
66 return;
67 }
68 hdr->AS.Header = (uint8_t*)ptr;
69 hdr->HeadLen += ifread(hdr->AS.Header+hdr->HeadLen, 1, SIZE-hdr->HeadLen, hdr);
70 }
71
72 if ((*pos + len ) > hdr->HeadLen)
73 biosigERROR(hdr, B4C_INCOMPLETE_FILE, "Format Intan RHD2000 - incomplete file");
74
75 // convert qString into UTF-8 string
76 if (outbuf != NULL) {
77 iconv_t CD = iconv_open ("UTF-8", "UTF-16LE");
78 size_t inlen = len;
79 char *inbuf = hdr->AS.Header+(*pos);
80 size_t iconv_res = iconv (CD, &inbuf, &inlen, &outbuf, &outlen);
81 *outbuf = '\0';
82 iconv_close (CD);
83 }
84 *pos += len;
85 return;
86 }
87
88
89 int sopen_intan_clp_read(HDRTYPE* hdr) {
90
91 uint16_t NumADCs=0, NumChips=0, NumChan=0;
92
93 float minor = leu16p(hdr->AS.Header+6);
94 minor *= (minor < 10) ? 0.1 : 0.01;
95 hdr->VERSION = leu16p(hdr->AS.Header+4) + minor;
96
97 uint16_t datatype=leu16p(hdr->AS.Header+8);
98 switch (datatype) {
99 case 1: NumADCs=leu16p(hdr->AS.Header+10);
100 hdr->SampleRate = lef32p(hdr->AS.Header+24);
101 case 0: break;
102 default:
103 // this should never ever occurs, because getfiletype checks this
104 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format Intan CLP - datatype unknown");
105 return -1;
106 }
107
108 size_t HeadLen=leu16p(hdr->AS.Header+10+(datatype*2));
109 // read header
110 if (HeadLen > hdr->HeadLen) {
111 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, HeadLen+1);
112 hdr->HeadLen += ifread(hdr->AS.Header + hdr->HeadLen, 1, HeadLen - hdr->HeadLen, hdr);
113 }
114 hdr->AS.Header[hdr->HeadLen]=0;
115 if (HeadLen > hdr->HeadLen) {
116 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format Intan/CLP - file is too short");
117 return -1;
118 }
119 ifseek(hdr, HeadLen, SEEK_SET);
120
121 // read recording date and time
122 size_t pos=12+(datatype*2);
123 {
124 struct tm t0;
125 t0.tm_year = leu16p(hdr->AS.Header+pos);
126 t0.tm_mon = leu16p(hdr->AS.Header+pos+2);
127 t0.tm_mday = leu16p(hdr->AS.Header+pos+4);
128 t0.tm_hour = leu16p(hdr->AS.Header+pos+6);
129 t0.tm_min = leu16p(hdr->AS.Header+pos+8);
130 t0.tm_sec = leu16p(hdr->AS.Header+pos+10);
131 hdr->T0 = tm_time2gdf_time(&t0);
132 }
133
134 switch (datatype) {
135 case 0:
136 // If this is the standard data file (including clamp and measured data),
137 /* TODO:
138 read chips
139 read settings
140 */
141 break;
142 case 1:
143 // If this is the aux data file (including Digital Ins/Outs and ADC data)
144 default:
145 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format Intan CLP - datatype unknown");
146 return -1;
147 }
148
149 switch (datatype) {
150 case 0: {
151 // read chips
152 pos = 24;
153 NumChips=leu16p(hdr->AS.Header+pos);
154 NumChan=leu16p(hdr->AS.Header+pos+2);
155 for (uint16_t k1 = 0; k1 < NumChips; k1++) {
156 for (uint16_t k2 = 0; k2 < NumChan; k2++) {
157 // read one header per chip
158 //14*2+4+4+16+20+4
159 }
160 // read
161 }
162 pos += ((14*2+4+4+16+20+4) * NumChan + 8) * NumChips;
163 // read settings
164
165 hdr->NS = 4;
166 hdr->SPR = 1;
167 hdr->NRec = -1;
168 hdr->AS.bpb= 16;
169
170 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
171 // see below // strcpy(hdr->CHANNEL[0].Label,"Time");
172 strcpy(hdr->CHANNEL[1].Label,"Clamp");
173 strcpy(hdr->CHANNEL[2].Label,"TotalClamp");
174 strcpy(hdr->CHANNEL[3].Label,"Measured");
175
176 for (int k=0; k < hdr->NS; k++) {
177 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
178 hc->Transducer[0]='\0';
179 hc->OnOff=1;
180 hc->GDFTYP=16;
181 hc->DigMax=+1e9;
182 hc->DigMin=-1e9;
183 hc->Cal=1;
184 hc->Off=0;
185 }
186 break;
187 }
188 case 1: {
189 hdr->NS = NumADCs;
190 hdr->SPR = 1;
191 hdr->NRec = -1;
192 hdr->AS.bpb= 4 + 2 + 2 + 2 * NumADCs;
193
194 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
195 for (int k=0; k < hdr->NS; k++) {
196 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
197 sprintf(hdr->CHANNEL[3].Label, "ADC%d", k-2);
198 hc->Transducer[0]='\0';
199 hc->OnOff=1;
200 hc->GDFTYP=4;
201 hc->DigMax=65535.0;
202 hc->DigMin=0;
203 hc->Cal = (k < 3) ? 1.0 : 0.0003125;
204 hc->Off = (k < 3) ? 0.0 : -10.24;
205 }
206 // see below // strcpy(hdr->CHANNEL[0].Label, "Time");
207 strcpy(hdr->CHANNEL[1].Label,"DigitalIn");
208 strcpy(hdr->CHANNEL[2].Label,"DigitalOut");
209 break;
210 }
211 default:
212 ;
213 }
214
215 hdr->CHANNEL[0].OnOff=2; //uint32
216 hdr->CHANNEL[0].GDFTYP=6; //uint32
217 hdr->CHANNEL[0].DigMax=ldexp(1l,32)-1; //uint32
218 hdr->CHANNEL[0].DigMin=0.0; //uint32
219 hdr->CHANNEL[0].Cal=1.0/hdr->SampleRate; //uint32
220 hdr->CHANNEL[0].PhysDimCode = 2176; //uint32
221 strcpy(hdr->CHANNEL[0].Label, "Time");
222
223 hdr->AS.bpb = 0;
224 for (int k = 0; k < hdr->NS; k++) {
225 CHANNEL_TYPE *hc = hdr->CHANNEL+k;
226 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
227 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
228 hc->LeadIdCode = 0;
229 hc->PhysDimCode = 0;
230 hc->TOffset = 0;
231 hc->LowPass = NAN;
232 hc->HighPass = NAN;
233 hc->Notch = NAN;
234 hc->Impedance = NAN;
235 hc->fZ = NAN;
236 hc->SPR = 1;
237 memset(hc->XYZ, 0, 12);
238 hc->bi = hdr->AS.bpb;
239 hdr->AS.bpb += (GDFTYP_BITS[hc->GDFTYP]*(size_t)hc->SPR) >> 3;
240 }
241
242 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format Intan/CLP not supported");
243 return -1;
244 }
245
246 /*
247 RHS2000 Data File Formats - Intan Tech
248 http://www.intantech.com/files/Intan_RHS2000_data_file_formats.pdf
249 */
250 int sopen_rhs2000_read(HDRTYPE* hdr) {
251
252 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %u); %s(...) [%u]\n",__FILE__,__LINE__,__func__,hdr->HeadLen);
253
254 // 8 bytes
255 int16_t major = leu16p(hdr->AS.Header+4);
256 int16_t minor = leu16p(hdr->AS.Header+6);
257 hdr->VERSION = major + minor * (minor < 10 ? 0.1 : 0.01);
258
259 // +38 = 46 bytes
260 hdr->NS = 1;
261 hdr->SampleRate = lef32p(hdr->AS.Header+8);
262
263 float HighPass = ( leu16p(hdr->AS.Header+12) ? lef32p(hdr->AS.Header+14) : 0.0 );
264 HighPass = max( HighPass, lef32p(hdr->AS.Header+18) );
265 float LowPass = lef32p(hdr->AS.Header+26);
266
267 // +10 = 56 bytes
268 const int ListNotch[] = {0,50,60};
269 uint16_t tmp = leu16p(hdr->AS.Header+46);
270 if (tmp>2) tmp=0;
271 float Notch = ListNotch[tmp];
272 float fZ_desired = lef32p(hdr->AS.Header+46); // desired impedance test frequency
273 float fZ_actual = lef32p(hdr->AS.Header+50); // actual impedance test frequency
274
275 // +4 = 60 bytes
276 // +12 = 72 bytes
277 float StimStepSize = lef32p(hdr->AS.Header+58); // Stim Step Size [A]
278 float ChargeRecoveryCurrentLimit = lef32p(hdr->AS.Header+62); // [A]
279 float ChargeRecoveryTargetVoltage = lef32p(hdr->AS.Header+66); // [V]
280
281 size_t pos = 72;
282 read_qstring(hdr, &pos, NULL, 0); // note1
283 read_qstring(hdr, &pos, NULL, 0); // note2
284 read_qstring(hdr, &pos, NULL, 0); // note3
285
286 uint16_t flag_DC_amplifier_data_saved = leu16p(hdr->AS.Header+pos) > 0;
287 pos += 2;
288 uint16_t BoardMode = leu16p(hdr->AS.Header+pos);
289 pos += 2;
290
291 char *ReferenceChannelName = NULL;
292 read_qstring(hdr, &pos, ReferenceChannelName, 0);
293
294 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %u); %s(...) %u %u\n", __FILE__, __LINE__, __func__, (unsigned)pos, hdr->HeadLen);
295
296 uint16_t numberOfSignalGroups = leu16p(hdr->AS.Header+pos);
297 pos += 2;
298 uint16_t NS = 0;
299
300 hdr->SPR = (major == 1 ? 60 : 128);
301 size_t bi = (0+4)*hdr->SPR;
302
303 // allocate memory for time channel
304 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, sizeof(CHANNEL_TYPE));
305
306 // read all signal groups
307 for (int nsg=0; nsg < numberOfSignalGroups; nsg++) {
308 char SignalGroupName[101], SignalGroupPrefix[101];
309 read_qstring(hdr, &pos, SignalGroupName, 100);
310 read_qstring(hdr, &pos, SignalGroupPrefix, 100);
311
312 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %u); %s(...) group=%u %u SGP<%s> SGP<%s>\n",__FILE__,__LINE__,__func__, nsg, (unsigned)pos, SignalGroupName,SignalGroupPrefix );
313
314 uint16_t flag_SignalGroupEnabled = leu16p(hdr->AS.Header+pos);
315 pos += 2;
316 uint16_t NumberOfChannelsInSignalGroup = leu16p(hdr->AS.Header+pos);
317 pos += 2;
318 uint16_t NumberOfAmplifierChannelsInSignalGroup = leu16p(hdr->AS.Header+pos);
319 pos += 2;
320
321 if (VERBOSE_LEVEL>7) fprintf(stdout,"%s (line %u); %s(...) group=%u %u+%u %d\n", __FILE__, __LINE__, __func__, nsg, NS, NumberOfChannelsInSignalGroup, (int)pos);
322
323 if (flag_SignalGroupEnabled) {
324 typeof(pos) tmppos = pos;
325 int NumChans0 = 0;
326 int NumChans1 = 0;
327 // get number of enabled channels
328 for (unsigned k = 0; k < NumberOfChannelsInSignalGroup; k++) {
329 read_qstring(hdr, &tmppos, NULL, 0);
330 read_qstring(hdr, &tmppos, NULL, 0);
331 tmppos += 4;
332 int16_t SignalType = lei16p(hdr->AS.Header+tmppos);
333 tmppos += 2;
334 int16_t ChannelEnabled = lei16p(hdr->AS.Header+tmppos);
335 tmppos += 24;
336 NumChans0 += (ChannelEnabled > 0);
337 NumChans1 += (ChannelEnabled > 0) * (1 + (SignalType==0) * (flag_DC_amplifier_data_saved+1));
338 }
339
340 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL, (1+NS+NumChans1) * sizeof(CHANNEL_TYPE));
341 for (unsigned k = 0; k < NumberOfChannelsInSignalGroup; k++) {
342 char NativeChannelName[MAX_LENGTH_LABEL+1];
343 char CustomChannelName[MAX_LENGTH_LABEL+1];
344 read_qstring(hdr, &pos, NativeChannelName, MAX_LENGTH_LABEL);
345 read_qstring(hdr, &pos, CustomChannelName, MAX_LENGTH_LABEL);
346 pos += 4;
347 int16_t SignalType = lei16p(hdr->AS.Header+pos);
348 pos += 2;
349 int16_t ChannelEnabled = leu16p(hdr->AS.Header+pos)>0;
350 pos += 2;
351 int16_t ChipChannel = lei16p(hdr->AS.Header+pos);
352 pos += 2;
353 int16_t CommandStream = lei16p(hdr->AS.Header+pos);
354 pos += 2;
355 int16_t BoardStream = lei16p(hdr->AS.Header+pos);
356 pos += 2;
357 int16_t SpikeScopeVoltageTriggerMode = lei16p(hdr->AS.Header+pos);
358 pos += 2;
359 int16_t SpikeScopeVoltageThreshold = lei16p(hdr->AS.Header+pos);
360 pos += 2;
361 int16_t SpikeScopeDigitalTriggerChannel = lei16p(hdr->AS.Header+pos);
362 pos += 2;
363 int16_t SpikeScopeDigitalTriggerEdgePolarity = lei16p(hdr->AS.Header+pos);
364 pos += 2;
365 float ElectrodeImpedanceMagnitude = lef32p(hdr->AS.Header+pos);
366 pos += 4;
367 float ElectrodeImpedancePhase = lef32p(hdr->AS.Header+pos);
368 pos += 4;
369
370 if (ChannelEnabled) {
371 int nn = 1 + (SignalType==0) * (1 + flag_DC_amplifier_data_saved);
372 char *ChannelName = NULL;
373 NS++; // first channel is Time channel
374 for (int k2=0; k2 < nn; k2++) {
375 // k2>0 takes care of StimDatea and DCamp channels
376 CHANNEL_TYPE *hc = hdr->CHANNEL + NS + NumChans0*k2;
377 strcpy(hc->Label, NativeChannelName);
378 strncat(hc->Label, ":", MAX_LENGTH_LABEL-strlen(hc->Label));
379 strncat(hc->Label, CustomChannelName, MAX_LENGTH_LABEL-strlen(hc->Label));
380 #ifdef MAX_LENGTH_PHYSDIM
381 strcpy(hc->PhysDim,"?");
382 #endif
383 hc->OnOff = 1;
384 hc->GDFTYP = 4; // uint16
385 hc->bi = bi + hdr->SPR * NumChans0 * k2 * 2;
386 hc->bi8 = bi << 3;
387 hc->LeadIdCode = 0;
388 hc->DigMin = 0.0;
389 hc->DigMax = ldexp(1,16)-1;
390
391 hc->PhysDimCode = 0; // [?] default
392 hc->Cal = 1; // default
393 hc->Off = 0; // default
394 hc->SPR = hdr->SPR; // default
395
396 switch (SignalType) {
397 case 0: // RHS2000 amplifier channel
398 if (k2==0) {
399 ChannelName = hc->Label;
400 hc->Cal = 0.195;
401 hc->Off = -32768 * hc->Cal;
402 hc->PhysDimCode = 4275; // [uV]
403 }
404 else if ((k2+1)==nn) {
405 // Stimulation Data
406 sprintf(hc->Label,"Stim %s",ChannelName);
407 hc->PhysDimCode = 4160; // [A]
408 hc->Cal = StimStepSize;
409 hc->Off = 0;
410 /* NOTE: reading StimData disabled - this is not supported yet.
411 Issues are decoding and scaling of data (bit9-16)
412 */
413 hc->OnOff = 0;
414 }
415 else {
416 sprintf(hc->Label,"DC_AmpData %s",ChannelName);
417 hc->Cal = 19.23;
418 hc->Off = -512 * hc->Cal;
419 hc->PhysDimCode = 4274; // [mV]
420 }
421 break;
422 /*
423 case 2: // supply voltage channel
424 hc->SPR = 1;
425 hc->Cal = 0.0000748;
426 hc->Off = 0;
427 hc->PhysDimCode = 4256; // [V]
428 break
429 */
430 case 3: // Analog input channel
431 case 4: // Analog output channel
432 hc->PhysDimCode = 4256; // [V]
433 hc->Cal = 0.0003125;
434 hc->Off = -32768 * hc->Cal;
435 break;
436 case 5: // Digital Input channel
437 case 6: // Digital Output channel
438 hc->PhysDimCode = 0; // [1]
439 break;
440 default:
441 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format Intan RHS2000 - not conformant to specification");
442 }
443 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
444 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
445
446 hc->bufptr = NULL;
447 hc->TOffset = 0;
448 hc->LowPass = LowPass;
449 hc->HighPass = HighPass;
450 hc->Notch = Notch;
451 hc->XYZ[0] = NAN;
452 hc->XYZ[1] = NAN;
453 hc->XYZ[2] = NAN;
454 if ( (hc->PhysDimCode & 0xffe0) == 4256) // [V]
455 hc->Impedance = ElectrodeImpedanceMagnitude;
456 }
457 bi += hdr->SPR * 2;
458 } // if (ChannelEnabled)
459 }
460 // take care of StimData Channels and AmpDC channels
461 bi += (NumChans1 - NumChans0) * hdr->SPR * 2;
462 NS += NumChans1 - NumChans0;
463 }
464 }
465 { // channel 0 - time channel
466 CHANNEL_TYPE *hc = hdr->CHANNEL+0;
467 hc->OnOff = 2;
468 strcpy(hc->Label, "time");
469 strcpy(hc->Transducer, "");
470 #ifdef MAX_LENGTH_PHYSDIM
471 strcpy(hc->PhysDim,"s");
472 #endif
473 hc->bi = 0;
474 hc->bufptr = NULL;
475
476 hc->OnOff = 2; // time channel
477 hc->SPR = hdr->SPR;
478 hc->GDFTYP = 6; // uint32
479 hc->bi = 0;
480 hc->bi8 = hc->bi << 3;
481 hc->LeadIdCode = 0;
482 hc->DigMin = 0.0;
483 hc->DigMax = ldexp(1,32)-1;
484 hc->Off = 0.0;
485 hc->Cal = 1.0/hdr->SampleRate;
486 hc->PhysDimCode = 2176; // [s]
487 hc->PhysMin = 0;
488 hc->PhysMax = hc->DigMax*hc->Cal;
489
490 hc->bufptr = NULL;
491 hc->TOffset = 0;
492 hc->LowPass = 0;
493 hc->HighPass = INFINITY;
494 hc->Notch = 0;
495 hc->XYZ[0] = NAN;
496 hc->XYZ[1] = NAN;
497 hc->XYZ[2] = NAN;
498 hc->Impedance = NAN;
499 }
500
501 hdr->HeadLen = pos;
502 hdr->NRec = -1;
503 hdr->NS = NS+1;
504 hdr->AS.bpb = bi;
505 ifseek(hdr, hdr->HeadLen, SEEK_SET);
506
507 struct stat FileBuf;
508 if (stat(hdr->FileName,&FileBuf)==0) hdr->FILE.size = FileBuf.st_size;
509 hdr->NRec = (hdr->FILE.size - hdr->HeadLen) / hdr->AS.bpb;
510
511 return 0;
512 }
513
514 /*
515 RHD2000 Data File Formats - Intan Tech
516 http://www.intantech.com/files/Intan_RHD2000_data_file_formats.pdf
517 */
518 int sopen_rhd2000_read(HDRTYPE* hdr) {
519
520 uint16_t major = leu16p(hdr->AS.Header+4);
521 float minor = leu16p(hdr->AS.Header+6);
522 minor *= (minor < 10) ? 0.1 : 0.01;
523 hdr->VERSION = major + minor;
524
525 hdr->NS = 1;
526 hdr->SampleRate = lef32p(hdr->AS.Header+8);
527
528 float HighPass = ( leu16p(hdr->AS.Header+12) ? lef32p(hdr->AS.Header+14) : 0.0 );
529 HighPass = max( HighPass, lef32p(hdr->AS.Header+18) );
530 float LowPass = lef32p(hdr->AS.Header+22);
531
532 const int ListNotch[] = {0,50,60};
533 uint16_t tmp = leu16p(hdr->AS.Header+38);
534 if (tmp>2) tmp=0;
535 float Notch = ListNotch[tmp];
536 float fZ_desired = lef32p(hdr->AS.Header+40); // desired impedance test frequency
537 float fZ_actual = lef32p(hdr->AS.Header+44); // actual impedance test frequency
538
539 size_t pos = 48;
540 char tmpstr[101];
541 read_qstring(hdr, &pos, tmpstr, 100); // note1
542 read_qstring(hdr, &pos, tmpstr, 100); // note2
543 read_qstring(hdr, &pos, tmpstr, 100); // note3
544
545 uint16_t numberTemperatureSensors = 0;
546 if (hdr->Version >= 1.1) {
547 numberTemperatureSensors = leu16p(hdr->AS.Header+pos);
548 pos += 2;
549 }
550
551 int BoardMode = 0;
552 if (hdr->Version >= 1.3) {
553 BoardMode = leu16p(hdr->AS.Header+pos);
554 pos += 2;
555 }
556 char referenceChannelName[101]; referenceChannelName[0]=0;
557 if (hdr->Version >= 2.0) {
558 read_qstring(hdr, &pos, referenceChannelName, 100);
559 }
560 uint16_t numberOfSignalGroups = leu16p(hdr->AS.Header+pos);
561 pos += 2;
562
563 hdr->NS = 1+numberTemperatureSensors; // time channel + temparature channels
564 hdr->CHANNEL = (CHANNEL_TYPE*)realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
565 CHANNEL_TYPE *hc = hdr->CHANNEL;
566 strcpy(hc->Label,"Time");
567 hc->Transducer[0] = 0;
568 hc->OnOff = 2;
569 hc->bi = 0;
570 hc->GDFTYP = 5; //int32_t
571 hc->DigMin = -ldexp(1,31);
572 hc->DigMax = ldexp(1,31) - 1;
573 hdr->SPR = (major==1) ? 60 : 128;
574 hc->SPR = hdr->SPR;
575 size_t bi = (0+4)*hdr->SPR;
576
577 for (uint16_t k = 0; k<numberOfSignalGroups; k++) {
578
579 char groupName[MAX_LENGTH_LABEL+1];
580 char groupNamePrefix[MAX_LENGTH_LABEL+1];
581
582 read_qstring(hdr, &pos, groupName, MAX_LENGTH_LABEL);
583 read_qstring(hdr, &pos, groupNamePrefix, MAX_LENGTH_LABEL);
584
585 uint16_t enabledGroup = leu16p(hdr->AS.Header+pos);
586 pos += 2;
587 uint16_t numberChannelsInGroup = leu16p(hdr->AS.Header+pos);
588 pos += 2;
589 uint16_t numberAmplifierChannels = leu16p(hdr->AS.Header+pos);
590 pos += 2;
591
592 if (enabledGroup && (numberChannelsInGroup > 0) ) {
593
594 hdr->CHANNEL = realloc(hdr->CHANNEL, ((size_t)hdr->NS + (size_t)numberChannelsInGroup) * sizeof(CHANNEL_TYPE));
595
596 int ns = 0;
597 uint32_t chan;
598 for (chan=0; chan < numberChannelsInGroup; chan++) {
599 char NativeChannelName[MAX_LENGTH_LABEL+1];
600 char CustomChannelName[MAX_LENGTH_LABEL+1];
601
602 read_qstring(hdr, &pos, NativeChannelName, MAX_LENGTH_LABEL);
603 read_qstring(hdr, &pos, CustomChannelName, MAX_LENGTH_LABEL);
604
605 uint16_t nativeOrder = leu16p(hdr->AS.Header+pos);
606 pos += 2;
607 uint16_t customOrder = leu16p(hdr->AS.Header+pos);
608 pos += 2;
609 uint16_t signalType = leu16p(hdr->AS.Header+pos);
610 pos += 2;
611
612 uint16_t channelEnabled = leu16p(hdr->AS.Header+pos);
613 pos += 2;
614 uint16_t chipChannel = leu16p(hdr->AS.Header+pos);
615 pos += 2;
616 uint16_t boardStream = leu16p(hdr->AS.Header+pos);
617 pos += 2;
618
619 // Spike Scope
620 uint16_t triggerMode = leu16p(hdr->AS.Header+pos);
621 pos += 2;
622 int16_t voltageThreshold = leu16p(hdr->AS.Header+pos); // uV
623 pos += 2;
624 uint16_t triggerChannel = leu16p(hdr->AS.Header+pos);
625 pos += 2;
626 uint16_t edgePolarity = leu16p(hdr->AS.Header+pos);
627 pos += 2;
628 float ImpedanceMagnitude = lef32p(hdr->AS.Header+pos); // Ohm
629 pos += 4;
630 float ImpedancePhase = lef32p(hdr->AS.Header+pos); // degree
631 pos += 4;
632
633 if (!(chipChannel<32) || !(signalType<6)) {
634 biosigERROR(hdr, B4C_FORMAT_UNSUPPORTED, "Format Intan RHD2000 - not conformant to specification");
635 return -1;
636 }
637
638 if (channelEnabled) {
639 CHANNEL_TYPE *hc = hdr->CHANNEL + hdr->NS + ns;
640 ns += channelEnabled > 0;
641
642 // translate into biosig HDR channel structure
643 hc->GDFTYP = 4; // uint16_t
644 hc->PhysDimCode = 0; // [?] default
645 hc->Cal = 1; // default
646 hc->Off = 0; // default
647 hc->SPR = hdr->SPR; // default
648 strcpy(hc->Label, NativeChannelName);
649 strncat(hc->Label, ":", MAX_LENGTH_LABEL - strlen(hc->Label));
650 strncat(hc->Label, CustomChannelName, MAX_LENGTH_LABEL - strlen(hc->Label));
651 hc->Transducer[0] = 0;
652 #ifdef MAX_LENGTH_PHYSDIM
653 strcpy(hc->PhysDim, "?");
654 #endif
655 hc->OnOff = 1;
656 hc->bi = bi;
657 hc->bi8 = bi << 3;
658 hc->LeadIdCode = 0;
659 hc->DigMin = 0.0;
660 hc->DigMax = ldexp(1,16)-1;
661
662 switch (signalType) {
663 case 0: // amplifier channel
664 hc->Cal = 0.195;
665 hc->Off = -32768*hc->Cal;
666 hc->PhysDimCode = 4275; // [uV]
667 break;
668 case 1: // auxilary input channel
669 hc->SPR = hc->SPR/4;
670 hc->Cal = 0.0000374;
671 hc->Off = -32768*hc->Cal;
672 hc->PhysDimCode = 4256; // [V]
673 break;
674 case 2: // supply voltage channel
675 hc->SPR = channelEnabled;
676 hc->Cal = 0.0000748;
677 hc->Off = 0;
678 hc->PhysDimCode = 4256; // [V]
679 break;
680 case 0xffff: // Temperature Sensor channel
681 hc->GDFTYP = 3; // int16
682 hc->SPR = 1;
683 hc->Cal = 0.01;
684 hc->Off = 0;
685 hc->PhysDimCode = 6048; // [°C]
686 break;
687 case 3: // USB board ADC input channel
688 hc->PhysDimCode = 4256; // [V]
689 switch(BoardMode) {
690 case 0:
691 hc->Cal = 0.000050354;
692 hc->Off = 0;
693 break;
694 case 1:
695 hc->Cal = 0.00015259;
696 hc->Off = -32768*hc->Cal;
697 break;
698 case 13:
699 hc->Cal = 0.0003125;
700 hc->Off = -32768*hc->Cal;
701 break;
702 }
703 break;
704 case 4: // USB board digital input channel
705 ;
706 case 5: // USB board digital output channel
707 ;
708 }
709 bi += hc->SPR*2;
710 hc->PhysMin = hc->DigMin * hc->Cal + hc->Off;
711 hc->PhysMax = hc->DigMax * hc->Cal + hc->Off;
712
713 if ( (hc->PhysDimCode & 0xffe0) == 4256) // [V]
714 hc->Impedance = ImpedanceMagnitude;
715 } // if (channelEnabled)
716 }
717 hdr->NS += ns;
718 }
719 }
720 hdr->CHANNEL = realloc(hdr->CHANNEL, hdr->NS * sizeof(CHANNEL_TYPE));
721
722 hdr->HeadLen = pos;
723 hdr->NRec = -1;
724 hdr->AS.bpb = bi;
725 ifseek(hdr, hdr->HeadLen, SEEK_SET);
726
727 struct stat FileBuf;
728 if (stat(hdr->FileName,&FileBuf)==0) hdr->FILE.size = FileBuf.st_size;
729 hdr->NRec = (hdr->FILE.size - hdr->HeadLen) / hdr->AS.bpb;
730
731 return 0;
732 }
733
734 #ifdef __cplusplus
735 }
736 #endif