stfio::filetype stfio_file_type(HDRTYPE* hdr) { #ifdef __LIBBIOSIG2_H__ switch (biosig_get_filetype(hdr)) { #else switch (hdr->TYPE) { #endif #if (BIOSIG_VERSION > 10500) case ABF2: return stfio::abf; #endif case ABF: return stfio::abf; case ATF: return stfio::atf; case CFS: return stfio::cfs; case HEKA: return stfio::heka; case HDF: return stfio::hdf5; #if (BIOSIG_VERSION > 10403) case AXG: return stfio::axg; case IBW: return stfio::igor; case SMR: return stfio::son; #endif default: return stfio::none; } } #if (defined(WITH_BIOSIG) || defined(WITH_BIOSIG2)) bool stfio::check_biosig_version(int a, int b, int c) { return (BIOSIG_VERSION >= 10000*a + 100*b + c); } #endif stfio::filetype stfio::importBiosigFile(const std::string &fName, Recording &ReturnData, ProgressInfo& progDlg) { std::string errorMsg("Exception while calling std::importBSFile():\n"); std::string yunits; stfio::filetype type; // ===================================================================================================================== // // importBiosig opens file with libbiosig // - performs an automated identification of the file format // - and decides whether the data is imported through importBiosig (currently CFS, HEKA, ABF1, GDF, and others) // - or handed back to other import*File functions (currently ABF2, AXG, HDF5) // // There are two implementations, level-1 and level-2 interface of libbiosig. // level 1 is used when -DWITH_BIOSIG, -lbiosig // level 2 is used when -DWITH_BIOSIG2, -lbiosig2 // // level 1 is better tested, but it does not provide ABI compatibility between MinGW and VisualStudio // level 2 interface has been developed to provide ABI compatibility, but it is less tested // and the API might still undergo major changes. // ===================================================================================================================== #ifdef __LIBBIOSIG2_H__ HDRTYPE* hdr = sopen( fName.c_str(), "r", NULL ); if (hdr==NULL) { ReturnData.resize(0); return stfio::none; } type = stfio_file_type(hdr); if (biosig_check_error(hdr)) { ReturnData.resize(0); destructHDR(hdr); return type; } enum FileFormat biosig_filetype=biosig_get_filetype(hdr); if (biosig_filetype==ATF || biosig_filetype==ABF2 || biosig_filetype==HDF ) { // ATF, ABF2 and HDF5 support should be handled by importATF, and importABF, and importHDF5 not importBiosig ReturnData.resize(0); destructHDR(hdr); return type; } // earlier versions of biosig support only the file type identification, but did not properly read the files if ( (BIOSIG_VERSION < 10603) && (biosig_filetype==AXG) ) { ReturnData.resize(0); destructHDR(hdr); return type; } // ensure the event table is in chronological order sort_eventtable(hdr); // allocate local memory for intermediate results; const int strSize=100; char str[strSize]; /* count sections and generate list of indices indicating start and end of sweeps */ double fs = biosig_get_eventtable_samplerate(hdr); size_t numberOfEvents = biosig_get_number_of_events(hdr); size_t nsections = biosig_get_number_of_segments(hdr); size_t *SegIndexList = (size_t*)malloc((nsections+1)*sizeof(size_t)); SegIndexList[0] = 0; SegIndexList[nsections] = biosig_get_number_of_samples(hdr); std::string annotationTableDesc = std::string(); for (size_t k=0, n=0; k < numberOfEvents; k++) { uint32_t pos; uint16_t typ; #if BIOSIG_VERSION < 10605 char *desc; #else const char *desc; #endif /* uint32_t dur; uint16_t chn; gdftype timestamp; */ biosig_get_nth_event(hdr, k, &typ, &pos, NULL, NULL, NULL, &desc); if (typ == 0x7ffe) { SegIndexList[++n] = pos; } else if (typ < 256) { sprintf(str,"%f s:\t%s\n", pos/fs, desc); annotationTableDesc += std::string( str ); } } int numberOfChannels = biosig_get_number_of_channels(hdr); /************************************************************************* rescale data to mV and pA *************************************************************************/ for (int ch=0; ch < numberOfChannels; ++ch) { CHANNEL_TYPE *hc = biosig_get_channel(hdr, ch); switch (biosig_channel_get_physdimcode(hc) & 0xffe0) { case 4256: // Volt //biosig_channel_scale_to_unit(hc, "mV"); biosig_channel_change_scale_to_physdimcode(hc, 4274); break; case 4160: // Ampere //biosig_channel_scale_to_unit(hc, "pA"); biosig_channel_change_scale_to_physdimcode(hc, 4181); break; } } /************************************************************************* read bulk data *************************************************************************/ biosig_data_type *data = biosig_get_data(hdr, 0); size_t SPR = biosig_get_number_of_samples(hdr); #ifdef _STFDEBUG std::cout << "Number of events: " << numberOfEvents << std::endl; /*int res = */ hdr2ascii(hdr, stdout, 4); #endif for (int NS=0; NS < numberOfChannels; ) { CHANNEL_TYPE *hc = biosig_get_channel(hdr, NS); Channel TempChannel(nsections); TempChannel.SetChannelName(biosig_channel_get_label(hc)); TempChannel.SetYUnits(biosig_channel_get_physdim(hc)); for (size_t ns=1; ns<=nsections; ns++) { size_t SPS = SegIndexList[ns]-SegIndexList[ns-1]; // length of segment, samples per segment int progbar = int(100.0*(1.0*ns/nsections + NS)/numberOfChannels); std::ostringstream progStr; progStr << "Reading channel #" << NS + 1 << " of " << numberOfChannels << ", Section #" << ns << " of " << nsections; progDlg.Update(progbar, progStr.str()); /* unused // char sweepname[20]; sprintf(sweepname,"sweep %i",(int)ns); */ Section TempSection( SPS, // TODO: hdr->nsamplingpoints[nc][ns] "" // TODO: hdr->sectionname[nc][ns] ); std::copy(&(data[NS*SPR + SegIndexList[ns-1]]), &(data[NS*SPR + SegIndexList[ns]]), TempSection.get_w().begin() ); try { TempChannel.InsertSection(TempSection, ns-1); } catch (...) { ReturnData.resize(0); destructHDR(hdr); return type; } } try { if ((int)ReturnData.size() < numberOfChannels) { ReturnData.resize(numberOfChannels); } ReturnData.InsertChannel(TempChannel, NS++); } catch (...) { ReturnData.resize(0); destructHDR(hdr); return type; } } free(SegIndexList); ReturnData.SetComment ( biosig_get_recording_id(hdr) ); sprintf(str,"v%i.%i.%i (compiled on %s %s)",BIOSIG_VERSION_MAJOR,BIOSIG_VERSION_MINOR,BIOSIG_PATCHLEVEL,__DATE__,__TIME__); std::string Desc = std::string("importBiosig with libbiosig ")+std::string(str) + " "; const char* tmpstr; if ((tmpstr=biosig_get_technician(hdr))) Desc += std::string ("\nTechnician:\t") + std::string (tmpstr) + " "; Desc += std::string( "\nCreated with: "); if ((tmpstr=biosig_get_manufacturer_name(hdr))) Desc += std::string( tmpstr ) + " "; if ((tmpstr=biosig_get_manufacturer_model(hdr))) Desc += std::string( tmpstr ) + " "; if ((tmpstr=biosig_get_manufacturer_version(hdr))) Desc += std::string( tmpstr ) + " "; if ((tmpstr=biosig_get_manufacturer_serial_number(hdr))) Desc += std::string( tmpstr ) + " "; Desc += std::string ("\nUser specified Annotations:\n")+annotationTableDesc; ReturnData.SetFileDescription(Desc); #if (BIOSIG_VERSION > 10509) tmpstr = biosig_get_application_specific_information(hdr); if (tmpstr != NULL) /* MSVC2008 can not properly handle std::string( (char*)NULL ) */ ReturnData.SetGlobalSectionDescription(tmpstr); #endif ReturnData.SetXScale(1000.0/biosig_get_samplerate(hdr)); ReturnData.SetXUnits("ms"); ReturnData.SetScaling("biosig scaling factor"); /************************************************************************* Date and time conversion *************************************************************************/ struct tm T; biosig_get_startdatetime(hdr, &T); ReturnData.SetDateTime(T); destructHDR(hdr); #else // #ifndef __LIBBIOSIG2_H__ HDRTYPE* hdr = sopen( fName.c_str(), "r", NULL ); if (hdr==NULL) { ReturnData.resize(0); return stfio::none; } type = stfio_file_type(hdr); #if !defined(BIOSIG_VERSION) || (BIOSIG_VERSION < 10501) if (hdr->TYPE==ABF) { /* biosig v1.5.0 and earlier does not always return with a proper error message for ABF files. This causes problems with the ABF fallback mechanism */ #else if ( hdr->TYPE==ABF2 ) { // ABF2 support should be handled by importABF not importBiosig ReturnData.resize(0); destructHDR(hdr); return type; } if (hdr->TYPE==ABF && hdr->AS.B4C_ERRNUM) { /* this triggers the fall back mechanims w/o reporting an error message */ #endif ReturnData.resize(0); destructHDR(hdr); // free allocated memory return type; } #if defined(BIOSIG_VERSION) && (BIOSIG_VERSION > 10400) if (hdr->AS.B4C_ERRNUM) { #else if (B4C_ERRNUM) { #endif ReturnData.resize(0); destructHDR(hdr); // free allocated memory return type; } if ( hdr->TYPE==ATF || hdr->TYPE==HDF) { // ATF, HDF5 support should be handled by importATF and importHDF5 not importBiosig ReturnData.resize(0); destructHDR(hdr); return type; } // earlier versions of biosig support only the file type identification, but did not read AXG files #if defined(BIOSIG_VERSION) && (BIOSIG_VERSION > 10403) if ( (BIOSIG_VERSION < 10600) && (hdr->TYPE==AXG) ) { // biosig's AXG import crashes on Windows at this time ReturnData.resize(0); destructHDR(hdr); return type; } #endif // ensure the event table is in chronological order sort_eventtable(hdr); // allocate local memory for intermediate results; const int strSize=100; char str[strSize]; /* count sections and generate list of indices indicating start and end of sweeps */ size_t numberOfEvents = hdr->EVENT.N; size_t LenIndexList = 256; if (LenIndexList > numberOfEvents) LenIndexList = numberOfEvents + 2; size_t *SegIndexList = (size_t*)malloc(LenIndexList*sizeof(size_t)); uint32_t nsections = 0; SegIndexList[nsections] = 0; size_t MaxSectionLength = 0; for (size_t k=0; k <= numberOfEvents; k++) { if (LenIndexList <= nsections+2) { // allocate more memory as needed LenIndexList *=2; SegIndexList = (size_t*)realloc(SegIndexList, LenIndexList*sizeof(size_t)); } /* count number of sections and stores it in nsections; EVENT.TYP==0x7ffe indicate number of breaks between sweeps SegIndexList includes index to first sample and index to last sample, thus, the effective length of SegIndexList is the number of 0x7ffe plus two. */ if (0) ; else if (k >= hdr->EVENT.N) SegIndexList[++nsections] = hdr->NRec*hdr->SPR; else if (hdr->EVENT.TYP[k]==0x7ffe) SegIndexList[++nsections] = hdr->EVENT.POS[k]; else continue; size_t SPS = SegIndexList[nsections]-SegIndexList[nsections-1]; // length of segment, samples per segment if (MaxSectionLength < SPS) MaxSectionLength = SPS; } int numberOfChannels = 0; for (int k=0; k < hdr->NS; k++) if (hdr->CHANNEL[k].OnOff==1) numberOfChannels++; /************************************************************************* rescale data to mV and pA *************************************************************************/ for (int ch=0; ch < hdr->NS; ++ch) { CHANNEL_TYPE *hc = hdr->CHANNEL+ch; if (hc->OnOff != 1) continue; double scale = PhysDimScale(hc->PhysDimCode); switch (hc->PhysDimCode & 0xffe0) { case 4256: // Volt hc->PhysDimCode = 4274; // = PhysDimCode("mV"); scale *=1e3; // V->mV hc->PhysMax *= scale; hc->PhysMin *= scale; hc->Cal *= scale; hc->Off *= scale; break; case 4160: // Ampere hc->PhysDimCode = 4181; // = PhysDimCode("pA"); scale *=1e12; // A->pA hc->PhysMax *= scale; hc->PhysMin *= scale; hc->Cal *= scale; hc->Off *= scale; break; } } /************************************************************************* read bulk data *************************************************************************/ hdr->FLAG.ROW_BASED_CHANNELS = 0; /* size_t blks = */ sread(NULL, 0, hdr->NRec, hdr); biosig_data_type *data = hdr->data.block; size_t SPR = hdr->NRec*hdr->SPR; #ifdef _STFDEBUG std::cout << "Number of events: " << numberOfEvents << std::endl; /*int res = */ hdr2ascii(hdr, stdout, 4); #endif int NS = 0; // number of non-empty channels for (size_t nc=0; nc < hdr->NS; ++nc) { if (hdr->CHANNEL[nc].OnOff == 0) continue; Channel TempChannel(nsections); TempChannel.SetChannelName(hdr->CHANNEL[nc].Label); #if defined(BIOSIG_VERSION) && (BIOSIG_VERSION > 10301) TempChannel.SetYUnits(PhysDim3(hdr->CHANNEL[nc].PhysDimCode)); #else PhysDim(hdr->CHANNEL[nc].PhysDimCode,str); TempChannel.SetYUnits(str); #endif for (size_t ns=1; ns<=nsections; ns++) { size_t SPS = SegIndexList[ns]-SegIndexList[ns-1]; // length of segment, samples per segment int progbar = 100.0*(1.0*ns/nsections + NS)/numberOfChannels; std::ostringstream progStr; progStr << "Reading channel #" << NS + 1 << " of " << numberOfChannels << ", Section #" << ns << " of " << nsections; progDlg.Update(progbar, progStr.str()); /* unused // char sweepname[20]; sprintf(sweepname,"sweep %i",(int)ns); */ Section TempSection( SPS, // TODO: hdr->nsamplingpoints[nc][ns] "" // TODO: hdr->sectionname[nc][ns] ); std::copy(&(data[NS*SPR + SegIndexList[ns-1]]), &(data[NS*SPR + SegIndexList[ns]]), TempSection.get_w().begin() ); try { TempChannel.InsertSection(TempSection, ns-1); } catch (...) { ReturnData.resize(0); destructHDR(hdr); return type; } } try { if ((int)ReturnData.size() < numberOfChannels) { ReturnData.resize(numberOfChannels); } ReturnData.InsertChannel(TempChannel, NS++); } catch (...) { ReturnData.resize(0); destructHDR(hdr); return type; } } free(SegIndexList); ReturnData.SetComment ( hdr->ID.Recording ); sprintf(str,"v%i.%i.%i (compiled on %s %s)",BIOSIG_VERSION_MAJOR,BIOSIG_VERSION_MINOR,BIOSIG_PATCHLEVEL,__DATE__,__TIME__); std::string Desc = std::string("importBiosig with libbiosig ")+std::string(str); if (hdr->ID.Technician) Desc += std::string ("\nTechnician:\t") + std::string (hdr->ID.Technician); Desc += std::string( "\nCreated with: "); if (hdr->ID.Manufacturer.Name) Desc += std::string( hdr->ID.Manufacturer.Name ); if (hdr->ID.Manufacturer.Model) Desc += std::string( hdr->ID.Manufacturer.Model ); if (hdr->ID.Manufacturer.Version) Desc += std::string( hdr->ID.Manufacturer.Version ); if (hdr->ID.Manufacturer.SerialNumber) Desc += std::string( hdr->ID.Manufacturer.SerialNumber ); Desc += std::string ("\nUser specified Annotations:\n"); for (size_t k=0; k < numberOfEvents; k++) { if (hdr->EVENT.TYP[k] < 256) { sprintf(str,"%f s\t",hdr->EVENT.POS[k]/hdr->EVENT.SampleRate); Desc += std::string( str ); if (hdr->EVENT.CodeDesc != NULL) Desc += std::string( hdr->EVENT.CodeDesc[hdr->EVENT.TYP[k]] ); Desc += "\n"; } } ReturnData.SetFileDescription(Desc); // hdr->AS.bci2000 is an alias to hdr->AS.fpulse, which available only in libbiosig v1.6.0 or later if (hdr->AS.bci2000) ReturnData.SetGlobalSectionDescription(std::string(hdr->AS.bci2000)); ReturnData.SetXScale(1000.0/hdr->SampleRate); ReturnData.SetXUnits("ms"); ReturnData.SetScaling("biosig scaling factor"); /************************************************************************* Date and time conversion *************************************************************************/ struct tm T; #if (BIOSIG_VERSION_MAJOR > 0) gdf_time2tm_time_r(hdr->T0, &T); #else struct tm* Tp; Tp = gdf_time2tm_time(hdr->T0); T = *Tp; #endif ReturnData.SetDateTime(T); destructHDR(hdr); #endif return stfio::biosig; } // ===================================================================================================================== // // Save file with libbiosig into GDF format // // There basically two implementations, one with libbiosig before v1.6.0 and // and one for libbiosig v1.6.0 and later // // ===================================================================================================================== bool stfio::exportBiosigFile(const std::string& fName, const Recording& Data, stfio::ProgressInfo& progDlg) { /* converts the internal data structure to libbiosig's internal structure and saves the file as gdf file. The data in converted into the raw data format, and not into the common data matrix. */ #ifdef __LIBBIOSIG2_H__ size_t numberOfChannels = Data.size(); HDRTYPE* hdr = constructHDR(numberOfChannels, 0); /* Initialize all header parameters */ biosig_set_filetype(hdr, GDF); biosig_set_startdatetime(hdr, Data.GetDateTime()); const char *xunits = Data.GetXUnits().c_str(); uint16_t pdc = PhysDimCode(xunits); if ((pdc & 0xffe0) != PhysDimCode("s")) { fprintf(stderr,"Stimfit exportBiosigFile: xunits [%s] has not proper units, assume [ms]\n",Data.GetXUnits().c_str()); pdc = PhysDimCode("ms"); } double fs = 1.0/(PhysDimScale(pdc) * Data.GetXScale()); biosig_set_samplerate(hdr, fs); #if (BIOSIG_VERSION < 10700) biosig_set_flags(hdr, 0, 0, 0); #else biosig_reset_flag(hdr, BIOSIG_FLAG_COMPRESSION | BIOSIG_FLAG_UCAL | BIOSIG_FLAG_OVERFLOWDETECTION | BIOSIG_FLAG_ROW_BASED_CHANNELS ); #endif size_t k, m, numberOfEvents=0; size_t NRec=0; // corresponds to hdr->NRec size_t SPR=1; // corresponds to hdr->SPR size_t chSPR=0; // corresponds to hc->SPR /* Initialize all channel parameters */ #ifndef DONOTUSE_DYNAMIC_ALLOCATION_FOR_CHANSPR size_t *chanSPR = (size_t*)malloc(numberOfChannels*sizeof(size_t)); #endif for (k = 0; k < numberOfChannels; ++k) { CHANNEL_TYPE *hc = biosig_get_channel(hdr, k); biosig_channel_set_datatype_to_double(hc); biosig_channel_set_scaling(hc, 1e9, -1e9, 1e9, -1e9); biosig_channel_set_label(hc, Data[k].GetChannelName().c_str()); biosig_channel_set_physdim(hc, Data[k].GetYUnits().c_str()); biosig_channel_set_filter(hc, NAN, NAN, NAN); biosig_channel_set_timing_offset(hc, 0.0); biosig_channel_set_impedance(hc, NAN); chSPR = SPR; // each segment gets one marker, roughly numberOfEvents += Data[k].size(); size_t m,len = 0; for (len=0, m = 0; m < Data[k].size(); ++m) { unsigned div = lround(Data[k][m].GetXScale()/Data.GetXScale()); chSPR = lcm(chSPR,div); // sampling interval of m-th segment in k-th channel len += div*Data[k][m].size(); } SPR = lcm(SPR, chSPR); /* hc->SPR (i.e. chSPR) is 'abused' to store final hdr->SPR/hc->SPR, this is corrected in the loop below its a hack to avoid the need for another malloc(). */ #ifdef DONOTUSE_DYNAMIC_ALLOCATION_FOR_CHANSPR biosig_channel_set_samples_per_record(hc, chSPR); #else chanSPR[k]=chSPR; #endif if (k==0) { NRec = len; } else if ((size_t)NRec != len) { destructHDR(hdr); throw std::runtime_error("File can't be exported:\n" "No data or traces have different sizes" ); return false; } } biosig_set_number_of_samples(hdr, NRec, SPR); size_t bpb = 0; for (k = 0; k < numberOfChannels; ++k) { CHANNEL_TYPE *hc = biosig_get_channel(hdr, k); // the 'abuse' of hc->SPR described above is corrected #ifdef DONOTUSE_DYNAMIC_ALLOCATION_FOR_CHANSPR size_t spr = biosig_channel_get_samples_per_record(hc); spr = SPR / spr; biosig_channel_set_samples_per_record(hc, spr); #else size_t spr = SPR/chanSPR[k]; chanSPR[k] = spr; #endif bpb += spr * 8; /* its always double */ } /*** build Event table for storing segment information pre-allocate memory for even table ***/ numberOfEvents *= 2; // about two events per segment biosig_set_number_of_events(hdr, numberOfEvents); /* check whether all segments have same size */ { char flag = (numberOfChannels > 0); size_t m, POS, pos; for (k=0; k < numberOfChannels; ++k) { pos = Data[k].size(); if (k==0) POS = pos; else flag &= (POS == pos); } for (m=0; flag && (m < Data[(size_t)0].size()); ++m) { for (k=0; k < biosig_get_number_of_channels(hdr); ++k) { pos = Data[k][m].size() * lround(Data[k][m].GetXScale()/Data.GetXScale()); if (k==0) POS = pos; else flag &= (POS == pos); } } if (!flag) { destructHDR(hdr); throw std::runtime_error( "File can't be exported:\n" "Traces have different sizes or no channels found" ); return false; } } size_t N=0; k=0; size_t pos = 0; for (m=0; m < (Data[k].size()); ++m) { if (pos > 0) { uint16_t typ=0x7ffe; uint32_t pos32=pos; uint16_t chn=0; uint32_t dur=0; // set break marker biosig_set_nth_event(hdr, N++, &typ, &pos32, &chn, &dur, NULL, NULL); /* // set annotation const char *Desc = Data[k][m].GetSectionDescription().c_str(); if (Desc != NULL && strlen(Desc)>0) biosig_set_nth_event(hdr, N++, NULL, &pos32, &chn, &dur, NULL, Desc); // TODO */ } pos += Data[k][m].size() * lround(Data[k][m].GetXScale()/Data.GetXScale()); } biosig_set_number_of_events(hdr, N); biosig_set_eventtable_samplerate(hdr, fs); sort_eventtable(hdr); /* convert data into GDF rawdata from */ uint8_t *rawdata = (uint8_t*)malloc(bpb * NRec); size_t bi=0; for (k=0; k < numberOfChannels; ++k) { CHANNEL_TYPE *hc = biosig_get_channel(hdr, k); #ifdef DONOTUSE_DYNAMIC_ALLOCATION_FOR_CHANSPR size_t chSPR = biosig_channel_get_samples_per_record(hc); #else size_t chSPR = chanSPR[k]; #endif size_t m,n,len=0; for (m=0; m < Data[k].size(); ++m) { size_t div = lround(Data[k][m].GetXScale()/Data.GetXScale()); size_t div2 = SPR/div; // TODO: avoid using hdr->SPR // fprintf(stdout,"k,m,div,div2: %i,%i,%i,%i\n",(int)k,(int)m,(int)div,(int)div2); // for (n=0; n < Data[k][m].size(); ++n) { uint64_t val; double d = Data[k][m][n]; #if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__APPLE__) val = htole64(*(uint64_t*)&d); #else val = *(uint64_t*)&d; #endif size_t p, spr = (len + n*div) / SPR; for (p=0; p < div2; p++) *(uint64_t*)(rawdata + bi + bpb * spr + p*8) = val; } len += div*Data[k][m].size(); } bi += chSPR*8; } #ifndef DONOTUSE_DYNAMIC_ALLOCATION_FOR_CHANSPR if (chanSPR) free(chanSPR); #endif /****************************** write to file *******************************/ std::string errorMsg("Exception while calling std::exportBiosigFile():\n"); hdr = sopen( fName.c_str(), "w", hdr ); if (serror2(hdr)) { errorMsg += biosig_get_errormsg(hdr); destructHDR(hdr); throw std::runtime_error(errorMsg.c_str()); return false; } ifwrite(rawdata, bpb, NRec, hdr); sclose(hdr); destructHDR(hdr); free(rawdata); #else // #ifndef __LIBBIOSIG2_H__ HDRTYPE* hdr = constructHDR(Data.size(), 0); assert(hdr->NS == Data.size()); /* Initialize all header parameters */ hdr->TYPE = GDF; #if (BIOSIG_VERSION >= 10508) /* transition in biosig to rename HDR->VERSION to HDR->Version to avoid name space conflict with macro VERSION */ hdr->Version = 3.0; // select latest supported version of GDF format #else hdr->VERSION = 3.0; // select latest supported version of GDF format #endif struct tm t = Data.GetDateTime(); hdr->T0 = tm_time2gdf_time(&t); const char *xunits = Data.GetXUnits().c_str(); #if (BIOSIG_VERSION_MAJOR > 0) uint16_t pdc = PhysDimCode(xunits); #else uint16_t pdc = PhysDimCode((char*)xunits); #endif if ((pdc & 0xffe0) == PhysDimCode("s")) { fprintf(stderr,"Stimfit exportBiosigFile: xunits [%s] has not proper units, assume [ms]\n",Data.GetXUnits().c_str()); pdc = PhysDimCode("ms"); } hdr->SampleRate = 1.0/(PhysDimScale(pdc) * Data.GetXScale()); hdr->SPR = 1; hdr->FLAG.UCAL = 0; hdr->FLAG.OVERFLOWDETECTION = 0; hdr->FILE.COMPRESSION = 0; /* Initialize all channel parameters */ size_t k, m; for (k = 0; k < hdr->NS; ++k) { CHANNEL_TYPE *hc = hdr->CHANNEL+k; hc->PhysMin = -1e9; hc->PhysMax = 1e9; hc->DigMin = -1e9; hc->DigMax = 1e9; hc->Cal = 1.0; hc->Off = 0.0; /* Channel descriptions. */ strncpy(hc->Label, Data[k].GetChannelName().c_str(), MAX_LENGTH_LABEL); #if (BIOSIG_VERSION_MAJOR > 0) hc->PhysDimCode = PhysDimCode(Data[k].GetYUnits().c_str()); #else hc->PhysDimCode = PhysDimCode((char*)Data[k].GetYUnits().c_str()); #endif hc->OnOff = 1; hc->LeadIdCode = 0; hc->TOffset = 0.0; hc->Notch = NAN; hc->LowPass = NAN; hc->HighPass = NAN; hc->Impedance= NAN; hc->SPR = hdr->SPR; hc->GDFTYP = 17; // double // each segment gets one marker, roughly hdr->EVENT.N += Data[k].size(); size_t m,len = 0; for (len=0, m = 0; m < Data[k].size(); ++m) { unsigned div = lround(Data[k][m].GetXScale()/Data.GetXScale()); hc->SPR = lcm(hc->SPR,div); // sampling interval of m-th segment in k-th channel len += div*Data[k][m].size(); } hdr->SPR = lcm(hdr->SPR, hc->SPR); if (k==0) { hdr->NRec = len; } else if ((size_t)hdr->NRec != len) { destructHDR(hdr); throw std::runtime_error("File can't be exported:\n" "No data or traces have different sizes" ); return false; } } hdr->AS.bpb = 0; for (k = 0; k < hdr->NS; ++k) { CHANNEL_TYPE *hc = hdr->CHANNEL+k; hc->SPR = hdr->SPR / hc->SPR; hc->bi = hdr->AS.bpb; hdr->AS.bpb += hc->SPR * 8; /* its always double */ } /*** build Event table for storing segment information ***/ size_t N = hdr->EVENT.N * 2; // about two events per segment hdr->EVENT.POS = (uint32_t*)realloc(hdr->EVENT.POS, N * sizeof(*hdr->EVENT.POS)); hdr->EVENT.DUR = (uint32_t*)realloc(hdr->EVENT.DUR, N * sizeof(*hdr->EVENT.DUR)); hdr->EVENT.TYP = (uint16_t*)realloc(hdr->EVENT.TYP, N * sizeof(*hdr->EVENT.TYP)); hdr->EVENT.CHN = (uint16_t*)realloc(hdr->EVENT.CHN, N * sizeof(*hdr->EVENT.CHN)); #if (BIOSIG_VERSION >= 10500) hdr->EVENT.TimeStamp = (gdf_time*)realloc(hdr->EVENT.TimeStamp, N * sizeof(gdf_time)); #endif /* check whether all segments have same size */ { char flag = (hdr->NS>0); size_t m, POS, pos; for (k=0; k < hdr->NS; ++k) { pos = Data[k].size(); if (k==0) POS = pos; else flag &= (POS == pos); } for (m=0; flag && (m < Data[(size_t)0].size()); ++m) { for (k=0; k < hdr->NS; ++k) { pos = Data[k][m].size() * lround(Data[k][m].GetXScale()/Data.GetXScale()); if (k==0) POS = pos; else flag &= (POS == pos); } } if (!flag) { destructHDR(hdr); throw std::runtime_error( "File can't be exported:\n" "Traces have different sizes or no channels found" ); return false; } } N=0; k=0; size_t pos = 0; for (m=0; m < (Data[k].size()); ++m) { if (pos > 0) { // start of new segment after break hdr->EVENT.POS[N] = pos; hdr->EVENT.TYP[N] = 0x7ffe; hdr->EVENT.CHN[N] = 0; hdr->EVENT.DUR[N] = 0; N++; } #if 0 // event description hdr->EVENT.POS[N] = pos; FreeTextEvent(hdr, N, "myevent"); //FreeTextEvent(hdr, N, Data[k][m].GetSectionDescription().c_str()); // TODO hdr->EVENT.CHN[N] = k; hdr->EVENT.DUR[N] = 0; N++; #endif pos += Data[k][m].size() * lround(Data[k][m].GetXScale()/Data.GetXScale()); } hdr->EVENT.N = N; hdr->EVENT.SampleRate = hdr->SampleRate; sort_eventtable(hdr); /* convert data into GDF rawdata from */ hdr->AS.rawdata = (uint8_t*)realloc(hdr->AS.rawdata, hdr->AS.bpb*hdr->NRec); for (k=0; k < hdr->NS; ++k) { CHANNEL_TYPE *hc = hdr->CHANNEL+k; size_t m,n,len=0; for (m=0; m < Data[k].size(); ++m) { size_t div = lround(Data[k][m].GetXScale()/Data.GetXScale()); size_t div2 = hdr->SPR/div; // fprintf(stdout,"k,m,div,div2: %i,%i,%i,%i\n",(int)k,(int)m,(int)div,(int)div2); // for (n=0; n < Data[k][m].size(); ++n) { uint64_t val; double d = Data[k][m][n]; #if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__APPLE__) val = htole64(*(uint64_t*)&d); #else val = *(uint64_t*)&d; #endif size_t p, spr = (len + n*div) / hdr->SPR; for (p=0; p < div2; p++) *(uint64_t*)(hdr->AS.rawdata + hc->bi + hdr->AS.bpb * spr + p*8) = val; } len += div*Data[k][m].size(); } } /****************************** write to file *******************************/ std::string errorMsg("Exception while calling std::exportBiosigFile():\n"); hdr = sopen( fName.c_str(), "w", hdr ); #if (BIOSIG_VERSION > 10400) if (serror2(hdr)) { errorMsg += hdr->AS.B4C_ERRMSG; #else if (serror()) { errorMsg += B4C_ERRMSG; #endif destructHDR(hdr); throw std::runtime_error(errorMsg.c_str()); return false; } ifwrite(hdr->AS.rawdata, hdr->AS.bpb, hdr->NRec, hdr); sclose(hdr); destructHDR(hdr); #endif return true; }
EXTERN_C int sopen_SCP_write(HDRTYPE* hdr) { /* this function is a stub or placeholder and need to be defined in order to be useful. It will be called by the function SOPEN in "biosig.c" Input: char* Header // contains the file content Output: HDRTYPE *hdr // defines the HDR structure accoring to "biosig.h" */ uint8_t* ptr; // pointer to memory mapping of the file layout uint8_t* PtrCurSect; // point to current section int curSect; uint32_t len; uint16_t crc; uint32_t i; uint32_t sectionStart; struct tm* T0_tm; double AVM, avm; aECG_TYPE* aECG; if ((fabs(hdr->VERSION - 1.3)<0.01) && (fabs(hdr->VERSION-2.0)<0.01)) fprintf(stderr,"Warning SOPEN (SCP-WRITE): Version %f not supported\n",hdr->VERSION); uint8_t VERSION = 20; // (uint8_t)round(hdr->VERSION*10); // implemented version number if (hdr->aECG==NULL) { fprintf(stderr,"Warning SOPEN_SCP_WRITE: No aECG info defined\n"); hdr->aECG = malloc(sizeof(aECG_TYPE)); aECG = (aECG_TYPE*)hdr->aECG; aECG->diastolicBloodPressure=0.0; aECG->systolicBloodPressure=0.0; aECG->MedicationDrugs="/0"; aECG->ReferringPhysician="/0"; aECG->LatestConfirmingPhysician="/0"; aECG->Diagnosis="/0"; aECG->EmergencyLevel=0; aECG->Section8.NumberOfStatements = 0; aECG->Section8.Statements = NULL; aECG->Section11.NumberOfStatements = 0; aECG->Section11.Statements = NULL; } else aECG = (aECG_TYPE*)hdr->aECG; //fprintf(stdout,"SCP-Write: IIb %s\n",hdr->aECG->ReferringPhysician); /* predefined values */ aECG->Section1.Tag14.INST_NUMBER = 0; // tag 14, byte 1-2 aECG->Section1.Tag14.DEPT_NUMBER = 0; // tag 14, byte 3-4 aECG->Section1.Tag14.DEVICE_ID = 0; // tag 14, byte 5-6 aECG->Section1.Tag14.DeviceType = 0; // tag 14, byte 7: 0: Cart, 1: System (or Host) aECG->Section1.Tag14.MANUF_CODE = 255; // tag 14, byte 8 (MANUF_CODE has to be 255) aECG->Section1.Tag14.MOD_DESC = "Cart1"; // tag 14, byte 9 (MOD_DESC has to be "Cart1") aECG->Section1.Tag14.VERSION = VERSION; // tag 14, byte 15 (VERSION * 10) aECG->Section1.Tag14.PROT_COMP_LEVEL = 0xA0; // tag 14, byte 16 (PROT_COMP_LEVEL has to be 0xA0 => level II) aECG->Section1.Tag14.LANG_SUPP_CODE = 0x00; // tag 14, byte 17 (LANG_SUPP_CODE has to be 0x00 => Ascii only, latin and 1-byte code) aECG->Section1.Tag14.ECG_CAP_DEV = 0xD0; // tag 14, byte 18 (ECG_CAP_DEV has to be 0xD0 => Acquire, (No Analysis), Print and Store) aECG->Section1.Tag14.MAINS_FREQ = 0; // tag 14, byte 19 (MAINS_FREQ has to be 0: unspecified, 1: 50 Hz, 2: 60Hz) aECG->Section1.Tag14.ANAL_PROG_REV_NUM = ""; aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV = ""; aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID = ""; aECG->Section1.Tag14.ACQ_DEV_SCP_SW = "OpenECG XML-SCP 1.00"; // tag 14, byte 38 (SCP_IMPL_SW has to be "OpenECG XML-SCP 1.00") aECG->Section1.Tag14.ACQ_DEV_MANUF = "Manufacturer"; // tag 14, byte 38 (ACQ_DEV_MANUF has to be "Manufacturer") aECG->Section5.Length = 0; aECG->Section6.Length = 0; /* */ aECG->FLAG.HUFFMAN = 0; aECG->FLAG.REF_BEAT= 0; aECG->FLAG.DIFF = 0; aECG->FLAG.BIMODAL = 0; ptr = (uint8_t*)hdr->AS.Header; int NSections = 12; // initialize section 0 sectionStart = 6+16+NSections*10; ptr = (uint8_t*)realloc(ptr,sectionStart); memset(ptr,0,sectionStart); uint32_t curSectLen = 0; // current section length for (curSect=NSections-1; curSect>=0; curSect--) { curSectLen = 0; // current section length //ptr = (uint8_t*)realloc(ptr,sectionStart+curSectLen); // fprintf(stdout,"Section %i %x\n",curSect,ptr); if (curSect==0) // SECTION 0 { hdr->HeadLen = sectionStart; // length of all other blocks together ptr = (uint8_t*)realloc(ptr,hdr->HeadLen); // total file length curSectLen = 16; // current section length sectionStart = 6; memcpy(ptr+16,"SCPECG",6); // reserved curSectLen += NSections*10; } else if (curSect==1) // SECTION 1 { ptr = (uint8_t*)realloc(ptr,sectionStart+10000); PtrCurSect = ptr+sectionStart; curSectLen = 16; // current section length // Tag 0 (max len = 64) if (!hdr->FLAG.ANONYMOUS && (hdr->Patient.Name != NULL)) { *(ptr+sectionStart+curSectLen) = 0; // tag len = strlen(hdr->Patient.Name) + 1; *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)ptr+sectionStart+curSectLen+3,hdr->Patient.Name,len); // field curSectLen += len+3; } // Tag 1 (max len = 64) Firstname /* *(ptr+sectionStart+curSectLen) = 1; // tag len = strlen(hdr->Patient.Name) + 1; *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)ptr+sectionStart+curSectLen+3,hdr->Patient.Name,len); // field curSectLen += len+3; */ // Tag 2 (max len = 64) Patient ID // if (hdr->Patient.Id != NULL) { if (strlen(hdr->Patient.Id)>0) { *(ptr+sectionStart+curSectLen) = 2; // tag len = strlen(hdr->Patient.Id) + 1; *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)ptr+sectionStart+curSectLen+3,hdr->Patient.Id,len); // field curSectLen += len+3; } // fprintf(stdout,"Section %i Len %i %x\n",curSect,curSectLen,sectionStart); // Tag 3 (max len = 64) Second Last Name /* *(ptr+sectionStart+curSectLen) = 3; // tag len = strlen(hdr->Patient.Name) + 1; *(uint16_t)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy(ptr+sectionStart+curSectLen+3,hdr->Patient.Name,len); // field curSectLen += len+3; */ // Tag 5 (len = 4) if ((hdr->Patient.Birthday) > 0) { T0_tm = gdf_time2tm_time(hdr->Patient.Birthday); *(ptr+sectionStart+curSectLen) = 5; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(4); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16(T0_tm->tm_year+1900);// year *(ptr+sectionStart+curSectLen+5) = (uint8_t)(T0_tm->tm_mon + 1); // month *(ptr+sectionStart+curSectLen+6) = (uint8_t)(T0_tm->tm_mday); // day curSectLen += 7; } // Tag 6 (len = 3) Height if (hdr->Patient.Height>0.0) { *(ptr+sectionStart+curSectLen) = 6; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(3); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16(hdr->Patient.Height); // value *(ptr+sectionStart+curSectLen+5) = 1; // cm curSectLen += 6; } // Tag 7 (len = 3) Weight if (hdr->Patient.Weight>0.0) { *(ptr+sectionStart+curSectLen) = 7; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(3); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16(hdr->Patient.Weight); // value *(ptr+sectionStart+curSectLen+5) = 1; // kg curSectLen += 6; } // Tag 8 (len = 1) if (hdr->Patient.Sex != 0) { *(ptr+sectionStart+curSectLen) = 8; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(1); // length *(ptr+sectionStart+curSectLen+3) = hdr->Patient.Sex; // value curSectLen += 4; } // Tag 11 (len = 2) if (aECG->systolicBloodPressure>0.0) { *(ptr+sectionStart+curSectLen) = 11; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(2); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16((uint16_t)aECG->systolicBloodPressure); // value curSectLen += 5; }; // Tag 12 (len = 2) if (aECG->diastolicBloodPressure>0.0) { *(ptr+sectionStart+curSectLen) = 12; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(2); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16((uint16_t)aECG->diastolicBloodPressure); // value curSectLen += 5; }; // Tag 13 (max len = 80) aECG->Diagnosis=""; len = strlen(aECG->Diagnosis); if (len>0) { *(ptr+sectionStart+curSectLen) = 13; // tag len = min(64,len+1); *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->Diagnosis,len); curSectLen += 3+len; }; // Tag 14 (max len = 2 + 2 + 2 + 1 + 1 + 6 + 1 + 1 + 1 + 1 + 1 + 16 + 1 + 25 + 25 + 25 + 25 + 25) // Total = 161 (max value) *(ptr+sectionStart+curSectLen) = 14; // tag //len = 41; // minimum length // *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length memset(ptr+sectionStart+curSectLen+3,0,41); // dummy value curSectLen += 3; *(uint16_t*)(ptr+sectionStart+curSectLen) = aECG->Section1.Tag14.INST_NUMBER; *(uint16_t*)(ptr+sectionStart+curSectLen+2) = aECG->Section1.Tag14.DEPT_NUMBER; *(uint16_t*)(ptr+sectionStart+curSectLen+4) = aECG->Section1.Tag14.DEVICE_ID; *(ptr+sectionStart+curSectLen+ 6) = aECG->Section1.Tag14.DeviceType; *(ptr+sectionStart+curSectLen+ 7) = aECG->Section1.Tag14.MANUF_CODE; // tag 14, byte 7 (MANUF_CODE has to be 255) strncpy((char*)(ptr+sectionStart+curSectLen+8), aECG->Section1.Tag14.MOD_DESC, 6); // tag 14, byte 7 (MOD_DESC has to be "Cart1") *(ptr+sectionStart+curSectLen+14) = VERSION; // tag 14, byte 14 (VERSION has to be 20) *(ptr+sectionStart+curSectLen+14) = aECG->Section1.Tag14.VERSION; *(ptr+sectionStart+curSectLen+15) = aECG->Section1.Tag14.PROT_COMP_LEVEL; // tag 14, byte 15 (PROT_COMP_LEVEL has to be 0xA0 => level II) *(ptr+sectionStart+curSectLen+16) = aECG->Section1.Tag14.LANG_SUPP_CODE; // tag 14, byte 16 (LANG_SUPP_CODE has to be 0x00 => Ascii only, latin and 1-byte code) *(ptr+sectionStart+curSectLen+17) = aECG->Section1.Tag14.ECG_CAP_DEV; // tag 14, byte 17 (ECG_CAP_DEV has to be 0xD0 => Acquire, (No Analysis), Print and Store) *(ptr+sectionStart+curSectLen+18) = aECG->Section1.Tag14.MAINS_FREQ; // tag 14, byte 18 (MAINS_FREQ has to be 0: unspecified, 1: 50 Hz, 2: 60Hz) *(ptr+sectionStart+curSectLen+35) = strlen(aECG->Section1.Tag14.ANAL_PROG_REV_NUM)+1; // tag 14, byte 34 => length of ANAL_PROG_REV_NUM + 1 = 1 uint16_t len1 = 36; char* tmp; tmp = aECG->Section1.Tag14.ANAL_PROG_REV_NUM; len = min(25, strlen(tmp) + 1); strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); len1 += len; tmp = aECG->Section1.Tag14.SERIAL_NUMBER_ACQ_DEV; len = min(25, strlen(tmp) + 1); strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); len1 += len; tmp = aECG->Section1.Tag14.ACQ_DEV_SYS_SW_ID; len = min(25, strlen(tmp) + 1); strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); len1 += len; tmp = aECG->Section1.Tag14.ACQ_DEV_SCP_SW; len = min(25, strlen(tmp) + 1); strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); len1 += len; tmp = aECG->Section1.Tag14.ACQ_DEV_MANUF; len = min(25, strlen(tmp) + 1); strncpy((char*)(ptr+sectionStart+curSectLen+len1), tmp, len); len1 += len; *(uint16_t*)(ptr+sectionStart+curSectLen+1-3) = l_endian_u16(len1); // length curSectLen += len1; // Tag 16 (max len = 80) len = strlen(hdr->ID.Hospital); if (len>0) { *(ptr+sectionStart+curSectLen) = 16; // tag len = min(64,len+1); *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)(ptr+sectionStart+curSectLen+3),hdr->ID.Hospital,len); curSectLen += 3+len; }; // Tag 20 (max len = 64 ) len = strlen(aECG->ReferringPhysician); if (len>0) { *(ptr+sectionStart+curSectLen) = 20; // tag len = min(64,len+1); *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->ReferringPhysician,len); curSectLen += 3+len; }; // Tag 21 (max len = 64 ) len = strlen(aECG->MedicationDrugs); if (len>0) { *(ptr+sectionStart+curSectLen) = 21; // tag len = min(64,len+1); *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)(ptr+sectionStart+curSectLen+3),aECG->MedicationDrugs,len); curSectLen += 3+len; }; // Tag 22 (max len = 40 ) len = strlen(hdr->ID.Technician); if (len>0) { *(ptr+sectionStart+curSectLen) = 22; // tag len = min(64,len+1); *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(len); // length strncpy((char*)(ptr+sectionStart+curSectLen+3),hdr->ID.Technician,len); curSectLen += 3+len; }; // Tag 24 ( len = 1 ) *(ptr+sectionStart+curSectLen) = 24; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(1); // length *(ptr+sectionStart+curSectLen+3) = aECG->EmergencyLevel; curSectLen += 4; // Tag 25 (len = 4) gdf_time T1 = hdr->T0; #ifndef __APPLE__ T1 += (int32_t)ldexp(timezone/86400.0,32); #endif T0_tm = gdf_time2tm_time(T1); *(ptr+sectionStart+curSectLen) = 25; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(4); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16((uint16_t)(T0_tm->tm_year+1900));// year *(ptr+sectionStart+curSectLen+5) = (uint8_t)(T0_tm->tm_mon + 1);// month *(ptr+sectionStart+curSectLen+6) = (uint8_t)T0_tm->tm_mday; // day curSectLen += 7; // Tag 26 (len = 3) *(ptr+sectionStart+curSectLen) = 26; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(3); // length *(ptr+sectionStart+curSectLen+3) = (uint8_t)T0_tm->tm_hour; // hour *(ptr+sectionStart+curSectLen+4) = (uint8_t)T0_tm->tm_min; // minute *(ptr+sectionStart+curSectLen+5) = (uint8_t)T0_tm->tm_sec; // second curSectLen += 6; if (hdr->NS>0) { // Tag 27 (len = 3) highpass filter *(ptr+sectionStart+curSectLen) = 27; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(2); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = (uint16_t)hdr->CHANNEL[1].HighPass; // hour curSectLen += 5; // Tag 28 (len = 3) lowpass filter *(ptr+sectionStart+curSectLen) = 28; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(2); // length *(uint16_t*)(ptr+sectionStart+curSectLen+3) = (uint16_t)hdr->CHANNEL[1].LowPass; // hour curSectLen += 5; // Tag 29 (len = 1) filter bitmap uint8_t bitmap = 0; if (fabs(hdr->CHANNEL[1].LowPass-60.0)<0.01) bitmap = 1; else if (fabs(hdr->CHANNEL[1].LowPass-50.0)<0.01) bitmap = 2; else bitmap = 0; *(ptr+sectionStart+curSectLen) = 29; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(1); // length *(ptr+sectionStart+curSectLen+3) = bitmap; curSectLen += 4; } // Tag 32 (len = 5) *(ptr+sectionStart+curSectLen) = 32; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(2); // length if (hdr->Patient.Impairment.Heart==1) { *(ptr+sectionStart+curSectLen+3) = 0; *(ptr+sectionStart+curSectLen+4) = 1; // Apparently healthy curSectLen += 5; } else if (hdr->Patient.Impairment.Heart==3) { *(ptr+sectionStart+curSectLen+3) = 0; *(ptr+sectionStart+curSectLen+4) = 42; // Implanted cardiac pacemaker curSectLen += 5; } // Tag 34 (len = 5) *(ptr+sectionStart+curSectLen) = 34; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(5); // length // FIXME: compensation for daylight saving time not included #ifdef __APPLE__ // ### FIXME: for some (unknown) reason, timezone does not work on MacOSX *(int16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_i16(0x7fff); printf("Warning SOPEN(SCP,write): timezone not supported\n"); #else *(int16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_i16((int16_t)lrint(-timezone/60.0)); #endif //*(int16_t*)(ptr+sectionStart+curSectLen+3) = l_endian_u16((int16_t)round(T0_tm->tm_gmtoff/60)); *(int16_t*)(ptr+sectionStart+curSectLen+5) = 0; curSectLen += 8; // Tag 255 (len = 0) *(ptr+sectionStart+curSectLen) = 255; // tag *(uint16_t*)(ptr+sectionStart+curSectLen+1) = l_endian_u16(0); // length curSectLen += 3; // Evaluate the size and correct it if odd if (curSectLen & 1) { *(ptr+sectionStart+curSectLen++) = 0; } } else if (curSect==2) // SECTION 2 { } else if (curSect==3) // SECTION 3 { ptr = (uint8_t*)realloc(ptr,sectionStart+16+2+9*hdr->NS+1); PtrCurSect = ptr+sectionStart; curSectLen = 16; // current section length // Number of leads enclosed *(ptr+sectionStart+curSectLen++) = hdr->NS; // ### Situations with reference beat subtraction are not supported // Situations with not all the leads simultaneously recorded are not supported // Situations number of leads simultaneouly recorded != total number of leads are not supported // We assume all the leads are recorded simultaneously *(ptr+sectionStart+curSectLen++) = (hdr->NS<<3) | 0x04; for (i = 0; i < hdr->NS; i++) { *(uint32_t*)(ptr+sectionStart+curSectLen) = l_endian_u32(1L); *(uint32_t*)(ptr+sectionStart+curSectLen+4) = l_endian_u32(hdr->data.size[0]); *(ptr+sectionStart+curSectLen+8) = (uint8_t)hdr->CHANNEL[i].LeadIdCode; curSectLen += 9; } // Evaluate the size and correct it if odd if ((curSectLen % 2) != 0) { *(ptr+sectionStart+curSectLen++) = 0; } memset(ptr+sectionStart+10,0,6); // reserved } else if (curSect==4) // SECTION 4 { } else if (curSect==5) // SECTION 5 { curSectLen = 0; // current section length aECG->Section5.StartPtr = sectionStart; aECG->Section5.Length = curSectLen; } else if (curSect==6) // SECTION 6 { uint16_t GDFTYP = 3; size_t SZ = GDFTYP_BITS[GDFTYP]>>3; for (i = 0; i < hdr->NS; i++) hdr->CHANNEL[i].GDFTYP = GDFTYP; ptr = (uint8_t*)realloc(ptr,sectionStart+16+6+2*hdr->NS+SZ*(hdr->data.size[0]*hdr->data.size[1])); PtrCurSect = ptr+sectionStart; curSectLen = 16; // current section length // Create all the fields // Amplitude Value Multiplier (AVM) i = 0; AVM = hdr->CHANNEL[i].Cal * 1e9 * PhysDimScale(hdr->CHANNEL[i].PhysDimCode); for (i = 1; i < hdr->NS; i++) { // check for physical dimension and adjust scaling factor to "nV" avm = hdr->CHANNEL[i].Cal * 1e9 * PhysDimScale(hdr->CHANNEL[i].PhysDimCode); // check whether all channels have the same scaling factor if (fabs((AVM - avm)/AVM) > 1e-14) fprintf(stderr,"Warning SOPEN (SCP-WRITE): scaling factors differ between channel #1 and #%i. Scaling factor of 1st channel is used.\n",i+1); }; *(uint16_t*)(ptr+sectionStart+curSectLen) = l_endian_u16((uint16_t)lrint(AVM)); avm = l_endian_u16(*(uint16_t*)(ptr+sectionStart+curSectLen)); curSectLen += 2; if (fabs((AVM - avm)/AVM)>1e-14) fprintf(stderr,"Warning SOPEN (SCP-WRITE): Scaling factor has been truncated (%f instead %f).\n",avm,AVM); // Sample interval AVM = 1e6/hdr->SampleRate; *(uint16_t*)(ptr+sectionStart+curSectLen) = l_endian_u16((uint16_t)lrint(AVM)); avm = l_endian_u16(*(uint16_t*)(ptr+sectionStart+curSectLen)); curSectLen += 2; if (fabs((AVM - avm)/AVM)>1e-14) fprintf(stderr,"Warning SOPEN (SCP-WRITE): Sampling interval has been truncated (%f instead %f us).\n",avm,AVM); // Diff used *(ptr+sectionStart+curSectLen++) = 0; // Bimodal/Non-bimodal *(ptr+sectionStart+curSectLen++) = 0; /* DATA COMPRESSION currently, no compression method is supported. In case of data compression, the data compression can happen here. */ // Fill the length block for (i = 0; i < hdr->NS; i++) { *(uint16_t*)(ptr+sectionStart+curSectLen) = l_endian_u16((uint16_t)hdr->data.size[0]*2); avm = l_endian_u16(*(uint16_t*)(ptr+sectionStart+curSectLen)); AVM = hdr->data.size[0]*2; if (fabs((AVM - avm)/AVM)>1e-14) fprintf(stderr,"Warning SOPEN (SCP-WRITE): Block length truncated (%f instead %f us).\n",avm,AVM); curSectLen += 2; } /* data in channel multiplexed order */ for (i = 0; i < hdr->NS; i++) { hdr->CHANNEL[i].SPR *= hdr->NRec; }; hdr->NRec = 1; // Prepare filling the data block with the ECG samples by SWRITE // free(hdr->AS.rawdata); // hdr->AS.rawdata = PtrCurSect+16+6+2*hdr->NS; curSectLen += SZ*(hdr->data.size[0]*hdr->data.size[1]); // Evaluate the size and correct it if odd if ((curSectLen % 2) != 0) { fprintf(stderr,"Warning Section 6 has an odd length\n"); *(ptr+sectionStart+curSectLen++) = 0; } memset(ptr+sectionStart+10,0,6); // reserved aECG->Section6.StartPtr = sectionStart; aECG->Section6.Length = curSectLen; } else if (curSect==7) // SECTION 7