PaxHeader/edflib.c000644 001750 000144 00000000066 13532510714 014423 xustar00guvusers000000 000000 30 mtime=1567265228.053619792 24 SCHILY.fflags=extent edflib.c000644 001750 000144 00000412577 13532510714 012470 0ustar00guvusers000000 000000 /* ***************************************************************************** * * Copyright (c) 2009 - 2019 Teunis van Beelen * All rights reserved. * * Email: teuniz@protonmail.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ***************************************************************************** */ /* compile with options "-D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE" */ #include "edflib.h" #define EDFLIB_VERSION 116 #define EDFLIB_MAXFILES 64 #if defined(__APPLE__) || defined(__MACH__) || defined(__APPLE_CC__) #define fopeno fopen #else #define fseeko fseeko64 #define ftello ftello64 #define fopeno fopen64 #endif #ifdef _WIN32 #ifndef __MINGW32__ /* needed for visual c */ #undef fseeko #define fseeko _fseeki64 #undef ftello #define ftello _ftelli64 #undef fopeno #define fopeno fopen #endif #endif /* max size of annotationtext */ #define EDFLIB_WRITE_MAX_ANNOTATION_LEN 40 /* bytes in datarecord for EDF annotations, must be an integer multiple of three and two */ #define EDFLIB_ANNOTATION_BYTES 114 /* for writing only */ #define EDFLIB_MAX_ANNOTATION_CHANNELS 64 #define EDFLIB_ANNOT_MEMBLOCKSZ 1000 struct edfparamblock{ char label[17]; char transducer[81]; char physdimension[9]; double phys_min; double phys_max; int dig_min; int dig_max; char prefilter[81]; int smp_per_record; char reserved[33]; double offset; int buf_offset; double bitvalue; int annotation; long long sample_pntr; }; struct edfhdrblock{ FILE *file_hdl; char path[1024]; int writemode; char version[32]; char patient[81]; char recording[81]; char plus_patientcode[81]; char plus_gender[16]; char plus_birthdate[16]; char plus_patient_name[81]; char plus_patient_additional[81]; char plus_startdate[16]; char plus_admincode[81]; char plus_technician[81]; char plus_equipment[81]; char plus_recording_additional[81]; long long l_starttime; int startdate_day; int startdate_month; int startdate_year; int starttime_second; int starttime_minute; int starttime_hour; char reserved[45]; int hdrsize; int edfsignals; long long datarecords; int recordsize; int annot_ch[EDFLIB_MAXSIGNALS]; int nr_annot_chns; int mapped_signals[EDFLIB_MAXSIGNALS]; int edf; int edfplus; int bdf; int bdfplus; int discontinuous; int signal_write_sequence_pos; long long starttime_offset; double data_record_duration; long long long_data_record_duration; int annots_in_file; int annotlist_sz; int total_annot_bytes; int eq_sf; char *wrbuf; int wrbufsize; struct edfparamblock *edfparam; }; static struct edf_annotationblock{ long long onset; char duration[16]; char annotation[EDFLIB_MAX_ANNOTATION_LEN + 1]; } *annotationslist[EDFLIB_MAXFILES]; static struct edf_write_annotationblock{ long long onset; long long duration; char annotation[EDFLIB_WRITE_MAX_ANNOTATION_LEN + 1]; } *write_annotationslist[EDFLIB_MAXFILES]; static int edf_files_open=0; static struct edfhdrblock *hdrlist[EDFLIB_MAXFILES]; static struct edfhdrblock * edflib_check_edf_file(FILE *, int *); static int edflib_is_integer_number(char *); static int edflib_is_number(char *); static long long edflib_get_long_duration(char *); static int edflib_get_annotations(struct edfhdrblock *, int, int); static int edflib_is_duration_number(char *); static int edflib_is_onset_number(char *); static long long edflib_get_long_time(char *); static int edflib_write_edf_header(struct edfhdrblock *); static void edflib_latin1_to_ascii(char *, int); static void edflib_latin12utf8(char *, int); static void edflib_remove_padding_trailing_spaces(char *); static int edflib_atoi_nonlocalized(const char *); static double edflib_atof_nonlocalized(const char *); static int edflib_snprint_number_nonlocalized(char *, double, int); /* static int edflib_sprint_int_number_nonlocalized(char *, int, int, int); */ static int edflib_snprint_ll_number_nonlocalized(char *, long long, int, int, int); static int edflib_fprint_int_number_nonlocalized(FILE *, int, int, int); static int edflib_fprint_ll_number_nonlocalized(FILE *, long long, int, int); static int edflib_write_tal(struct edfhdrblock *, FILE *); static int edflib_strlcpy(char *, const char *, int); static int edflib_strlcat(char *, const char *, int); int edflib_is_file_used(const char *path) { int i; for(i=0; ipath))) { return 1; } } } return 0; } int edflib_get_number_of_open_files() { return edf_files_open; } int edflib_get_handle(int file_number) { int i, file_count=0; for(i=0; ifiletype = EDFLIB_INVALID_READ_ANNOTS_VALUE; return -1; } if(read_annotations>2) { edfhdr->filetype = EDFLIB_INVALID_READ_ANNOTS_VALUE; return -1; } memset(edfhdr, 0, sizeof(struct edf_hdr_struct)); if(edf_files_open>=EDFLIB_MAXFILES) { edfhdr->filetype = EDFLIB_MAXFILES_REACHED; return -1; } for(i=0; ipath))) { edfhdr->filetype = EDFLIB_FILE_ALREADY_OPENED; return -1; } } } file = fopeno(path, "rb"); if(file==NULL) { edfhdr->filetype = EDFLIB_NO_SUCH_FILE_OR_DIRECTORY; return -1; } hdr = edflib_check_edf_file(file, &edf_error); if(hdr==NULL) { edfhdr->filetype = edf_error; fclose(file); return -1; } if(hdr->discontinuous) { edfhdr->filetype = EDFLIB_FILE_IS_DISCONTINUOUS; free(hdr->edfparam); free(hdr); fclose(file); return -1; } hdr->writemode = 0; for(i=0; ihandle = i; break; } } if((hdr->edf)&&(!(hdr->edfplus))) { edfhdr->filetype = EDFLIB_FILETYPE_EDF; } if(hdr->edfplus) { edfhdr->filetype = EDFLIB_FILETYPE_EDFPLUS; } if((hdr->bdf)&&(!(hdr->bdfplus))) { edfhdr->filetype = EDFLIB_FILETYPE_BDF; } if(hdr->bdfplus) { edfhdr->filetype = EDFLIB_FILETYPE_BDFPLUS; } edfhdr->edfsignals = hdr->edfsignals - hdr->nr_annot_chns; edfhdr->file_duration = hdr->long_data_record_duration * hdr->datarecords; edfhdr->startdate_day = hdr->startdate_day; edfhdr->startdate_month = hdr->startdate_month; edfhdr->startdate_year = hdr->startdate_year; edfhdr->starttime_hour = hdr->starttime_hour; edfhdr->starttime_second = hdr->starttime_second; edfhdr->starttime_minute = hdr->starttime_minute; edfhdr->starttime_subsecond = hdr->starttime_offset; edfhdr->datarecords_in_file = hdr->datarecords; edfhdr->datarecord_duration = hdr->long_data_record_duration; annotationslist[edfhdr->handle] = NULL; hdr->annotlist_sz = 0; hdr->annots_in_file = 0; edfhdr->annotations_in_file = 0LL; if((!(hdr->edfplus))&&(!(hdr->bdfplus))) { edflib_strlcpy(edfhdr->patient, hdr->patient, 81); edflib_strlcpy(edfhdr->recording, hdr->recording, 81); edfhdr->patientcode[0] = 0; edfhdr->gender[0] = 0; edfhdr->birthdate[0] = 0; edfhdr->patient_name[0] = 0; edfhdr->patient_additional[0] = 0; edfhdr->admincode[0] = 0; edfhdr->technician[0] = 0; edfhdr->equipment[0] = 0; edfhdr->recording_additional[0] = 0; } else { edfhdr->patient[0] = 0; edfhdr->recording[0] = 0; edflib_strlcpy(edfhdr->patientcode, hdr->plus_patientcode, 81); edflib_strlcpy(edfhdr->gender, hdr->plus_gender, 16); edflib_strlcpy(edfhdr->birthdate, hdr->plus_birthdate, 16); edflib_strlcpy(edfhdr->patient_name, hdr->plus_patient_name, 81); edflib_strlcpy(edfhdr->patient_additional, hdr->plus_patient_additional, 81); edflib_strlcpy(edfhdr->admincode, hdr->plus_admincode, 81); edflib_strlcpy(edfhdr->technician, hdr->plus_technician, 81); edflib_strlcpy(edfhdr->equipment, hdr->plus_equipment, 81); edflib_strlcpy(edfhdr->recording_additional, hdr->plus_recording_additional, 81); if((read_annotations==EDFLIB_READ_ANNOTATIONS)||(read_annotations==EDFLIB_READ_ALL_ANNOTATIONS)) { if(edflib_get_annotations(hdr, edfhdr->handle, read_annotations)) { edfhdr->filetype = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; fclose(file); free(hdr->edfparam); hdr->edfparam = NULL; free(hdr); hdr = NULL; hdrlist[edfhdr->handle] = NULL; free(annotationslist[edfhdr->handle]); annotationslist[edfhdr->handle] = NULL; return -1; } } edfhdr->annotations_in_file = hdr->annots_in_file; } edflib_strlcpy(hdr->path, path, 1024); edf_files_open++; j = 0; for(i=0; iedfsignals; i++) { if(!(hdr->edfparam[i].annotation)) { hdr->mapped_signals[j++] = i; } } for(i=0; iedfsignals; i++) { channel = hdr->mapped_signals[i]; edflib_strlcpy(edfhdr->signalparam[i].label, hdr->edfparam[channel].label, 17); edflib_strlcpy(edfhdr->signalparam[i].transducer, hdr->edfparam[channel].transducer, 81); edflib_strlcpy(edfhdr->signalparam[i].physdimension, hdr->edfparam[channel].physdimension, 9); edflib_strlcpy(edfhdr->signalparam[i].prefilter, hdr->edfparam[channel].prefilter, 81); edfhdr->signalparam[i].smp_in_file = hdr->edfparam[channel].smp_per_record * hdr->datarecords; edfhdr->signalparam[i].phys_max = hdr->edfparam[channel].phys_max; edfhdr->signalparam[i].phys_min = hdr->edfparam[channel].phys_min; edfhdr->signalparam[i].dig_max = hdr->edfparam[channel].dig_max; edfhdr->signalparam[i].dig_min = hdr->edfparam[channel].dig_min; edfhdr->signalparam[i].smp_in_datarecord = hdr->edfparam[channel].smp_per_record; } return 0; } int edfclose_file(int handle) { struct edf_write_annotationblock *annot2; int i, j, k, n, p, err, datrecsize, nmemb; long long offset, datarecords; char str[EDFLIB_ANNOTATION_BYTES * 2]; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } hdr = hdrlist[handle]; if(hdr->writemode) { if(hdr->datarecords == 0LL) { err = edflib_write_edf_header(hdr); if(err) { fclose(hdr->file_hdl); free(hdr->edfparam); free(hdr->wrbuf); free(hdr); hdrlist[handle] = NULL; free(write_annotationslist[handle]); write_annotationslist[handle] = NULL; edf_files_open--; return err; } for(k=0; kannots_in_file; k++) { annot2 = write_annotationslist[handle] + k; p = edflib_fprint_ll_number_nonlocalized(hdr->file_hdl, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) { fputc('.', hdr->file_hdl); p++; p += edflib_fprint_ll_number_nonlocalized(hdr->file_hdl, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); } fputc(20, hdr->file_hdl); fputc(20, hdr->file_hdl); p += 2; for(; ptotal_annot_bytes; p++) { fputc(0, hdr->file_hdl); } hdr->datarecords++; } } if(hdr->datarecords<100000000LL) { fseeko(hdr->file_hdl, 236LL, SEEK_SET); p = edflib_fprint_int_number_nonlocalized(hdr->file_hdl, (int)(hdr->datarecords), 0, 0); if(p < 2) { fputc(' ', hdr->file_hdl); } } datarecords = 0LL; offset = (long long)((hdr->edfsignals + hdr->nr_annot_chns + 1) * 256); datrecsize = hdr->total_annot_bytes; for(i=0; iedfsignals; i++) { if(hdr->edf) { offset += (long long)(hdr->edfparam[i].smp_per_record * 2); datrecsize += (hdr->edfparam[i].smp_per_record * 2); } else { offset += (long long)(hdr->edfparam[i].smp_per_record * 3); datrecsize += (hdr->edfparam[i].smp_per_record * 3); } } j = 0; for(k=0; kannots_in_file; k++) { annot2 = write_annotationslist[handle] + k; p = 0; if(j==0) // first annotation signal { if(fseeko(hdr->file_hdl, offset, SEEK_SET)) { break; } p += edflib_snprint_ll_number_nonlocalized(str, (datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1, EDFLIB_ANNOTATION_BYTES * 2); if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) { str[p++] = '.'; n = edflib_snprint_ll_number_nonlocalized(str + p, (datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0, (EDFLIB_ANNOTATION_BYTES * 2) - p); p += n; } str[p++] = 20; str[p++] = 20; str[p++] = 0; } n = edflib_snprint_ll_number_nonlocalized(str + p, annot2->onset / 10000LL, 0, 1, (EDFLIB_ANNOTATION_BYTES * 2) - p); p += n; if(annot2->onset % 10000LL) { str[p++] = '.'; n = edflib_snprint_ll_number_nonlocalized(str + p, annot2->onset % 10000LL, 4, 0, (EDFLIB_ANNOTATION_BYTES * 2) - p); p += n; } if(annot2->duration>=0LL) { str[p++] = 21; n = edflib_snprint_ll_number_nonlocalized(str + p, annot2->duration / 10000LL, 0, 0, (EDFLIB_ANNOTATION_BYTES * 2) - p); p += n; if(annot2->duration % 10000LL) { str[p++] = '.'; n = edflib_snprint_ll_number_nonlocalized(str + p, annot2->duration % 10000LL, 4, 0, (EDFLIB_ANNOTATION_BYTES * 2) - p); p += n; } } str[p++] = 20; for(i=0; iannotation[i]==0) { break; } str[p++] = annot2->annotation[i]; } str[p++] = 20; for(; pfile_hdl); if(nmemb != 1) { break; } j++; if(j >= hdr->nr_annot_chns) { j = 0; offset += datrecsize; datarecords++; if(datarecords>=hdr->datarecords) { break; } } } free(write_annotationslist[handle]); } else { free(annotationslist[handle]); } fclose(hdr->file_hdl); free(hdr->edfparam); free(hdr->wrbuf); free(hdr); hdrlist[handle] = NULL; edf_files_open--; return 0; } long long edfseek(int handle, int edfsignal, long long offset, int whence) { long long smp_in_file; int channel; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(edfsignal<0) { return -1; } if(hdrlist[handle]->writemode) { return -1; } if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns)) { return -1; } channel = hdrlist[handle]->mapped_signals[edfsignal]; smp_in_file = hdrlist[handle]->edfparam[channel].smp_per_record * hdrlist[handle]->datarecords; if(whence==EDFSEEK_SET) { hdrlist[handle]->edfparam[channel].sample_pntr = offset; } if(whence==EDFSEEK_CUR) { hdrlist[handle]->edfparam[channel].sample_pntr += offset; } if(whence==EDFSEEK_END) { hdrlist[handle]->edfparam[channel].sample_pntr = (hdrlist[handle]->edfparam[channel].smp_per_record * hdrlist[handle]->datarecords) + offset; } if(hdrlist[handle]->edfparam[channel].sample_pntr > smp_in_file) { hdrlist[handle]->edfparam[channel].sample_pntr = smp_in_file; } if(hdrlist[handle]->edfparam[channel].sample_pntr < 0LL) { hdrlist[handle]->edfparam[channel].sample_pntr = 0LL; } return hdrlist[handle]->edfparam[channel].sample_pntr; } long long edftell(int handle, int edfsignal) { int channel; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(edfsignal<0) { return -1; } if(hdrlist[handle]->writemode) { return -1; } if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns)) { return -1; } channel = hdrlist[handle]->mapped_signals[edfsignal]; return hdrlist[handle]->edfparam[channel].sample_pntr; } void edfrewind(int handle, int edfsignal) { int channel; if(handle<0) { return; } if(handle>=EDFLIB_MAXFILES) { return; } if(hdrlist[handle]==NULL) { return; } if(edfsignal<0) { return; } if(hdrlist[handle]->writemode) { return; } if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns)) { return; } channel = hdrlist[handle]->mapped_signals[edfsignal]; hdrlist[handle]->edfparam[channel].sample_pntr = 0LL; } int edfread_physical_samples(int handle, int edfsignal, int n, double *buf) { int bytes_per_smpl=2, tmp, i, channel; double phys_bitvalue, phys_offset; long long smp_in_file, offset, sample_pntr, smp_per_record, jump; struct edfhdrblock *hdr; union { unsigned int one; signed int one_signed; unsigned short two[2]; signed short two_signed[2]; unsigned char four[4]; } var; FILE *file; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(edfsignal<0) { return -1; } if(hdrlist[handle]->writemode) { return -1; } if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns)) { return -1; } channel = hdrlist[handle]->mapped_signals[edfsignal]; if(n<0LL) { return -1; } if(n==0LL) { return 0LL; } hdr = hdrlist[handle]; if(hdr->edf) { bytes_per_smpl = 2; } if(hdr->bdf) { bytes_per_smpl = 3; } smp_in_file = hdr->edfparam[channel].smp_per_record * hdr->datarecords; if((hdr->edfparam[channel].sample_pntr + n) > smp_in_file) { n = smp_in_file - hdr->edfparam[channel].sample_pntr; if(n==0) { return 0LL; } if(n<0) { return -1; } } file = hdr->file_hdl; offset = hdr->hdrsize; offset += (hdr->edfparam[channel].sample_pntr / hdr->edfparam[channel].smp_per_record) * hdr->recordsize; offset += hdr->edfparam[channel].buf_offset; offset += ((hdr->edfparam[channel].sample_pntr % hdr->edfparam[channel].smp_per_record) * bytes_per_smpl); fseeko(file, offset, SEEK_SET); sample_pntr = hdr->edfparam[channel].sample_pntr; smp_per_record = hdr->edfparam[channel].smp_per_record; jump = hdr->recordsize - (smp_per_record * bytes_per_smpl); phys_bitvalue = hdr->edfparam[channel].bitvalue; phys_offset = hdr->edfparam[channel].offset; if(hdr->edf) { for(i=0; ibdf) { for(i=0; iedfparam[channel].sample_pntr = sample_pntr; return n; } int edfread_digital_samples(int handle, int edfsignal, int n, int *buf) { int bytes_per_smpl=2, tmp, i, channel; long long smp_in_file, offset, sample_pntr, smp_per_record, jump; struct edfhdrblock *hdr; union { unsigned int one; signed int one_signed; unsigned short two[2]; signed short two_signed[2]; unsigned char four[4]; } var; FILE *file; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(edfsignal<0) { return -1; } if(hdrlist[handle]->writemode) { return -1; } if(edfsignal>=(hdrlist[handle]->edfsignals - hdrlist[handle]->nr_annot_chns)) { return -1; } channel = hdrlist[handle]->mapped_signals[edfsignal]; if(n<0LL) { return -1; } if(n==0LL) { return 0LL; } hdr = hdrlist[handle]; if(hdr->edf) { bytes_per_smpl = 2; } if(hdr->bdf) { bytes_per_smpl = 3; } smp_in_file = hdr->edfparam[channel].smp_per_record * hdr->datarecords; if((hdr->edfparam[channel].sample_pntr + n) > smp_in_file) { n = smp_in_file - hdr->edfparam[channel].sample_pntr; if(n==0) { return 0LL; } if(n<0) { return -1; } } file = hdr->file_hdl; offset = hdr->hdrsize; offset += (hdr->edfparam[channel].sample_pntr / hdr->edfparam[channel].smp_per_record) * hdr->recordsize; offset += hdr->edfparam[channel].buf_offset; offset += ((hdr->edfparam[channel].sample_pntr % hdr->edfparam[channel].smp_per_record) * bytes_per_smpl); fseeko(file, offset, SEEK_SET); sample_pntr = hdr->edfparam[channel].sample_pntr; smp_per_record = hdr->edfparam[channel].smp_per_record; jump = hdr->recordsize - (smp_per_record * bytes_per_smpl); if(hdr->edf) { for(i=0; ibdf) { for(i=0; iedfparam[channel].sample_pntr = sample_pntr; return n; } int edf_get_annotation(int handle, int n, struct edf_annotation_struct *annot) { memset(annot, 0, sizeof(struct edf_annotation_struct)); if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(hdrlist[handle]->writemode) { return -1; } if(n<0) { return -1; } if(n>=hdrlist[handle]->annots_in_file) { return -1; } annot->onset = (annotationslist[handle] + n)->onset; edflib_strlcpy(annot->duration, (annotationslist[handle] + n)->duration, 16); edflib_strlcpy(annot->annotation, (annotationslist[handle] + n)->annotation, EDFLIB_MAX_ANNOTATION_LEN + 1); return 0; } static struct edfhdrblock * edflib_check_edf_file(FILE *inputfile, int *edf_error) { int i, j, p, r=0, n, dotposition, error; char *edf_hdr, scratchpad[128], scratchpad2[64]; struct edfhdrblock *edfhdr; /***************** check header ******************************/ edf_hdr = (char *)calloc(1, 256); if(edf_hdr==NULL) { *edf_error = EDFLIB_MALLOC_ERROR; return NULL; } edfhdr = (struct edfhdrblock *)calloc(1, sizeof(struct edfhdrblock)); if(edfhdr==NULL) { free(edf_hdr); *edf_error = EDFLIB_MALLOC_ERROR; return NULL; } rewind(inputfile); if(fread(edf_hdr, 256, 1, inputfile)!=1) { *edf_error = EDFLIB_FILE_READ_ERROR; free(edf_hdr); free(edfhdr); return NULL; } /**************************** VERSION ***************************************/ strncpy(scratchpad, edf_hdr, 8); scratchpad[8] = 0; if(((signed char *)scratchpad)[0]==-1) /* BDF-file */ { for(i=1; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(strcmp(scratchpad + 1, "BIOSEMI")) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->bdf = 1; } else /* EDF-file */ { for(i=0; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(strcmp(scratchpad, "0 ")) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->edf = 1; } strncpy(edfhdr->version, edf_hdr, 8); edfhdr->version[8] = 0; if(edfhdr->bdf) edfhdr->version[0] = '.'; /********************* PATIENTNAME *********************************************/ strncpy(scratchpad, edf_hdr + 8, 80); scratchpad[80] = 0; for(i=0; i<80; i++) { if((((unsigned char *)scratchpad)[i]<32)||(((unsigned char *)scratchpad)[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } strncpy(edfhdr->patient, edf_hdr + 8, 80); edfhdr->patient[80] = 0; /********************* RECORDING *********************************************/ strncpy(scratchpad, edf_hdr + 88, 80); scratchpad[80] = 0; for(i=0; i<80; i++) { if((((unsigned char *)scratchpad)[i]<32)||(((unsigned char *)scratchpad)[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } strncpy(edfhdr->recording, edf_hdr + 88, 80); edfhdr->recording[80] = 0; /********************* STARTDATE *********************************************/ strncpy(scratchpad, edf_hdr + 168, 8); scratchpad[8] = 0; for(i=0; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } error = 0; if((edf_hdr[170]!='.')||(edf_hdr[173]!='.')) error = 1; if((edf_hdr[168]<48)||(edf_hdr[168]>57)) error = 1; if((edf_hdr[169]<48)||(edf_hdr[169]>57)) error = 1; if((edf_hdr[171]<48)||(edf_hdr[171]>57)) error = 1; if((edf_hdr[172]<48)||(edf_hdr[172]>57)) error = 1; if((edf_hdr[174]<48)||(edf_hdr[174]>57)) error = 1; if((edf_hdr[175]<48)||(edf_hdr[175]>57)) error = 1; strncpy(scratchpad, edf_hdr + 168, 8); if(error) { scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } scratchpad[2] = 0; scratchpad[5] = 0; scratchpad[8] = 0; if((edflib_atof_nonlocalized(scratchpad)<1)||(edflib_atof_nonlocalized(scratchpad)>31)) { strncpy(scratchpad, edf_hdr + 168, 8); scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } if((edflib_atof_nonlocalized(scratchpad+3)<1)||(edflib_atof_nonlocalized(scratchpad+3)>12)) { strncpy(scratchpad, edf_hdr + 168, 8); scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->startdate_day = edflib_atof_nonlocalized(scratchpad); edfhdr->startdate_month = edflib_atof_nonlocalized(scratchpad + 3); edfhdr->startdate_year = edflib_atof_nonlocalized(scratchpad + 6); if(edfhdr->startdate_year>84) { edfhdr->startdate_year += 1900; } else { edfhdr->startdate_year += 2000; } /********************* STARTTIME *********************************************/ strncpy(scratchpad, edf_hdr + 176, 8); scratchpad[8] = 0; for(i=0; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } error = 0; if((edf_hdr[178]!='.')||(edf_hdr[181]!='.')) error = 1; if((edf_hdr[176]<48)||(edf_hdr[176]>57)) error = 1; if((edf_hdr[177]<48)||(edf_hdr[177]>57)) error = 1; if((edf_hdr[179]<48)||(edf_hdr[179]>57)) error = 1; if((edf_hdr[180]<48)||(edf_hdr[180]>57)) error = 1; if((edf_hdr[182]<48)||(edf_hdr[182]>57)) error = 1; if((edf_hdr[183]<48)||(edf_hdr[183]>57)) error = 1; strncpy(scratchpad, edf_hdr + 176, 8); if(error) { strncpy(scratchpad, edf_hdr + 176, 8); scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } scratchpad[2] = 0; scratchpad[5] = 0; scratchpad[8] = 0; if(edflib_atof_nonlocalized(scratchpad)>23) { strncpy(scratchpad, edf_hdr + 176, 8); scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } if(edflib_atof_nonlocalized(scratchpad+3)>59) { strncpy(scratchpad, edf_hdr + 176, 8); scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } if(edflib_atof_nonlocalized(scratchpad+6)>59) { strncpy(scratchpad, edf_hdr + 176, 8); scratchpad[8] = 0; *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->starttime_hour = edflib_atof_nonlocalized(scratchpad); edfhdr->starttime_minute = edflib_atof_nonlocalized(scratchpad + 3); edfhdr->starttime_second = edflib_atof_nonlocalized(scratchpad + 6); edfhdr->l_starttime = 3600 * edflib_atof_nonlocalized(scratchpad); edfhdr->l_starttime += 60 * edflib_atof_nonlocalized(scratchpad + 3); edfhdr->l_starttime += edflib_atof_nonlocalized(scratchpad + 6); edfhdr->l_starttime *= EDFLIB_TIME_DIMENSION; /***************** NUMBER OF SIGNALS IN HEADER *******************************/ strncpy(scratchpad, edf_hdr + 252, 4); scratchpad[4] = 0; for(i=0; i<4; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(edflib_is_integer_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->edfsignals = edflib_atof_nonlocalized(scratchpad); if(edfhdr->edfsignals<1) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } if(edfhdr->edfsignals>EDFLIB_MAXSIGNALS) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } /***************** NUMBER OF BYTES IN HEADER *******************************/ strncpy(scratchpad, edf_hdr + 184, 8); scratchpad[8] = 0; for(i=0; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(edflib_is_integer_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } n = edflib_atof_nonlocalized(scratchpad); if((edfhdr->edfsignals * 256 + 256)!=n) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } /********************* RESERVED FIELD *************************************/ edfhdr->edfplus = 0; edfhdr->discontinuous = 0; strncpy(scratchpad, edf_hdr + 192, 44); scratchpad[44] = 0; for(i=0; i<44; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(edfhdr->edf) { if(!strncmp(scratchpad, "EDF+C", 5)) { edfhdr->edfplus = 1; } if(!strncmp(scratchpad, "EDF+D", 5)) { edfhdr->edfplus = 1; edfhdr->discontinuous = 1; } } if(edfhdr->bdf) { if(!strncmp(scratchpad, "BDF+C", 5)) { edfhdr->bdfplus = 1; } if(!strncmp(scratchpad, "BDF+D", 5)) { edfhdr->bdfplus = 1; edfhdr->discontinuous = 1; } } strncpy(edfhdr->reserved, edf_hdr + 192, 44); edfhdr->reserved[44] = 0; /********************* NUMBER OF DATARECORDS *************************************/ strncpy(scratchpad, edf_hdr + 236, 8); scratchpad[8] = 0; for(i=0; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(edflib_is_integer_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->datarecords = edflib_atof_nonlocalized(scratchpad); if(edfhdr->datarecords<1) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } /********************* DATARECORD DURATION *************************************/ strncpy(scratchpad, edf_hdr + 244, 8); scratchpad[8] = 0; for(i=0; i<8; i++) { if((scratchpad[i]<32)||(scratchpad[i]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } } if(edflib_is_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->data_record_duration = edflib_atof_nonlocalized(scratchpad); if(edfhdr->data_record_duration < -0.000001) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->long_data_record_duration = edflib_get_long_duration(scratchpad); free(edf_hdr); /********************* START WITH THE SIGNALS IN THE HEADER *********************/ edf_hdr = (char *)calloc(1, (edfhdr->edfsignals + 1) * 256); if(edf_hdr==NULL) { *edf_error = EDFLIB_MALLOC_ERROR; free(edfhdr); return NULL; } rewind(inputfile); if(fread(edf_hdr, (edfhdr->edfsignals + 1) * 256, 1, inputfile)!=1) { *edf_error = EDFLIB_FILE_READ_ERROR; free(edf_hdr); free(edfhdr); return NULL; } edfhdr->edfparam = (struct edfparamblock *)calloc(1, sizeof(struct edfparamblock) * edfhdr->edfsignals); if(edfhdr->edfparam==NULL) { *edf_error = EDFLIB_MALLOC_ERROR; free(edf_hdr); free(edfhdr); return NULL; } /**************************** LABELS *************************************/ edfhdr->nr_annot_chns = 0; for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (i * 16), 16); for(j=0; j<16; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edfhdr->edfplus) { if(!strncmp(scratchpad, "EDF Annotations ", 16)) { edfhdr->annot_ch[edfhdr->nr_annot_chns] = i; edfhdr->nr_annot_chns++; edfhdr->edfparam[i].annotation = 1; } } if(edfhdr->bdfplus) { if(!strncmp(scratchpad, "BDF Annotations ", 16)) { edfhdr->annot_ch[edfhdr->nr_annot_chns] = i; edfhdr->nr_annot_chns++; edfhdr->edfparam[i].annotation = 1; } } strncpy(edfhdr->edfparam[i].label, edf_hdr + 256 + (i * 16), 16); edfhdr->edfparam[i].label[16] = 0; } if(edfhdr->edfplus&&(!edfhdr->nr_annot_chns)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } if(edfhdr->bdfplus&&(!edfhdr->nr_annot_chns)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } if((edfhdr->edfsignals!=edfhdr->nr_annot_chns)||((!edfhdr->edfplus)&&(!edfhdr->bdfplus))) { if(edfhdr->data_record_duration<0.0000001) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } /**************************** TRANSDUCER TYPES *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 16) + (i * 80), 80); for(j=0; j<80; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } strncpy(edfhdr->edfparam[i].transducer, edf_hdr + 256 + (edfhdr->edfsignals * 16) + (i * 80), 80); edfhdr->edfparam[i].transducer[80] = 0; if((edfhdr->edfplus) || (edfhdr->bdfplus)) { if(edfhdr->edfparam[i].annotation) { for(j=0; j<80; j++) { if(edfhdr->edfparam[i].transducer[j]!=' ') { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } } } } /**************************** PHYSICAL DIMENSIONS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 96) + (i * 8), 8); for(j=0; j<8; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } strncpy(edfhdr->edfparam[i].physdimension, edf_hdr + 256 + (edfhdr->edfsignals * 96) + (i * 8), 8); edfhdr->edfparam[i].physdimension[8] = 0; } /**************************** PHYSICAL MINIMUMS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 104) + (i * 8), 8); scratchpad[8] = 0; for(j=0; j<8; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edflib_is_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } edfhdr->edfparam[i].phys_min = edflib_atof_nonlocalized(scratchpad); } /**************************** PHYSICAL MAXIMUMS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 112) + (i * 8), 8); scratchpad[8] = 0; for(j=0; j<8; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edflib_is_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } edfhdr->edfparam[i].phys_max = edflib_atof_nonlocalized(scratchpad); if(edfhdr->edfparam[i].phys_max==edfhdr->edfparam[i].phys_min) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } /**************************** DIGITAL MINIMUMS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 120) + (i * 8), 8); scratchpad[8] = 0; for(j=0; j<8; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edflib_is_integer_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } n = edflib_atof_nonlocalized(scratchpad); if(edfhdr->edfplus) { if(edfhdr->edfparam[i].annotation) { if(n!=-32768) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } } if(edfhdr->bdfplus) { if(edfhdr->edfparam[i].annotation) { if(n!=-8388608) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } } if(edfhdr->edf) { if((n>32767)||(n<-32768)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edfhdr->bdf) { if((n>8388607)||(n<-8388608)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } edfhdr->edfparam[i].dig_min = n; } /**************************** DIGITAL MAXIMUMS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 128) + (i * 8), 8); scratchpad[8] = 0; for(j=0; j<8; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edflib_is_integer_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } n = edflib_atof_nonlocalized(scratchpad); if(edfhdr->edfplus) { if(edfhdr->edfparam[i].annotation) { if(n!=32767) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } } if(edfhdr->bdfplus) { if(edfhdr->edfparam[i].annotation) { if(n!=8388607) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } } if(edfhdr->edf) { if((n>32767)||(n<-32768)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } else { if((n>8388607)||(n<-8388608)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } edfhdr->edfparam[i].dig_max = n; if(edfhdr->edfparam[i].dig_max<(edfhdr->edfparam[i].dig_min + 1)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } /**************************** PREFILTER FIELDS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 136) + (i * 80), 80); for(j=0; j<80; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } strncpy(edfhdr->edfparam[i].prefilter, edf_hdr + 256 + (edfhdr->edfsignals * 136) + (i * 80), 80); edfhdr->edfparam[i].prefilter[80] = 0; if((edfhdr->edfplus) || (edfhdr->bdfplus)) { if(edfhdr->edfparam[i].annotation) { for(j=0; j<80; j++) { if(edfhdr->edfparam[i].prefilter[j]!=' ') { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } } } } /*********************** NR OF SAMPLES IN EACH DATARECORD ********************/ edfhdr->recordsize = 0; for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 216) + (i * 8), 8); scratchpad[8] = 0; for(j=0; j<8; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } if(edflib_is_integer_number(scratchpad)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } n = edflib_atof_nonlocalized(scratchpad); if(n<1) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } edfhdr->edfparam[i].smp_per_record = n; edfhdr->recordsize += n; } if(edfhdr->bdf) { edfhdr->recordsize *= 3; if(edfhdr->recordsize > (15 * 1024 * 1024)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } else { edfhdr->recordsize *= 2; if(edfhdr->recordsize > (10 * 1024 * 1024)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } /**************************** RESERVED FIELDS *************************************/ for(i=0; iedfsignals; i++) { strncpy(scratchpad, edf_hdr + 256 + (edfhdr->edfsignals * 224) + (i * 32), 32); for(j=0; j<32; j++) { if((scratchpad[j]<32)||(scratchpad[j]>126)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } strncpy(edfhdr->edfparam[i].reserved, edf_hdr + 256 + (edfhdr->edfsignals * 224) + (i * 32), 32); edfhdr->edfparam[i].reserved[32] = 0; } /********************* EDF+ PATIENTNAME *********************************************/ if(edfhdr->edfplus || edfhdr->bdfplus) { error = 0; dotposition = 0; strncpy(scratchpad, edf_hdr + 8, 80); scratchpad[80] = 0; for(i=0; i<80; i++) { if(scratchpad[i]==' ') { dotposition = i; break; } } dotposition++; if((dotposition>73)||(dotposition<2)) error = 1; if(scratchpad[dotposition + 2]!='X') { if(dotposition>65) error = 1; } if((scratchpad[dotposition]!='M')&&(scratchpad[dotposition]!='F')&&(scratchpad[dotposition]!='X')) error = 1; dotposition++; if(scratchpad[dotposition]!=' ') error = 1; if(scratchpad[dotposition + 1]=='X') { if(scratchpad[dotposition + 2]!=' ') error = 1; if(scratchpad[dotposition + 3]==' ') error = 1; } else { if(scratchpad[dotposition + 12]!=' ') error = 1; if(scratchpad[dotposition + 13]==' ') error = 1; dotposition++; strncpy(scratchpad2, scratchpad + dotposition, 11); scratchpad2[11] = 0; if((scratchpad2[2]!='-')||(scratchpad2[6]!='-')) error = 1; scratchpad2[2] = 0; scratchpad2[6] = 0; if((scratchpad2[0]<48)||(scratchpad2[0]>57)) error = 1; if((scratchpad2[1]<48)||(scratchpad2[1]>57)) error = 1; if((scratchpad2[7]<48)||(scratchpad2[7]>57)) error = 1; if((scratchpad2[8]<48)||(scratchpad2[8]>57)) error = 1; if((scratchpad2[9]<48)||(scratchpad2[9]>57)) error = 1; if((scratchpad2[10]<48)||(scratchpad2[10]>57)) error = 1; if((edflib_atof_nonlocalized(scratchpad2)<1)||(edflib_atof_nonlocalized(scratchpad2)>31)) error = 1; if(strcmp(scratchpad2 + 3, "JAN")) if(strcmp(scratchpad2 + 3, "FEB")) if(strcmp(scratchpad2 + 3, "MAR")) if(strcmp(scratchpad2 + 3, "APR")) if(strcmp(scratchpad2 + 3, "MAY")) if(strcmp(scratchpad2 + 3, "JUN")) if(strcmp(scratchpad2 + 3, "JUL")) if(strcmp(scratchpad2 + 3, "AUG")) if(strcmp(scratchpad2 + 3, "SEP")) if(strcmp(scratchpad2 + 3, "OCT")) if(strcmp(scratchpad2 + 3, "NOV")) if(strcmp(scratchpad2 + 3, "DEC")) error = 1; } if(error) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } p = 0; if(edfhdr->patient[p]=='X') { edfhdr->plus_patientcode[0] = 0; p += 2; } else { for(i=0; i<(80-p); i++) { if(edfhdr->patient[i+p]==' ') { break; } edfhdr->plus_patientcode[i] = edfhdr->patient[i+p]; if(edfhdr->plus_patientcode[i]=='_') edfhdr->plus_patientcode[i] = ' '; } edfhdr->plus_patientcode[i] = 0; p += i + 1; } if(edfhdr->patient[p]=='M') { edflib_strlcpy(edfhdr->plus_gender, "Male", 16); } if(edfhdr->patient[p]=='F') { edflib_strlcpy(edfhdr->plus_gender, "Female", 16); } if(edfhdr->patient[p]=='X') { edfhdr->plus_gender[0] = 0; } for(i=0; i<(80-p);i++) { if(edfhdr->patient[i+p]==' ') { break; } } p += i + 1; if(edfhdr->patient[p]=='X') { edfhdr->plus_birthdate[0] = 0; p += 2; } else { for(i=0; i<(80-p); i++) { if(edfhdr->patient[i+p]==' ') { break; } edfhdr->plus_birthdate[i] = edfhdr->patient[i+p]; } edfhdr->plus_birthdate[2] = ' '; edfhdr->plus_birthdate[3] += 32; edfhdr->plus_birthdate[4] += 32; edfhdr->plus_birthdate[5] += 32; edfhdr->plus_birthdate[6] = ' '; edfhdr->plus_birthdate[11] = 0; p += i + 1; } for(i=0; i<(80-p);i++) { if(edfhdr->patient[i+p]==' ') { break; } edfhdr->plus_patient_name[i] = edfhdr->patient[i+p]; if(edfhdr->plus_patient_name[i]=='_') edfhdr->plus_patient_name[i] = ' '; } edfhdr->plus_patient_name[i] = 0; p += i + 1; for(i=0; i<(80-p);i++) { edfhdr->plus_patient_additional[i] = edfhdr->patient[i+p]; } edfhdr->plus_patient_additional[i] = 0; p += i + 1; } /********************* EDF+ RECORDINGFIELD *********************************************/ if(edfhdr->edfplus || edfhdr->bdfplus) { error = 0; strncpy(scratchpad, edf_hdr + 88, 80); scratchpad[80] = 0; if(strncmp(scratchpad, "Startdate ", 10)) error = 1; if(scratchpad[10]=='X') { if(scratchpad[11]!=' ') error = 1; if(scratchpad[12]==' ') error = 1; p = 12; } else { if(scratchpad[21]!=' ') error = 1; if(scratchpad[22]==' ') error = 1; p = 22; strncpy(scratchpad2, scratchpad + 10, 11); scratchpad2[11] = 0; if((scratchpad2[2]!='-')||(scratchpad2[6]!='-')) error = 1; scratchpad2[2] = 0; scratchpad2[6] = 0; if((scratchpad2[0]<48)||(scratchpad2[0]>57)) error = 1; if((scratchpad2[1]<48)||(scratchpad2[1]>57)) error = 1; if((scratchpad2[7]<48)||(scratchpad2[7]>57)) error = 1; if((scratchpad2[8]<48)||(scratchpad2[8]>57)) error = 1; if((scratchpad2[9]<48)||(scratchpad2[9]>57)) error = 1; if((scratchpad2[10]<48)||(scratchpad2[10]>57)) error = 1; if((edflib_atof_nonlocalized(scratchpad2)<1)||(edflib_atof_nonlocalized(scratchpad2)>31)) error = 1; r = 0; if(!strcmp(scratchpad2 + 3, "JAN")) r = 1; else if(!strcmp(scratchpad2 + 3, "FEB")) r = 2; else if(!strcmp(scratchpad2 + 3, "MAR")) r = 3; else if(!strcmp(scratchpad2 + 3, "APR")) r = 4; else if(!strcmp(scratchpad2 + 3, "MAY")) r = 5; else if(!strcmp(scratchpad2 + 3, "JUN")) r = 6; else if(!strcmp(scratchpad2 + 3, "JUL")) r = 7; else if(!strcmp(scratchpad2 + 3, "AUG")) r = 8; else if(!strcmp(scratchpad2 + 3, "SEP")) r = 9; else if(!strcmp(scratchpad2 + 3, "OCT")) r = 10; else if(!strcmp(scratchpad2 + 3, "NOV")) r = 11; else if(!strcmp(scratchpad2 + 3, "DEC")) r = 12; else error = 1; } n = 0; for(i=p; i<80; i++) { if(i>78) { error = 1; break; } if(scratchpad[i]==' ') { n++; if(scratchpad[i + 1]==' ') { error = 1; break; } } if(n>1) break; } if(error) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } if(edf_hdr[98]!='X') { error = 0; strncpy(scratchpad, edf_hdr + 168, 8); scratchpad[2] = 0; scratchpad[5] = 0; scratchpad[8] = 0; if(edflib_atof_nonlocalized(scratchpad)!=edflib_atof_nonlocalized(scratchpad2)) error = 1; if(edflib_atof_nonlocalized(scratchpad+3)!=r) error = 1; if(edflib_atof_nonlocalized(scratchpad+6)!=edflib_atof_nonlocalized(scratchpad2+9)) error = 1; if(error) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } edfhdr->startdate_year = edflib_atof_nonlocalized(scratchpad2 + 7); if(edfhdr->startdate_year<1970) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } } p = 10; for(i=0; i<(80-p); i++) { if(edfhdr->recording[i+p]==' ') { break; } edfhdr->plus_startdate[i] = edfhdr->recording[i+p]; } edfhdr->plus_startdate[2] = ' '; edfhdr->plus_startdate[3] += 32; edfhdr->plus_startdate[4] += 32; edfhdr->plus_startdate[5] += 32; edfhdr->plus_startdate[6] = ' '; edfhdr->plus_startdate[11] = 0; p += i + 1; if(edfhdr->recording[p]=='X') { edfhdr->plus_admincode[0] = 0; p += 2; } else { for(i=0; i<(80-p); i++) { if(edfhdr->recording[i+p]==' ') { break; } edfhdr->plus_admincode[i] = edfhdr->recording[i+p]; if(edfhdr->plus_admincode[i]=='_') edfhdr->plus_admincode[i] = ' '; } edfhdr->plus_admincode[i] = 0; p += i + 1; } if(edfhdr->recording[p]=='X') { edfhdr->plus_technician[0] = 0; p += 2; } else { for(i=0; i<(80-p); i++) { if(edfhdr->recording[i+p]==' ') { break; } edfhdr->plus_technician[i] = edfhdr->recording[i+p]; if(edfhdr->plus_technician[i]=='_') edfhdr->plus_technician[i] = ' '; } edfhdr->plus_technician[i] = 0; p += i + 1; } if(edfhdr->recording[p]=='X') { edfhdr->plus_equipment[0] = 0; p += 2; } else { for(i=0; i<(80-p); i++) { if(edfhdr->recording[i+p]==' ') { break; } edfhdr->plus_equipment[i] = edfhdr->recording[i+p]; if(edfhdr->plus_equipment[i]=='_') edfhdr->plus_equipment[i] = ' '; } edfhdr->plus_equipment[i] = 0; p += i + 1; } for(i=0; i<(80-p);i++) { edfhdr->plus_recording_additional[i] = edfhdr->recording[i+p]; } edfhdr->plus_recording_additional[i] = 0; p += i + 1; } /********************* FILESIZE *********************************************/ edfhdr->hdrsize = edfhdr->edfsignals * 256 + 256; fseeko(inputfile, 0LL, SEEK_END); if(ftello(inputfile)!=(edfhdr->recordsize * edfhdr->datarecords + edfhdr->hdrsize)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); free(edfhdr->edfparam); free(edfhdr); return NULL; } n = 0; for(i=0; iedfsignals; i++) { edfhdr->edfparam[i].buf_offset = n; if(edfhdr->bdf) n += edfhdr->edfparam[i].smp_per_record * 3; else n += edfhdr->edfparam[i].smp_per_record * 2; edfhdr->edfparam[i].bitvalue = (edfhdr->edfparam[i].phys_max - edfhdr->edfparam[i].phys_min) / (edfhdr->edfparam[i].dig_max - edfhdr->edfparam[i].dig_min); edfhdr->edfparam[i].offset = edfhdr->edfparam[i].phys_max / edfhdr->edfparam[i].bitvalue - edfhdr->edfparam[i].dig_max; } edfhdr->file_hdl = inputfile; free(edf_hdr); return edfhdr; } static int edflib_is_integer_number(char *str) { int i=0, l, hasspace = 0, hassign=0, digit=0; l = strlen(str); if(!l) return 1; if((str[0]=='+')||(str[0]=='-')) { hassign++; i++; } for(; i57)) { return 1; } else { if(hasspace) { return 1; } digit++; } } } if(digit) return 0; else return 1; } static int edflib_is_number(char *str) { int i=0, l, hasspace = 0, hassign=0, digit=0, hasdot=0, hasexp=0; l = strlen(str); if(!l) return 1; if((str[0]=='+')||(str[0]=='-')) { hassign++; i++; } for(; i57))&&str[i]!='.') { return 1; } else { if(hasspace) { return 1; } if(str[i]=='.') { if(hasdot) return 1; hasdot++; } else { digit++; } } } } if(hasexp) { if(++i==l) { return 1; } if((str[i]=='+')||(str[i]=='-')) { hassign++; i++; } for(; i57)) { return 1; } else { if(hasspace) { return 1; } digit++; } } } } if(digit) return 0; else return 1; } static long long edflib_get_long_duration(char *str) { int i, len=8, hasdot=0, dotposition=0; long long value=0, radix; if((str[0] == '+') || (str[0] == '-')) { for(i=0; i<7; i++) { str[i] = str[i+1]; } str[7] = ' '; } for(i=0; i<8; i++) { if(str[i]==' ') { len = i; break; } } for(i=0; i=0; i--) { value += ((long long)(str[i] - 48)) * radix; radix *= 10; } radix = EDFLIB_TIME_DIMENSION / 10; for(i=dotposition+1; i=0; i--) { value += ((long long)(str[i] - 48)) * radix; radix *= 10; } } return value; } int edflib_version(void) { return EDFLIB_VERSION; } static int edflib_get_annotations(struct edfhdrblock *edfhdr, int hdl, int read_annotations) { int i, j, k, p, r=0, n, edfsignals, datarecords, recordsize, discontinuous, *annot_ch, nr_annot_chns, max, onset, duration, duration_start, zero, max_tal_ln, error, annots_in_record, annots_in_tal, samplesize=2; char *scratchpad, *cnv_buf, *time_in_txt, *duration_in_txt; long long data_record_duration, elapsedtime, time_tmp=0; FILE *inputfile; struct edfparamblock *edfparam; struct edf_annotationblock *new_annotation=NULL, *malloc_list; inputfile = edfhdr->file_hdl; edfsignals = edfhdr->edfsignals; recordsize = edfhdr->recordsize; edfparam = edfhdr->edfparam; nr_annot_chns = edfhdr->nr_annot_chns; datarecords = edfhdr->datarecords; data_record_duration = edfhdr->long_data_record_duration; discontinuous = edfhdr->discontinuous; annot_ch = edfhdr->annot_ch; if(edfhdr->edfplus) { samplesize = 2; } if(edfhdr->bdfplus) { samplesize = 3; } cnv_buf = (char *)calloc(1, recordsize); if(cnv_buf==NULL) { return 1; } max_tal_ln = 0; for(i=0; i=EDFLIB_TIME_DIMENSION) { error = 2; goto END; } else { edfhdr->starttime_offset = time_tmp; } } elapsedtime = time_tmp; error = 0; break; } } } } for(k=0; k1) { error = 34; goto END; } zero = 0; if((scratchpad[n]==20)||(scratchpad[n]==21)) { if(scratchpad[n]==21) { if(duration||duration_start||onset||annots_in_tal) { /* it's not allowed to have multiple duration fields */ error = 35; /* in one TAL or to have a duration field which is */ goto END; /* not immediately behind the onsetfield */ } duration_start = 1; } if((scratchpad[n]==20)&&onset&&(!duration_start)) { if(r||annots_in_record) { if(n >= 0) { if(edfhdr->annots_in_file >= edfhdr->annotlist_sz) { malloc_list = (struct edf_annotationblock *)realloc(annotationslist[hdl], sizeof(struct edf_annotationblock) * (edfhdr->annotlist_sz + EDFLIB_ANNOT_MEMBLOCKSZ)); if(malloc_list==NULL) { free(cnv_buf); free(scratchpad); free(time_in_txt); free(duration_in_txt); return -1; } annotationslist[hdl] = malloc_list; edfhdr->annotlist_sz += EDFLIB_ANNOT_MEMBLOCKSZ; } new_annotation = annotationslist[hdl] + edfhdr->annots_in_file; new_annotation->annotation[0] = 0; if(duration) edflib_strlcpy(new_annotation->duration, duration_in_txt, 16); else new_annotation->duration[0] = 0; for(j=0; jannotation[j] = scratchpad[j]; } new_annotation->annotation[j] = 0; new_annotation->onset = edflib_get_long_time(time_in_txt); edfhdr->annots_in_file++; if(read_annotations==EDFLIB_READ_ANNOTATIONS) { if(!(strncmp(new_annotation->annotation, "Recording ends", 14))) { if(nr_annot_chns==1) { goto END; } } } } } annots_in_tal++; annots_in_record++; n = 0; continue; } if(!onset) { scratchpad[n] = 0; if(edflib_is_onset_number(scratchpad)) { error = 36; goto END; } onset = 1; n = 0; edflib_strlcpy(time_in_txt, scratchpad, max_tal_ln + 3); continue; } if(duration_start) { scratchpad[n] = 0; if(edflib_is_duration_number(scratchpad)) { error = 37; goto END; } for(j=0; j126)) { duration_in_txt[j] = '.'; } } duration_in_txt[j] = 0; duration = 1; duration_start = 0; n = 0; continue; } } n++; } END: /****************** end ************************/ if(error) { free(cnv_buf); free(scratchpad); free(time_in_txt); free(duration_in_txt); return 9; } } } free(cnv_buf); free(scratchpad); free(time_in_txt); free(duration_in_txt); return 0; } static int edflib_is_duration_number(char *str) { int i, l, hasdot = 0; l = strlen(str); if(!l) return 1; if((str[0] == '.')||(str[l-1] == '.')) return 1; for(i=0; i57)) return 1; } } return 0; } static int edflib_is_onset_number(char *str) { int i, l, hasdot = 0; l = strlen(str); if(l<2) return 1; if((str[0]!='+')&&(str[0]!='-')) return 1; if((str[1] == '.')||(str[l-1] == '.')) return 1; for(i=1; i57)) return 1; } } return 0; } static long long edflib_get_long_time(char *str) { int i, len, hasdot=0, dotposition=0, neg=0; long long value=0, radix; if(str[0] == '+') { str++; } else if(str[0] == '-') { neg = 1; str++; } len = strlen(str); for(i=0; i=0; i--) { value += ((long long)(str[i] - 48)) * radix; radix *= 10; } radix = EDFLIB_TIME_DIMENSION / 10; for(i=dotposition+1; i=0; i--) { value += ((long long)(str[i] - 48)) * radix; radix *= 10; } } if(neg) value = -value; return value; } static void edflib_latin1_to_ascii(char *str, int len) { int i, value; for(i=0; i31)&&(value<127)) { continue; } switch(value) { case 128 : str[i] = 'E'; break; case 130 : str[i] = ','; break; case 131 : str[i] = 'F'; break; case 132 : str[i] = '\"'; break; case 133 : str[i] = '.'; break; case 134 : str[i] = '+'; break; case 135 : str[i] = '+'; break; case 136 : str[i] = '^'; break; case 137 : str[i] = 'm'; break; case 138 : str[i] = 'S'; break; case 139 : str[i] = '<'; break; case 140 : str[i] = 'E'; break; case 142 : str[i] = 'Z'; break; case 145 : str[i] = '`'; break; case 146 : str[i] = '\''; break; case 147 : str[i] = '\"'; break; case 148 : str[i] = '\"'; break; case 149 : str[i] = '.'; break; case 150 : str[i] = '-'; break; case 151 : str[i] = '-'; break; case 152 : str[i] = '~'; break; case 154 : str[i] = 's'; break; case 155 : str[i] = '>'; break; case 156 : str[i] = 'e'; break; case 158 : str[i] = 'z'; break; case 159 : str[i] = 'Y'; break; case 171 : str[i] = '<'; break; case 180 : str[i] = '\''; break; case 181 : str[i] = 'u'; break; case 187 : str[i] = '>'; break; case 191 : str[i] = '\?'; break; case 192 : str[i] = 'A'; break; case 193 : str[i] = 'A'; break; case 194 : str[i] = 'A'; break; case 195 : str[i] = 'A'; break; case 196 : str[i] = 'A'; break; case 197 : str[i] = 'A'; break; case 198 : str[i] = 'E'; break; case 199 : str[i] = 'C'; break; case 200 : str[i] = 'E'; break; case 201 : str[i] = 'E'; break; case 202 : str[i] = 'E'; break; case 203 : str[i] = 'E'; break; case 204 : str[i] = 'I'; break; case 205 : str[i] = 'I'; break; case 206 : str[i] = 'I'; break; case 207 : str[i] = 'I'; break; case 208 : str[i] = 'D'; break; case 209 : str[i] = 'N'; break; case 210 : str[i] = 'O'; break; case 211 : str[i] = 'O'; break; case 212 : str[i] = 'O'; break; case 213 : str[i] = 'O'; break; case 214 : str[i] = 'O'; break; case 215 : str[i] = 'x'; break; case 216 : str[i] = 'O'; break; case 217 : str[i] = 'U'; break; case 218 : str[i] = 'U'; break; case 219 : str[i] = 'U'; break; case 220 : str[i] = 'U'; break; case 221 : str[i] = 'Y'; break; case 222 : str[i] = 'I'; break; case 223 : str[i] = 's'; break; case 224 : str[i] = 'a'; break; case 225 : str[i] = 'a'; break; case 226 : str[i] = 'a'; break; case 227 : str[i] = 'a'; break; case 228 : str[i] = 'a'; break; case 229 : str[i] = 'a'; break; case 230 : str[i] = 'e'; break; case 231 : str[i] = 'c'; break; case 232 : str[i] = 'e'; break; case 233 : str[i] = 'e'; break; case 234 : str[i] = 'e'; break; case 235 : str[i] = 'e'; break; case 236 : str[i] = 'i'; break; case 237 : str[i] = 'i'; break; case 238 : str[i] = 'i'; break; case 239 : str[i] = 'i'; break; case 240 : str[i] = 'd'; break; case 241 : str[i] = 'n'; break; case 242 : str[i] = 'o'; break; case 243 : str[i] = 'o'; break; case 244 : str[i] = 'o'; break; case 245 : str[i] = 'o'; break; case 246 : str[i] = 'o'; break; case 247 : str[i] = '-'; break; case 248 : str[i] = '0'; break; case 249 : str[i] = 'u'; break; case 250 : str[i] = 'u'; break; case 251 : str[i] = 'u'; break; case 252 : str[i] = 'u'; break; case 253 : str[i] = 'y'; break; case 254 : str[i] = 't'; break; case 255 : str[i] = 'y'; break; default : str[i] = ' '; break; } } } static void edflib_latin12utf8(char *latin1_str, int len) { int i, j; unsigned char *str, tmp_str[512]; str = (unsigned char *)latin1_str; j = 0; for(i=0; i126)&&(str[i]<160)) tmp_str[j] = '.'; if(str[i]>159) { if((len-j)<2) { tmp_str[j] = ' '; } else { tmp_str[j] = 192 + (str[i]>>6); j++; tmp_str[j] = 128 + (str[i]&63); } } j++; if(j>=len) break; } for(i=0; i=EDFLIB_MAXFILES) { return EDFLIB_MAXFILES_REACHED; } for(i=0; ipath))) { return EDFLIB_FILE_ALREADY_OPENED; } } } if(number_of_signals<0) { return EDFLIB_NUMBER_OF_SIGNALS_INVALID; } if(number_of_signals>EDFLIB_MAXSIGNALS) { return EDFLIB_NUMBER_OF_SIGNALS_INVALID; } hdr = (struct edfhdrblock *)calloc(1, sizeof(struct edfhdrblock)); if(hdr==NULL) { return EDFLIB_MALLOC_ERROR; } hdr->edfparam = (struct edfparamblock *)calloc(1, sizeof(struct edfparamblock) * number_of_signals); if(hdr->edfparam==NULL) { free(hdr); return EDFLIB_MALLOC_ERROR; } hdr->writemode = 1; hdr->edfsignals = number_of_signals; handle = -1; for(i=0; iedfparam); free(hdr); return EDFLIB_MAXFILES_REACHED; } write_annotationslist[handle] = NULL; hdr->annotlist_sz = 0; hdr->annots_in_file = 0; file = fopeno(path, "wb"); if(file==NULL) { free(hdr->edfparam); hdr->edfparam = NULL; free(hdr); hdr = NULL; hdrlist[handle] = NULL; return EDFLIB_NO_SUCH_FILE_OR_DIRECTORY; } hdr->file_hdl = file; edflib_strlcpy(hdr->path, path, 1024); edf_files_open++; if(filetype==EDFLIB_FILETYPE_EDFPLUS) { hdr->edf = 1; hdr->edfplus = 1; } if(filetype==EDFLIB_FILETYPE_BDFPLUS) { hdr->bdf = 1; hdr->bdfplus = 1; } hdr->long_data_record_duration = EDFLIB_TIME_DIMENSION; hdr->data_record_duration = 1.0; hdr->nr_annot_chns = 1; return handle; } int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(samplefrequency<1) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } hdrlist[handle]->edfparam[edfsignal].smp_per_record = samplefrequency; return 0; } int edf_set_number_of_annotation_signals(int handle, int annot_signals) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } if((annot_signals < 1) || (annot_signals > EDFLIB_MAX_ANNOTATION_CHANNELS)) { return -1; } hdrlist[handle]->nr_annot_chns = annot_signals; return 0; } int edf_set_datarecord_duration(int handle, int duration) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } if((duration < 100) || (duration > 6000000)) { return -1; } hdrlist[handle]->long_data_record_duration = (long long)duration * 100LL; if(hdrlist[handle]->long_data_record_duration < (EDFLIB_TIME_DIMENSION * 10LL)) { hdrlist[handle]->long_data_record_duration /= 10LL; hdrlist[handle]->long_data_record_duration *= 10LL; } else { hdrlist[handle]->long_data_record_duration /= 100LL; hdrlist[handle]->long_data_record_duration *= 100LL; } hdrlist[handle]->data_record_duration = ((double)(hdrlist[handle]->long_data_record_duration)) / EDFLIB_TIME_DIMENSION; return 0; } int edf_set_micro_datarecord_duration(int handle, int duration) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } if((duration < 1) || (duration > 9999)) { return -1; } hdrlist[handle]->long_data_record_duration = (long long)duration * 10LL; hdrlist[handle]->data_record_duration = ((double)(hdrlist[handle]->long_data_record_duration)) / EDFLIB_TIME_DIMENSION; return 0; } int edfwrite_digital_short_samples(int handle, short *buf) { int i, error, sf, digmax, digmin, edfsignal, value; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } if(hdrlist[handle]->bdf == 1) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignal = hdr->signal_write_sequence_pos; if(!hdr->datarecords) { if(!edfsignal) { error = edflib_write_edf_header(hdr); if(error) { return error; } } } sf = hdr->edfparam[edfsignal].smp_per_record; digmax = hdr->edfparam[edfsignal].dig_max; digmin = hdr->edfparam[edfsignal].dig_min; if(hdr->edf) { if((digmax != 32767) || (digmin != -32768)) { for(i=0; idigmax) { buf[i] = digmax; } if(buf[i]wrbufsize < (sf * 3)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 3); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 3; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 3] = value & 0xff; hdr->wrbuf[i * 3 + 1] = (value >> 8) & 0xff; hdr->wrbuf[i * 3 + 2] = (value >> 16) & 0xff; } if(fwrite(hdr->wrbuf, sf * 3, 1, file) != 1) { return -1; } } hdr->signal_write_sequence_pos++; if(hdr->signal_write_sequence_pos == hdr->edfsignals) { hdr->signal_write_sequence_pos = 0; if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); } return 0; } int edfwrite_digital_samples(int handle, int *buf) { int i, error, sf, digmax, digmin, edfsignal, value; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignal = hdr->signal_write_sequence_pos; if(!hdr->datarecords) { if(!edfsignal) { error = edflib_write_edf_header(hdr); if(error) { return error; } } } sf = hdr->edfparam[edfsignal].smp_per_record; digmax = hdr->edfparam[edfsignal].dig_max; digmin = hdr->edfparam[edfsignal].dig_min; if(hdr->edf) { if(hdr->wrbufsize < (sf * 2)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 2); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 2; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 2] = value & 0xff; hdr->wrbuf[i * 2 + 1] = (value >> 8) & 0xff; } if(fwrite(hdr->wrbuf, sf * 2, 1, file) != 1) { return -1; } } else // BDF { if(hdr->wrbufsize < (sf * 3)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 3); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 3; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 3] = value & 0xff; hdr->wrbuf[i * 3 + 1] = (value >> 8) & 0xff; hdr->wrbuf[i * 3 + 2] = (value >> 16) & 0xff; } if(fwrite(hdr->wrbuf, sf * 3, 1, file) != 1) { return -1; } } hdr->signal_write_sequence_pos++; if(hdr->signal_write_sequence_pos == hdr->edfsignals) { hdr->signal_write_sequence_pos = 0; if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); } return 0; } int edf_blockwrite_digital_samples(int handle, int *buf) { int i, j, error, sf, digmax, digmin, edfsignals, buf_offset, value; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->signal_write_sequence_pos) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignals = hdr->edfsignals; if(!hdr->datarecords) { error = edflib_write_edf_header(hdr); if(error) { return error; } } buf_offset = 0; for(j=0; jedfparam[j].smp_per_record; digmax = hdr->edfparam[j].dig_max; digmin = hdr->edfparam[j].dig_min; if(hdr->edf) { if(hdr->wrbufsize < (sf * 2)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 2); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 2; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 2] = value & 0xff; hdr->wrbuf[i * 2 + 1] = (value >> 8) & 0xff; } if(fwrite(hdr->wrbuf, sf * 2, 1, file) != 1) { return -1; } } else // BDF { if(hdr->wrbufsize < (sf * 3)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 3); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 3; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 3] = value & 0xff; hdr->wrbuf[i * 3 + 1] = (value >> 8) & 0xff; hdr->wrbuf[i * 3 + 2] = (value >> 16) & 0xff; } if(fwrite(hdr->wrbuf, sf * 3, 1, file) != 1) { return -1; } } buf_offset += sf; } if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); return 0; } int edf_blockwrite_digital_short_samples(int handle, short *buf) { int i, j, error, sf, digmax, digmin, edfsignals, buf_offset, value; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->signal_write_sequence_pos) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } if(hdrlist[handle]->bdf == 1) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignals = hdr->edfsignals; if(!hdr->datarecords) { error = edflib_write_edf_header(hdr); if(error) { return error; } } buf_offset = 0; for(j=0; jedfparam[j].smp_per_record; digmax = hdr->edfparam[j].dig_max; digmin = hdr->edfparam[j].dig_min; if(hdr->edf) { if((digmax != 32767) || (digmin != -32768)) { for(i=0; i digmax) { buf[i + buf_offset] = digmax; } if(buf[i + buf_offset] < digmin) { buf[i + buf_offset] = digmin; } } } if(fwrite(buf + buf_offset, sf * 2, 1, file) != 1) { return -1; } } else // BDF { if(hdr->wrbufsize < (sf * 3)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 3); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 3; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 3] = value & 0xff; hdr->wrbuf[i * 3 + 1] = (value >> 8) & 0xff; hdr->wrbuf[i * 3 + 2] = (value >> 16) & 0xff; } if(fwrite(hdr->wrbuf, sf * 3, 1, file) != 1) { return -1; } } buf_offset += sf; } if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); return 0; } int edf_blockwrite_digital_3byte_samples(int handle, void *buf) { int j, error, edfsignals, total_samples=0; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->signal_write_sequence_pos) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } if(hdrlist[handle]->bdf != 1) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignals = hdr->edfsignals; if(!hdr->datarecords) { error = edflib_write_edf_header(hdr); if(error) { return error; } } for(j=0; jedfparam[j].smp_per_record; } if(fwrite(buf, total_samples * 3, 1, file) != 1) { return -1; } if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); return 0; } int edfwrite_physical_samples(int handle, double *buf) { int i, error, sf, digmax, digmin, value, edfsignal; double bitvalue, phys_offset; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignal = hdr->signal_write_sequence_pos; if(!hdr->datarecords) { if(!edfsignal) { error = edflib_write_edf_header(hdr); if(error) { return error; } } } sf = hdr->edfparam[edfsignal].smp_per_record; digmax = hdr->edfparam[edfsignal].dig_max; digmin = hdr->edfparam[edfsignal].dig_min; bitvalue = hdr->edfparam[edfsignal].bitvalue; phys_offset = hdr->edfparam[edfsignal].offset; if(hdr->edf) { if(hdr->wrbufsize < (sf * 2)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 2); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 2; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 2] = value & 0xff; hdr->wrbuf[i * 2 + 1] = (value >> 8) & 0xff; } if(fwrite(hdr->wrbuf, sf * 2, 1, file) != 1) { return -1; } } else // BDF { if(hdr->wrbufsize < (sf * 3)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 3); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 3; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 3] = value & 0xff; hdr->wrbuf[i * 3 + 1] = (value >> 8) & 0xff; hdr->wrbuf[i * 3 + 2] = (value >> 16) & 0xff; } if(fwrite(hdr->wrbuf, sf * 3, 1, file) != 1) { return -1; } } hdr->signal_write_sequence_pos++; if(hdr->signal_write_sequence_pos == hdr->edfsignals) { hdr->signal_write_sequence_pos = 0; if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); } return 0; } int edf_blockwrite_physical_samples(int handle, double *buf) { int i, j, error, sf, digmax, digmin, edfsignals, buf_offset, value; double bitvalue, phys_offset; FILE *file; struct edfhdrblock *hdr; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->signal_write_sequence_pos) { return -1; } if(hdrlist[handle]->edfsignals == 0) { return -1; } hdr = hdrlist[handle]; file = hdr->file_hdl; edfsignals = hdr->edfsignals; if(!hdr->datarecords) { error = edflib_write_edf_header(hdr); if(error) { return error; } } buf_offset = 0; for(j=0; jedfparam[j].smp_per_record; digmax = hdr->edfparam[j].dig_max; digmin = hdr->edfparam[j].dig_min; bitvalue = hdr->edfparam[j].bitvalue; phys_offset = hdr->edfparam[j].offset; if(hdr->edf) { if(hdr->wrbufsize < (sf * 2)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 2); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 2; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 2] = value & 0xff; hdr->wrbuf[i * 2 + 1] = (value >> 8) & 0xff; } if(fwrite(hdr->wrbuf, sf * 2, 1, file) != 1) { return -1; } } else // BDF { if(hdr->wrbufsize < (sf * 3)) { free(hdr->wrbuf); hdr->wrbufsize = 0; hdr->wrbuf = (char *)malloc(sf * 3); if(hdr->wrbuf == NULL) { return -1; } hdr->wrbufsize = sf * 3; } for(i=0; idigmax) { value = digmax; } if(valuewrbuf[i * 3] = value & 0xff; hdr->wrbuf[i * 3 + 1] = (value >> 8) & 0xff; hdr->wrbuf[i * 3 + 2] = (value >> 16) & 0xff; } if(fwrite(hdr->wrbuf, sf * 3, 1, file) != 1) { return -1; } } buf_offset += sf; } if(edflib_write_tal(hdr, file)) { return -1; } hdr->datarecords++; fflush(file); return 0; } static int edflib_write_edf_header(struct edfhdrblock *hdr) { int i, j, p, q, len, rest, edfsignals; char str[128]; struct tm *date_time; time_t elapsed_time; FILE *file; file = hdr->file_hdl; edfsignals = hdr->edfsignals; if(edfsignals<0) { return EDFLIB_NO_SIGNALS; } if(edfsignals>EDFLIB_MAXSIGNALS) { return EDFLIB_TOO_MANY_SIGNALS; } hdr->eq_sf = 1; hdr->recordsize = 0; hdr->total_annot_bytes = EDFLIB_ANNOTATION_BYTES * hdr->nr_annot_chns; for(i=0; iedfparam[i].smp_per_record<1) { return EDFLIB_NO_SAMPLES_IN_RECORD; } if(hdr->edfparam[i].dig_max==hdr->edfparam[i].dig_min) { return EDFLIB_DIGMIN_IS_DIGMAX; } if(hdr->edfparam[i].dig_maxedfparam[i].dig_min) { return EDFLIB_DIGMAX_LOWER_THAN_DIGMIN; } if(hdr->edfparam[i].phys_max==hdr->edfparam[i].phys_min) { return EDFLIB_PHYSMIN_IS_PHYSMAX; } hdr->recordsize += hdr->edfparam[i].smp_per_record; if(i > 0) { if(hdr->edfparam[i].smp_per_record != hdr->edfparam[i-1].smp_per_record) { hdr->eq_sf = 0; } } } if(hdr->edf) { hdr->recordsize *= 2; hdr->recordsize += hdr->total_annot_bytes; if(hdr->recordsize > (10 * 1024 * 1024)) /* datarecord size should not exceed 10MB for EDF */ { return EDFLIB_DATARECORD_SIZE_TOO_BIG; } /* if your application gets hit by this limitation, lower the value for the datarecord duration */ /* using the function edf_set_datarecord_duration() */ } else { hdr->recordsize *= 3; hdr->recordsize += hdr->total_annot_bytes; if(hdr->recordsize > (15 * 1024 * 1024)) /* datarecord size should not exceed 15MB for BDF */ { return EDFLIB_DATARECORD_SIZE_TOO_BIG; } /* if your application gets hit by this limitation, lower the value for the datarecord duration */ /* using the function edf_set_datarecord_duration() */ } for(i=0; iedfparam[i].bitvalue = (hdr->edfparam[i].phys_max - hdr->edfparam[i].phys_min) / (hdr->edfparam[i].dig_max - hdr->edfparam[i].dig_min); hdr->edfparam[i].offset = hdr->edfparam[i].phys_max / hdr->edfparam[i].bitvalue - hdr->edfparam[i].dig_max; } rewind(file); if(hdr->edf) { fprintf(file, "0 "); } else { fputc(255, file); fprintf(file, "BIOSEMI"); } p = 0; if(hdr->plus_birthdate[0]==0) { rest = 72; } else { rest = 62; } len = strlen(hdr->plus_patientcode); if(len && rest) { if(len>rest) { len = rest; rest = 0; } else { rest -= len; } edflib_strlcpy(str, hdr->plus_patientcode, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; for(i=0; iplus_gender[0]=='M') { fputc('M', file); } else { if(hdr->plus_gender[0]=='F') { fputc('F', file); } else { fputc('X', file); } } fputc(' ', file); p +=2; if(hdr->plus_birthdate[0]==0) { fputc('X', file); fputc(' ', file); p +=2; } else { fputc(hdr->plus_birthdate[0], file); fputc(hdr->plus_birthdate[1], file); fputc('-', file); q = edflib_atof_nonlocalized(&(hdr->plus_birthdate[3])); switch(q) { case 1: fprintf(file, "JAN"); break; case 2: fprintf(file, "FEB"); break; case 3: fprintf(file, "MAR"); break; case 4: fprintf(file, "APR"); break; case 5: fprintf(file, "MAY"); break; case 6: fprintf(file, "JUN"); break; case 7: fprintf(file, "JUL"); break; case 8: fprintf(file, "AUG"); break; case 9: fprintf(file, "SEP"); break; case 10: fprintf(file, "OCT"); break; case 11: fprintf(file, "NOV"); break; case 12: fprintf(file, "DEC"); break; } fputc('-', file); fputc(hdr->plus_birthdate[6], file); fputc(hdr->plus_birthdate[7], file); fputc(hdr->plus_birthdate[8], file); fputc(hdr->plus_birthdate[9], file); fputc(' ', file); p += 12; } len = strlen(hdr->plus_patient_name); if(len && rest) { if(len>rest) { len = rest; rest = 0; } else { rest -= len; } edflib_strlcpy(str, hdr->plus_patient_name, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; for(i=0; iplus_patient_additional); if(len && rest) { if(len>rest) { len = rest; } edflib_strlcpy(str, hdr->plus_patient_additional, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; p += fprintf(file, "%s", str); } for(; p<80; p++) { fputc(' ', file); } if(!hdr->startdate_year) { elapsed_time = time(NULL); date_time = localtime(&elapsed_time); hdr->startdate_year = date_time->tm_year + 1900; hdr->startdate_month = date_time->tm_mon + 1; hdr->startdate_day = date_time->tm_mday; hdr->starttime_hour = date_time->tm_hour; hdr->starttime_minute = date_time->tm_min; hdr->starttime_second = date_time->tm_sec % 60; } p = 0; p += fprintf(file, "Startdate %02u-", hdr->startdate_day); switch(hdr->startdate_month) { case 1 : fprintf(file, "JAN"); break; case 2 : fprintf(file, "FEB"); break; case 3 : fprintf(file, "MAR"); break; case 4 : fprintf(file, "APR"); break; case 5 : fprintf(file, "MAY"); break; case 6 : fprintf(file, "JUN"); break; case 7 : fprintf(file, "JUL"); break; case 8 : fprintf(file, "AUG"); break; case 9 : fprintf(file, "SEP"); break; case 10 : fprintf(file, "OCT"); break; case 11 : fprintf(file, "NOV"); break; case 12 : fprintf(file, "DEC"); break; } p += 3; fputc('-', file); p++; p += edflib_fprint_int_number_nonlocalized(file, hdr->startdate_year, 4, 0); fputc(' ', file); p++; rest = 42; len = strlen(hdr->plus_admincode); if(len && rest) { if(len>rest) { len = rest; rest = 0; } else { rest -= len; } edflib_strlcpy(str, hdr->plus_admincode, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; for(i=0; iplus_technician); if(len && rest) { if(len>rest) { len = rest; rest = 0; } else { rest -= len; } edflib_strlcpy(str, hdr->plus_technician, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; for(i=0; iplus_equipment); if(len && rest) { if(len>rest) { len = rest; rest = 0; } else { rest -= len; } edflib_strlcpy(str, hdr->plus_equipment, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; for(i=0; iplus_recording_additional); if(len && rest) { if(len>rest) { len = rest; } edflib_strlcpy(str, hdr->plus_recording_additional, 128); edflib_latin1_to_ascii(str, len); str[len] = 0; p += fprintf(file, "%s", str); } for(; p<80; p++) { fputc(' ', file); } fprintf(file, "%02u.%02u.%02u", hdr->startdate_day, hdr->startdate_month, (hdr->startdate_year % 100)); fprintf(file, "%02u.%02u.%02u", hdr->starttime_hour, hdr->starttime_minute, hdr->starttime_second); p = edflib_fprint_int_number_nonlocalized(file, (edfsignals + hdr->nr_annot_chns + 1) * 256, 0, 0); for(; p<8; p++) { fputc(' ', file); } if(hdr->edf) { fprintf(file, "EDF+C"); } else { fprintf(file, "BDF+C"); } for(i=0; i<39; i++) { fputc(' ', file); } fprintf(file, "-1 "); if(hdr->long_data_record_duration == EDFLIB_TIME_DIMENSION) { fprintf(file, "1 "); } else { edflib_snprint_number_nonlocalized(str, hdr->data_record_duration, 128); edflib_strlcat(str, " ", 128); str[8] = 0; fprintf(file, "%s", str); } p = edflib_fprint_int_number_nonlocalized(file, edfsignals + hdr->nr_annot_chns, 0, 0); for(; p<4; p++) { fputc(' ', file); } for(i=0; iedfparam[i].label); edflib_latin1_to_ascii(hdr->edfparam[i].label, len); for(j=0; jedfparam[i].label[j], file); } for(; j<16; j++) { fputc(' ', file); } } for(j=0; jnr_annot_chns; j++) { if(hdr->edf) { fprintf(file, "EDF Annotations "); } else { fprintf(file, "BDF Annotations "); } } for(i=0; iedfparam[i].transducer); edflib_latin1_to_ascii(hdr->edfparam[i].transducer, len); for(j=0; jedfparam[i].transducer[j], file); } for(; j<80; j++) { fputc(' ', file); } } for(j=0; jnr_annot_chns; j++) { for(i=0; i<80; i++) { fputc(' ', file); } } for(i=0; iedfparam[i].physdimension); edflib_latin1_to_ascii(hdr->edfparam[i].physdimension, len); for(j=0; jedfparam[i].physdimension[j], file); } for(; j<8; j++) { fputc(' ', file); } } for(j=0; jnr_annot_chns; j++) { fprintf(file, " "); } for(i=0; iedfparam[i].phys_min, 128); for(; p<8; p++) { str[p] = ' '; } str[8] = 0; fprintf(file, "%s", str); } for(j=0; jnr_annot_chns; j++) { fprintf(file, "-1 "); } for(i=0; iedfparam[i].phys_max, 128); for(; p<8; p++) { str[p] = ' '; } str[8] = 0; fprintf(file, "%s", str); } for(j=0; jnr_annot_chns; j++) { fprintf(file, "1 "); } for(i=0; iedfparam[i].dig_min, 0, 0); for(; p<8; p++) { fputc(' ', file); } } for(j=0; jnr_annot_chns; j++) { if(hdr->edf) { fprintf(file, "-32768 "); } else { fprintf(file, "-8388608"); } } for(i=0; iedfparam[i].dig_max, 0, 0); for(; p<8; p++) { fputc(' ', file); } } for(j=0; jnr_annot_chns; j++) { if(hdr->edf) { fprintf(file, "32767 "); } else { fprintf(file, "8388607 "); } } for(i=0; iedfparam[i].prefilter); edflib_latin1_to_ascii(hdr->edfparam[i].prefilter, len); for(j=0; jedfparam[i].prefilter[j], file); } for(; j<80; j++) { fputc(' ', file); } } for(i=0; inr_annot_chns; i++) { for(j=0; j<80; j++) { fputc(' ', file); } } for(i=0; iedfparam[i].smp_per_record, 0, 0); for(; p<8; p++) { fputc(' ', file); } } for(j=0; jnr_annot_chns; j++) { if(hdr->edf) { p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 2, 0, 0); for(; p<8; p++) { fputc(' ', file); } } else { p = edflib_fprint_int_number_nonlocalized(file, EDFLIB_ANNOTATION_BYTES / 3, 0, 0); for(; p<8; p++) { fputc(' ', file); } } } for(i=0; i<(edfsignals * 32); i++) { fputc(' ', file); } for(i=0; i<(hdr->nr_annot_chns * 32); i++) { fputc(' ', file); } return 0; } int edf_set_label(int handle, int edfsignal, const char *label) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->edfparam[edfsignal].label, label, 16); hdrlist[handle]->edfparam[edfsignal].label[16] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->edfparam[edfsignal].label); return 0; } int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->edfparam[edfsignal].physdimension, phys_dim, 8); hdrlist[handle]->edfparam[edfsignal].physdimension[8] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->edfparam[edfsignal].physdimension); return 0; } int edf_set_physical_maximum(int handle, int edfsignal, double phys_max) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } hdrlist[handle]->edfparam[edfsignal].phys_max = phys_max; return 0; } int edf_set_physical_minimum(int handle, int edfsignal, double phys_min) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } hdrlist[handle]->edfparam[edfsignal].phys_min = phys_min; return 0; } int edf_set_digital_maximum(int handle, int edfsignal, int dig_max) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->edf) { if(dig_max > 32767) { return -1; } } else { if(dig_max > 8388607) { return -1; } } if(hdrlist[handle]->datarecords) { return -1; } hdrlist[handle]->edfparam[edfsignal].dig_max = dig_max; return 0; } int edf_set_digital_minimum(int handle, int edfsignal, int dig_min) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->edf) { if(dig_min < (-32768)) { return -1; } } else { if(dig_min < (-8388608)) { return -1; } } if(hdrlist[handle]->datarecords) { return -1; } hdrlist[handle]->edfparam[edfsignal].dig_min = dig_min; return 0; } int edf_set_patientname(int handle, const char *patientname) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_patient_name, patientname, 80); hdrlist[handle]->plus_patient_name[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_patient_name); return 0; } int edf_set_patientcode(int handle, const char *patientcode) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_patientcode, patientcode, 80); hdrlist[handle]->plus_patientcode[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_patientcode); return 0; } int edf_set_gender(int handle, int gender) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } if((gender<0)||(gender>1)) { return -1; } if(gender) { hdrlist[handle]->plus_gender[0] = 'M'; } else { hdrlist[handle]->plus_gender[0] = 'F'; } hdrlist[handle]->plus_gender[1] = 0; return 0; } int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } if((birthdate_year<1800) || (birthdate_year>3000) || (birthdate_month<1) || (birthdate_month>12) || (birthdate_day<1) || (birthdate_day>31)) { return -1; } sprintf(hdrlist[handle]->plus_birthdate, "%02i.%02i.%02i%02i", birthdate_day, birthdate_month, birthdate_year / 100, birthdate_year % 100); hdrlist[handle]->plus_birthdate[10] = 0; return 0; } int edf_set_patient_additional(int handle, const char *patient_additional) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_patient_additional, patient_additional, 80); hdrlist[handle]->plus_patient_additional[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_patient_additional); return 0; } int edf_set_admincode(int handle, const char *admincode) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_admincode, admincode, 80); hdrlist[handle]->plus_admincode[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_admincode); return 0; } int edf_set_technician(int handle, const char *technician) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_technician, technician, 80); hdrlist[handle]->plus_technician[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_technician); return 0; } int edf_set_equipment(int handle, const char *equipment) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_equipment, equipment, 80); hdrlist[handle]->plus_equipment[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_equipment); return 0; } int edf_set_recording_additional(int handle, const char *recording_additional) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->plus_recording_additional, recording_additional, 80); hdrlist[handle]->plus_recording_additional[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->plus_recording_additional); return 0; } int edf_set_startdatetime(int handle, int startdate_year, int startdate_month, int startdate_day, int starttime_hour, int starttime_minute, int starttime_second) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } if((startdate_year<1970) || (startdate_year>3000) || (startdate_month<1) || (startdate_month>12) || (startdate_day<1) || (startdate_day>31) || (starttime_hour<0) || (starttime_hour>23) || (starttime_minute<0) || (starttime_minute>59) || (starttime_second<0) || (starttime_second>59)) { return -1; } hdrlist[handle]->startdate_year = startdate_year; hdrlist[handle]->startdate_month = startdate_month; hdrlist[handle]->startdate_day = startdate_day; hdrlist[handle]->starttime_hour = starttime_hour; hdrlist[handle]->starttime_minute = starttime_minute; hdrlist[handle]->starttime_second = starttime_second; return 0; } int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description) { int i; struct edf_write_annotationblock *list_annot, *malloc_list; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(onset<0LL) { return -1; } if(hdrlist[handle]->annots_in_file >= hdrlist[handle]->annotlist_sz) { malloc_list = (struct edf_write_annotationblock *)realloc(write_annotationslist[handle], sizeof(struct edf_write_annotationblock) * (hdrlist[handle]->annotlist_sz + EDFLIB_ANNOT_MEMBLOCKSZ)); if(malloc_list==NULL) { return -1; } write_annotationslist[handle] = malloc_list; hdrlist[handle]->annotlist_sz += EDFLIB_ANNOT_MEMBLOCKSZ; } list_annot = write_annotationslist[handle] + hdrlist[handle]->annots_in_file; list_annot->onset = onset; list_annot->duration = duration; strncpy(list_annot->annotation, description, EDFLIB_WRITE_MAX_ANNOTATION_LEN); list_annot->annotation[EDFLIB_WRITE_MAX_ANNOTATION_LEN] = 0; for(i=0; ; i++) { if(list_annot->annotation[i] == 0) { break; } if(list_annot->annotation[i] < 32) { list_annot->annotation[i] = '.'; } } hdrlist[handle]->annots_in_file++; return 0; } int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description) { struct edf_write_annotationblock *list_annot, *malloc_list; char str[EDFLIB_WRITE_MAX_ANNOTATION_LEN + 1]; if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(onset<0LL) { return -1; } if(hdrlist[handle]->annots_in_file >= hdrlist[handle]->annotlist_sz) { malloc_list = (struct edf_write_annotationblock *)realloc(write_annotationslist[handle], sizeof(struct edf_write_annotationblock) * (hdrlist[handle]->annotlist_sz + EDFLIB_ANNOT_MEMBLOCKSZ)); if(malloc_list==NULL) { return -1; } write_annotationslist[handle] = malloc_list; hdrlist[handle]->annotlist_sz += EDFLIB_ANNOT_MEMBLOCKSZ; } list_annot = write_annotationslist[handle] + hdrlist[handle]->annots_in_file; list_annot->onset = onset; list_annot->duration = duration; strncpy(str, description, EDFLIB_WRITE_MAX_ANNOTATION_LEN); str[EDFLIB_WRITE_MAX_ANNOTATION_LEN] = 0; edflib_latin12utf8(str, strlen(str)); strncpy(list_annot->annotation, str, EDFLIB_WRITE_MAX_ANNOTATION_LEN); list_annot->annotation[EDFLIB_WRITE_MAX_ANNOTATION_LEN] = 0; hdrlist[handle]->annots_in_file++; return 0; } static void edflib_remove_padding_trailing_spaces(char *str) { int i; while(str[0]==' ') { for(i=0; ; i++) { if(str[i]==0) { break; } str[i] = str[i+1]; } } for(i = strlen(str); i>0; i--) { if(str[i-1]==' ') { str[i-1] = 0; } else { break; } } } int edf_set_prefilter(int handle, int edfsignal, const char *prefilter) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->edfparam[edfsignal].prefilter, prefilter, 80); hdrlist[handle]->edfparam[edfsignal].prefilter[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->edfparam[edfsignal].prefilter); return 0; } int edf_set_transducer(int handle, int edfsignal, const char *transducer) { if(handle<0) { return -1; } if(handle>=EDFLIB_MAXFILES) { return -1; } if(hdrlist[handle]==NULL) { return -1; } if(!(hdrlist[handle]->writemode)) { return -1; } if(edfsignal<0) { return -1; } if(edfsignal>=hdrlist[handle]->edfsignals) { return -1; } if(hdrlist[handle]->datarecords) { return -1; } strncpy(hdrlist[handle]->edfparam[edfsignal].transducer, transducer, 80); hdrlist[handle]->edfparam[edfsignal].transducer[80] = 0; edflib_remove_padding_trailing_spaces(hdrlist[handle]->edfparam[edfsignal].transducer); return 0; } /* minimum is the minimum digits that will be printed (minus sign not included), leading zero's will be added if necessary */ /* if sign is zero, only negative numbers will have the sign '-' character */ /* if sign is one, the sign '+' or '-' character will always be printed */ /* returns the amount of characters printed */ static int edflib_fprint_int_number_nonlocalized(FILE *file, int q, int minimum, int sign) { int flag=0, z, i, j=0, base = 1000000000; if(minimum < 0) { minimum = 0; } if(minimum > 9) { flag = 1; } if(q < 0) { fputc('-', file); j++; q = -q; } else { if(sign) { fputc('+', file); j++; } } for(i=10; i; i--) { if(minimum == i) { flag = 1; } z = q / base; q %= base; if(z || flag) { fputc('0' + z, file); j++; flag = 1; } base /= 10; } if(!flag) { fputc('0', file); j++; } return j; } /* minimum is the minimum digits that will be printed (minus sign not included), leading zero's will be added if necessary */ /* if sign is zero, only negative numbers will have the sign '-' character */ /* if sign is one, the sign '+' or '-' character will always be printed */ /* returns the amount of characters printed */ static int edflib_fprint_ll_number_nonlocalized(FILE *file, long long q, int minimum, int sign) { int flag=0, z, i, j=0; long long base = 1000000000000000000LL; if(minimum < 0) { minimum = 0; } if(minimum > 18) { flag = 1; } if(q < 0LL) { fputc('-', file); j++; q = -q; } else { if(sign) { fputc('+', file); j++; } } for(i=19; i; i--) { if(minimum == i) { flag = 1; } z = q / base; q %= base; if(z || flag) { fputc('0' + z, file); j++; flag = 1; } base /= 10LL; } if(!flag) { fputc('0', file); j++; } return j; } /* minimum is the minimum digits that will be printed (minus sign not included), leading zero's will be added if necessary */ /* if sign is zero, only negative numbers will have the sign '-' character */ /* if sign is one, the sign '+' or '-' character will always be printed */ /* returns the amount of characters printed */ /* static int edflib_sprint_int_number_nonlocalized(char *str, int q, int minimum, int sign) { int flag=0, z, i, j=0, base = 1000000000; if(minimum < 0) { minimum = 0; } if(minimum > 9) { flag = 1; } if(q < 0) { str[j++] = '-'; q = -q; } else { if(sign) { str[j++] = '+'; } } for(i=10; i; i--) { if(minimum == i) { flag = 1; } z = q / base; q %= base; if(z || flag) { str[j++] = '0' + z; flag = 1; } base /= 10; } if(!flag) { str[j++] = '0'; } str[j] = 0; return j; } */ /* minimum is the minimum digits that will be printed (minus sign not included), leading zero's will be added if necessary */ /* if sign is zero, only negative numbers will have the sign '-' character */ /* if sign is one, the sign '+' or '-' character will always be printed */ /* returns the amount of characters printed */ static int edflib_snprint_ll_number_nonlocalized(char *dest, long long q, int minimum, int sign, int sz) { int flag=0, z, i, j=0; long long base = 1000000000000000000LL; if(sz < 1) { return 0; } if(minimum < 0) { minimum = 0; } if(minimum > 18) { flag = 1; } if(q < 0LL) { dest[j++] = '-'; q = -q; } else { if(sign) { dest[j++] = '+'; } } if(j == sz) { dest[--j] = 0; return j; } for(i=19; i; i--) { if(minimum == i) { flag = 1; } z = q / base; q %= base; if(z || flag) { dest[j++] = '0' + z; if(j == sz) { dest[--j] = 0; return j; } flag = 1; } base /= 10LL; } if(!flag) { dest[j++] = '0'; } if(j == sz) { dest[--j] = 0; return j; } dest[j] = 0; return j; } static int edflib_snprint_number_nonlocalized(char *dest, double val, int sz) { int flag=0, z, i, j=0, q, base = 1000000000; double var; if(sz < 1) return 0; q = (int)val; var = val - q; if(val < 0.0) { dest[j++] = '-'; if(q < 0) { q = -q; } } if(j == sz) { dest[--j] = 0; return j; } for(i=10; i; i--) { z = q / base; q %= base; if(z || flag) { dest[j++] = '0' + z; if(j == sz) { dest[--j] = 0; return j; } flag = 1; } base /= 10; } if(!flag) { dest[j++] = '0'; } if(j == sz) { dest[--j] = 0; return j; } base = 100000000; var *= (base * 10); q = (int)var; if(q < 0) { q = -q; } if(!q) { dest[j] = 0; return j; } dest[j++] = '.'; if(j == sz) { dest[--j] = 0; return j; } for(i=9; i; i--) { z = q / base; q %= base; dest[j++] = '0' + z; if(j == sz) { dest[--j] = 0; return j; } base /= 10; } dest[j] = 0; j--; for(; j>0; j--) { if(dest[j] == '0') { dest[j] = 0; } else { j++; break; } } return j; } static double edflib_atof_nonlocalized(const char *str) { int i=0, j, dot_pos=-1, decimals=0, sign=1, exp_pos=-1, exp_sign=1, exp_val=0; double value, value2=0.0; value = edflib_atoi_nonlocalized(str); while(str[i] == ' ') { i++; } if((str[i] == '+') || (str[i] == '-')) { if(str[i] == '-') { sign = -1; } i++; } for(; ; i++) { if(str[i] == 0) { break; } if((str[i] == 'e') || (str[i] == 'E')) { exp_pos = i; break; } if(((str[i] < '0') || (str[i] > '9')) && (str[i] != '.')) { break; } if(dot_pos >= 0) { if((str[i] >= '0') && (str[i] <= '9')) { decimals++; } else { break; } } if(str[i] == '.') { if(dot_pos < 0) { dot_pos = i; } } } if(decimals) { value2 = edflib_atoi_nonlocalized(str + dot_pos + 1) * sign; i = 1; while(decimals--) { i *= 10; } value2 /= i; value += value2; } if(exp_pos > 0) { i = exp_pos + 1; if(str[i]) { if(str[i] == '+') { i++; } else if(str[i] == '-') { exp_sign = -1; i++; } if(str[i]) { exp_val = edflib_atoi_nonlocalized(str + i); if(exp_val > 0) { for(j=0; j 0) { value *= 10; } else { value /= 10; } } } } } } return value; } static int edflib_atoi_nonlocalized(const char *str) { int i=0, value=0, sign=1; while(str[i] == ' ') { i++; } if((str[i] == '+') || (str[i] == '-')) { if(str[i] == '-') { sign = -1; } i++; } for( ; ; i++) { if(str[i] == 0) { break; } if((str[i] < '0') || (str[i] > '9')) { break; } value *= 10; value += (str[i] - '0'); } return value * sign; } static int edflib_write_tal(struct edfhdrblock *hdr, FILE *file) { int p; char str[EDFLIB_ANNOTATION_BYTES * (EDFLIB_MAX_ANNOTATION_CHANNELS + 1)]; p = edflib_snprint_ll_number_nonlocalized(str, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1, EDFLIB_ANNOTATION_BYTES * (EDFLIB_MAX_ANNOTATION_CHANNELS + 1)); if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) { str[p++] = '.'; p += edflib_snprint_ll_number_nonlocalized(str + p, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0, (EDFLIB_ANNOTATION_BYTES * (EDFLIB_MAX_ANNOTATION_CHANNELS + 1)) - p); } str[p++] = 20; str[p++] = 20; for(; ptotal_annot_bytes; p++) { str[p] = 0; } if(fwrite(str, hdr->total_annot_bytes, 1, file) != 1) { return -1; } return 0; } static int edflib_strlcpy(char *dst, const char *src, int sz) { int srclen; sz--; srclen = strlen(src); if(srclen > sz) srclen = sz; memcpy(dst, src, srclen); dst[srclen] = 0; return srclen; } static int edflib_strlcat(char *dst, const char *src, int sz) { int srclen, dstlen; dstlen = strlen(dst); sz -= dstlen + 1; if(!sz) return dstlen; srclen = strlen(src); if(srclen > sz) srclen = sz; memcpy(dst + dstlen, src, srclen); dst[dstlen + srclen] = 0; return (dstlen + srclen); } PaxHeader/edflib.h000644 001750 000144 00000000065 13532501005 014420 xustar00guvusers000000 000000 29 mtime=1567261189.80182895 24 SCHILY.fflags=extent edflib.h000644 001750 000144 00000105675 13532501005 012464 0ustar00guvusers000000 000000 /* ***************************************************************************** * * Copyright (c) 2009 - 2019 Teunis van Beelen * All rights reserved. * * Email: teuniz@protonmail.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ***************************************************************************** */ /* compile with options "-D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE" */ #ifndef EDFLIB_INCLUDED #define EDFLIB_INCLUDED #include #include #include #include #define EDFLIB_TIME_DIMENSION (10000000LL) #define EDFLIB_MAXSIGNALS 640 #define EDFLIB_MAX_ANNOTATION_LEN 512 #define EDFSEEK_SET 0 #define EDFSEEK_CUR 1 #define EDFSEEK_END 2 /* the following defines are used in the member "filetype" of the edf_hdr_struct */ /* and as return value for the function edfopen_file_readonly() */ #define EDFLIB_FILETYPE_EDF 0 #define EDFLIB_FILETYPE_EDFPLUS 1 #define EDFLIB_FILETYPE_BDF 2 #define EDFLIB_FILETYPE_BDFPLUS 3 #define EDFLIB_MALLOC_ERROR -1 #define EDFLIB_NO_SUCH_FILE_OR_DIRECTORY -2 /* when this error occurs, try to open the file with EDFbrowser, it will give you full details about the cause of the error. */ #define EDFLIB_FILE_CONTAINS_FORMAT_ERRORS -3 #define EDFLIB_MAXFILES_REACHED -4 #define EDFLIB_FILE_READ_ERROR -5 #define EDFLIB_FILE_ALREADY_OPENED -6 #define EDFLIB_FILETYPE_ERROR -7 #define EDFLIB_FILE_WRITE_ERROR -8 #define EDFLIB_NUMBER_OF_SIGNALS_INVALID -9 #define EDFLIB_FILE_IS_DISCONTINUOUS -10 #define EDFLIB_INVALID_READ_ANNOTS_VALUE -11 /* values for annotations */ #define EDFLIB_DO_NOT_READ_ANNOTATIONS 0 #define EDFLIB_READ_ANNOTATIONS 1 #define EDFLIB_READ_ALL_ANNOTATIONS 2 /* the following defines are possible errors returned by the first sample write action */ #define EDFLIB_NO_SIGNALS -20 #define EDFLIB_TOO_MANY_SIGNALS -21 #define EDFLIB_NO_SAMPLES_IN_RECORD -22 #define EDFLIB_DIGMIN_IS_DIGMAX -23 #define EDFLIB_DIGMAX_LOWER_THAN_DIGMIN -24 #define EDFLIB_PHYSMIN_IS_PHYSMAX -25 #define EDFLIB_DATARECORD_SIZE_TOO_BIG -26 #ifdef __cplusplus extern "C" { #endif /* For more info about the EDF and EDF+ format, visit: http://edfplus.info/specs/ */ /* For more info about the BDF and BDF+ format, visit: http://www.teuniz.net/edfbrowser/bdfplus%20format%20description.html */ /* * note: In EDF, the sensitivity (e.g. uV/bit) and offset are stored using four parameters: * digital maximum and minimum, and physical maximum and minimum. * Here, digital means the raw data coming from a sensor or ADC. Physical means the units like uV. * The sensitivity in units/bit is calculated as follows: * * units per bit = (physical max - physical min) / (digital max - digital min) * * The digital offset is calculated as follows: * * offset = (physical max / units per bit) - digital max * * For a better explanation about the relation between digital data and physical data, * read the document "Coding Schemes Used with Data Converters" (PDF): * * http://www.ti.com/general/docs/lit/getliterature.tsp?baseLiteratureNumber=sbaa042 * * note: An EDF file usually contains multiple so-called datarecords. One datarecord usually has a duration of one second (this is the default but it is not mandatory!). * In that case a file with a duration of five minutes contains 300 datarecords. The duration of a datarecord can be freely choosen but, if possible, use values from * 0.1 to 1 second for easier handling. Just make sure that the total size of one datarecord, expressed in bytes, does not exceed 10MByte (15MBytes for BDF(+)). * * The RECOMMENDATION of a maximum datarecordsize of 61440 bytes in the EDF and EDF+ specification was usefull in the time people were still using DOS as their main operating system. * Using DOS and fast (near) pointers (16-bit pointers), the maximum allocatable block of memory was 64KByte. * This is not a concern anymore so the maximum datarecord size now is limited to 10MByte for EDF(+) and 15MByte for BDF(+). This helps to accommodate for higher samplingrates * used by modern Analog to Digital Converters. * * EDF header character encoding: The EDF specification says that only ASCII characters are allowed. * EDFlib will automatically convert characters with accents, umlauts, tilde, etc. to their "normal" equivalent without the accent/umlaut/tilde/etc. * * The description/name of an EDF+ annotation on the other hand, is encoded in UTF-8. * */ struct edf_param_struct{ /* this structure contains all the relevant EDF-signal parameters of one signal */ char label[17]; /* label (name) of the signal, null-terminated string */ long long smp_in_file; /* number of samples of this signal in the file */ double phys_max; /* physical maximum, usually the maximum input of the ADC */ double phys_min; /* physical minimum, usually the minimum input of the ADC */ int dig_max; /* digital maximum, usually the maximum output of the ADC, can not not be higher than 32767 for EDF or 8388607 for BDF */ int dig_min; /* digital minimum, usually the minimum output of the ADC, can not not be lower than -32768 for EDF or -8388608 for BDF */ int smp_in_datarecord; /* number of samples of this signal in a datarecord, if the datarecord has a duration of one second (default), then it equals the samplerate */ char physdimension[9]; /* physical dimension (uV, bpm, mA, etc.), null-terminated string */ char prefilter[81]; /* null-terminated string */ char transducer[81]; /* null-terminated string */ }; struct edf_annotation_struct{ /* this structure is used for annotations */ long long onset; /* onset time of the event, expressed in units of 100 nanoSeconds and relative to the starttime in the header */ char duration[16]; /* duration time, this is a null-terminated ASCII text-string */ char annotation[EDFLIB_MAX_ANNOTATION_LEN + 1]; /* description of the event in UTF-8, this is a null terminated string */ }; struct edf_hdr_struct{ /* this structure contains all the relevant EDF header info and will be filled when calling the function edf_open_file_readonly() */ int handle; /* a handle (identifier) used to distinguish the different files */ int filetype; /* 0: EDF, 1: EDFplus, 2: BDF, 3: BDFplus, a negative number means an error */ int edfsignals; /* number of EDF signals in the file, annotation channels are NOT included */ long long file_duration; /* duration of the file expressed in units of 100 nanoSeconds */ int startdate_day; int startdate_month; int startdate_year; long long starttime_subsecond; /* starttime offset expressed in units of 100 nanoSeconds. Is always less than 10000000 (one second). Only used by EDFplus and BDFplus */ int starttime_second; int starttime_minute; int starttime_hour; char patient[81]; /* null-terminated string, contains patientfield of header, is always empty when filetype is EDFPLUS or BDFPLUS */ char recording[81]; /* null-terminated string, contains recordingfield of header, is always empty when filetype is EDFPLUS or BDFPLUS */ char patientcode[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char gender[16]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char birthdate[16]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char patient_name[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char patient_additional[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char admincode[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char technician[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char equipment[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ char recording_additional[81]; /* null-terminated string, is always empty when filetype is EDF or BDF */ long long datarecord_duration; /* duration of a datarecord expressed in units of 100 nanoSeconds */ long long datarecords_in_file; /* number of datarecords in the file */ long long annotations_in_file; /* number of annotations in the file */ struct edf_param_struct signalparam[EDFLIB_MAXSIGNALS]; /* array of structs which contain the relevant signal parameters */ }; /***************** the following functions are used to read files **************************/ int edfopen_file_readonly(const char *path, struct edf_hdr_struct *edfhdr, int read_annotations); /* opens an existing file for reading */ /* path is a null-terminated string containing the path to the file */ /* hdr is a pointer to an edf_hdr_struct, all fields in this struct will be overwritten */ /* the edf_hdr_struct will be filled with all the relevant header- and signalinfo/parameters */ /* read_annotations must have one of the following values: */ /* EDFLIB_DO_NOT_READ_ANNOTATIONS annotations will not be read (this saves time when opening a very large EDFplus or BDFplus file */ /* EDFLIB_READ_ANNOTATIONS annotations will be read immediately, stops when an annotation has */ /* been found which contains the description "Recording ends" */ /* EDFLIB_READ_ALL_ANNOTATIONS all annotations will be read immediately */ /* returns 0 on success, in case of an error it returns -1 and an errorcode will be set in the member "filetype" of struct edf_hdr_struct */ /* This function is required if you want to read a file */ int edfread_physical_samples(int handle, int edfsignal, int n, double *buf); /* reads n samples from edfsignal, starting from the current sample position indicator, into buf (edfsignal starts at 0) */ /* the values are converted to their physical values e.g. microVolts, beats per minute, etc. */ /* bufsize should be equal to or bigger than sizeof(double[n]) */ /* the sample position indicator will be increased with the amount of samples read */ /* returns the amount of samples read (this can be less than n or zero!) */ /* or -1 in case of an error */ int edfread_digital_samples(int handle, int edfsignal, int n, int *buf); /* reads n samples from edfsignal, starting from the current sample position indicator, into buf (edfsignal starts at 0) */ /* the values are the "raw" digital values */ /* bufsize should be equal to or bigger than sizeof(int[n]) */ /* the sample position indicator will be increased with the amount of samples read */ /* returns the amount of samples read (this can be less than n or zero!) */ /* or -1 in case of an error */ long long edfseek(int handle, int edfsignal, long long offset, int whence); /* The edfseek() function sets the sample position indicator for the edfsignal pointed to by edfsignal. */ /* The new position, measured in samples, is obtained by adding offset samples to the position specified by whence. */ /* If whence is set to EDFSEEK_SET, EDFSEEK_CUR, or EDFSEEK_END, the offset is relative to the start of the file, */ /* the current position indicator, or end-of-file, respectively. */ /* Returns the current offset. Otherwise, -1 is returned. */ /* note that every signal has it's own independent sample position indicator and edfseek() affects only one of them */ long long edftell(int handle, int edfsignal); /* The edftell() function obtains the current value of the sample position indicator for the edfsignal pointed to by edfsignal. */ /* Returns the current offset. Otherwise, -1 is returned */ /* note that every signal has it's own independent sample position indicator and edftell() affects only one of them */ void edfrewind(int handle, int edfsignal); /* The edfrewind() function sets the sample position indicator for the edfsignal pointed to by edfsignal to the beginning of the file. */ /* It is equivalent to: (void) edfseek(int handle, int edfsignal, 0LL, EDFSEEK_SET) */ /* note that every signal has it's own independent sample position indicator and edfrewind() affects only one of them */ int edf_get_annotation(int handle, int n, struct edf_annotation_struct *annot); /* Fills the edf_annotation_struct with the annotation n, returns 0 on success, otherwise -1 */ /* The string that describes the annotation/event is encoded in UTF-8 */ /* To obtain the number of annotations in a file, check edf_hdr_struct -> annotations_in_file. */ /* returns 0 on success or -1 in case of an error */ /* Notes: Annotationsignals ================= EDFplus and BDFplus store the annotations in one or more signals (in order to be backwards compatibel with EDF and BDF). The counting of the signals in the file starts at 0. Signals used for annotations are skipped by EDFlib. This means that the annotationsignal(s) in the file are hided. Use the function edf_get_annotation() to get the annotations. So, when a file contains 5 signals and the third signal is used to store the annotations, the library will report that there are only 4 signals in the file. The library will "map" the signalnumbers as follows: 0->0, 1->1, 2->3, 3->4. This way you don't need to worry about which signals are annotationsignals, the library will take care of it. How the library stores time-values ================================== To avoid rounding errors, the library stores some timevalues in variables of type long long int. In order not to loose the subsecond precision, all timevalues have been multiplied by 10000000. This will limit the timeresolution to 100 nanoSeconds. To calculate the amount of seconds, divide the timevalue by 10000000 or use the macro EDFLIB_TIME_DIMENSION which is declared in edflib.h. The following variables do use this when you open a file in read mode: "file_duration", "starttime_subsecond" and "onset". */ /***************** the following functions are used to read or write files **************************/ int edfclose_file(int handle); /* closes (and in case of writing, finalizes) the file */ /* returns -1 in case of an error, 0 on success */ /* this function MUST be called when you are finished reading or writing */ /* This function is required after reading or writing. Failing to do so will cause */ /* unnessecary memory usage and in case of writing it will cause a corrupted and incomplete file */ int edflib_version(void); /* Returns the version number of this library, multiplied by hundred. if version is "1.00" than it will return 100 */ int edflib_is_file_used(const char *path); /* returns 1 if the file is used, either for reading or writing */ /* otherwise returns 0 */ int edflib_get_number_of_open_files(void); /* returns the number of open files, either for reading or writing */ int edflib_get_handle(int file_number); /* returns the handle of an opened file, either for reading or writing */ /* file_number starts with 0 */ /* returns -1 if the file is not opened */ /***************** the following functions are used to write files **************************/ int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals); /* opens an new file for writing. warning, an already existing file with the same name will be silently overwritten without advance warning!! */ /* path is a null-terminated string containing the path and name of the file */ /* filetype must be EDFLIB_FILETYPE_EDFPLUS or EDFLIB_FILETYPE_BDFPLUS */ /* returns a handle on success, you need this handle for the other functions */ /* in case of an error it returns a negative number corresponding to one of the following values: */ /* EDFLIB_MALLOC_ERROR */ /* EDFLIB_NO_SUCH_FILE_OR_DIRECTORY */ /* EDFLIB_MAXFILES_REACHED */ /* EDFLIB_FILE_ALREADY_OPENED */ /* EDFLIB_NUMBER_OF_SIGNALS_INVALID */ /* This function is required if you want to write a file */ int edf_set_samplefrequency(int handle, int edfsignal, int samplefrequency); /* Sets the samplefrequency of signal edfsignal. (In reallity, it sets the number of samples in a datarecord.) */ /* Returns 0 on success, otherwise -1 */ /* This function is required for every signal and can be called only after opening a */ /* file in writemode and before the first sample write action */ int edf_set_physical_maximum(int handle, int edfsignal, double phys_max); /* Sets the maximum physical value of signal edfsignal. (the value of the input of the ADC when the output equals the value of "digital maximum") */ /* Returns 0 on success, otherwise -1 */ /* This function is required for every signal and can be called only after opening a */ /* file in writemode and before the first sample write action */ int edf_set_physical_minimum(int handle, int edfsignal, double phys_min); /* Sets the minimum physical value of signal edfsignal. (the value of the input of the ADC when the output equals the value of "digital minimum") */ /* Usually this will be (-(phys_max)) */ /* Returns 0 on success, otherwise -1 */ /* This function is required for every signal and can be called only after opening a */ /* file in writemode and before the first sample write action */ int edf_set_digital_maximum(int handle, int edfsignal, int dig_max); /* Sets the maximum digital value of signal edfsignal. The maximum value is 32767 for EDF+ and 8388607 for BDF+ */ /* Usually it's the extreme output of the ADC */ /* Returns 0 on success, otherwise -1 */ /* This function is required for every signal and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_digital_minimum(int handle, int edfsignal, int dig_min); /* Sets the minimum digital value of signal edfsignal. The minimum value is -32768 for EDF+ and -8388608 for BDF+ */ /* Usually it's the extreme output of the ADC */ /* Usually this will be (-(dig_max + 1)) */ /* Returns 0 on success, otherwise -1 */ /* This function is required for every signal and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_label(int handle, int edfsignal, const char *label); /* Sets the label (name) of signal edfsignal. ("FP1", "SaO2", etc.) */ /* label is a pointer to a NULL-terminated ASCII-string containing the label (name) of the signal edfsignal */ /* Returns 0 on success, otherwise -1 */ /* This function is recommended for every signal when you want to write a file */ /* and can be called only after opening a file in writemode and before the first sample write action */ int edf_set_prefilter(int handle, int edfsignal, const char *prefilter); /* Sets the prefilter of signal edfsignal ("HP:0.1Hz", "LP:75Hz N:50Hz", etc.). */ /* prefilter is a pointer to a NULL-terminated ASCII-string containing the prefilter text of the signal edfsignal */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode and before */ /* the first sample write action */ int edf_set_transducer(int handle, int edfsignal, const char *transducer); /* Sets the transducer of signal edfsignal ("AgAgCl cup electrodes", etc.). */ /* transducer is a pointer to a NULL-terminated ASCII-string containing the transducer text of the signal edfsignal */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode and before */ /* the first sample write action */ int edf_set_physical_dimension(int handle, int edfsignal, const char *phys_dim); /* Sets the physical dimension (unit) of signal edfsignal. ("uV", "BPM", "mA", "Degr.", etc.) */ /* phys_dim is a pointer to a NULL-terminated ASCII-string containing the physical dimension of the signal edfsignal */ /* Returns 0 on success, otherwise -1 */ /* This function is recommanded for every signal when you want to write a file */ /* and can be called only after opening a file in writemode and before the first sample write action */ int edf_set_startdatetime(int handle, int startdate_year, int startdate_month, int startdate_day, int starttime_hour, int starttime_minute, int starttime_second); /* Sets the startdate and starttime. */ /* year: 1970 - 3000, month: 1 - 12, day: 1 - 31 */ /* hour: 0 - 23, minute: 0 - 59, second: 0 - 59 */ /* If not called, the library will use the system date and time at runtime */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_patientname(int handle, const char *patientname); /* Sets the patientname. patientname is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_patientcode(int handle, const char *patientcode); /* Sets the patientcode. patientcode is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_gender(int handle, int gender); /* Sets the gender. 1 is male, 0 is female. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_birthdate(int handle, int birthdate_year, int birthdate_month, int birthdate_day); /* Sets the birthdate. */ /* year: 1800 - 3000, month: 1 - 12, day: 1 - 31 */ /* This function is optional */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_patient_additional(int handle, const char *patient_additional); /* Sets the additional patientinfo. patient_additional is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_admincode(int handle, const char *admincode); /* Sets the admincode. admincode is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_technician(int handle, const char *technician); /* Sets the technicians name. technician is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_equipment(int handle, const char *equipment); /* Sets the name of the equipment used during the aquisition. equipment is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edf_set_recording_additional(int handle, const char *recording_additional); /* Sets the additional recordinginfo. recording_additional is a pointer to a null-terminated ASCII-string. */ /* Returns 0 on success, otherwise -1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ int edfwrite_physical_samples(int handle, double *buf); /* Writes n physical samples (uV, mA, Ohm) from *buf belonging to one signal */ /* where n is the samplefrequency of that signal. */ /* The physical samples will be converted to digital samples using the */ /* values of physical maximum, physical minimum, digital maximum and digital minimum */ /* The number of samples written is equal to the samplefrequency of the signal */ /* Size of buf should be equal to or bigger than sizeof(double[samplefrequency]) */ /* Call this function for every signal in the file. The order is important! */ /* When there are 4 signals in the file, the order of calling this function */ /* must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc. */ /* Returns 0 on success, otherwise -1 */ int edf_blockwrite_physical_samples(int handle, double *buf); /* Writes physical samples (uV, mA, Ohm) from *buf */ /* buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc. */ /* where n is the samplefrequency of that signal. */ /* buf must be filled with samples from all signals, starting with signal 0, 1, 2, etc. */ /* one block equals one second */ /* The physical samples will be converted to digital samples using the */ /* values of physical maximum, physical minimum, digital maximum and digital minimum */ /* The number of samples written is equal to the sum of the samplefrequencies of all signals */ /* Size of buf should be equal to or bigger than sizeof(double) multiplied by the sum of the samplefrequencies of all signals */ /* Returns 0 on success, otherwise -1 */ int edfwrite_digital_short_samples(int handle, short *buf); /* Writes n "raw" digital samples from *buf belonging to one signal */ /* where n is the samplefrequency of that signal. */ /* The samples will be written to the file without any conversion. */ /* Because the size of a short is 16-bit, do not use this function with BDF (24-bit) */ /* The number of samples written is equal to the samplefrequency of the signal */ /* Size of buf should be equal to or bigger than sizeof(short[samplefrequency]) */ /* Call this function for every signal in the file. The order is important! */ /* When there are 4 signals in the file, the order of calling this function */ /* must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc. */ /* Returns 0 on success, otherwise -1 */ int edfwrite_digital_samples(int handle, int *buf); /* Writes n "raw" digital samples from *buf belonging to one signal */ /* where n is the samplefrequency of that signal. */ /* The 16 (or 24 in case of BDF) least significant bits of the sample will be written to the */ /* file without any conversion. */ /* The number of samples written is equal to the samplefrequency of the signal */ /* Size of buf should be equal to or bigger than sizeof(int[samplefrequency]) */ /* Call this function for every signal in the file. The order is important! */ /* When there are 4 signals in the file, the order of calling this function */ /* must be: signal 0, signal 1, signal 2, signal 3, signal 0, signal 1, signal 2, etc. */ /* Returns 0 on success, otherwise -1 */ int edf_blockwrite_digital_3byte_samples(int handle, void *buf); /* Writes "raw" digital samples from *buf. */ /* buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc. */ /* where n is the samplefrequency of that signal. */ /* One block equals one second. One sample equals 3 bytes, order is little endian (least significant byte first) */ /* Encoding is second's complement, most significant bit of most significant byte is the sign-bit */ /* The samples will be written to the file without any conversion. */ /* Because the size of a 3-byte sample is 24-bit, this function can only be used when writing a BDF file */ /* The number of samples written is equal to the sum of the samplefrequencies of all signals. */ /* Size of buf should be equal to or bigger than: the sum of the samplefrequencies of all signals x 3 bytes */ /* Returns 0 on success, otherwise -1 */ int edf_blockwrite_digital_short_samples(int handle, short *buf); /* Writes "raw" digital samples from *buf. */ /* buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc. */ /* where n is the samplefrequency of that signal. */ /* One block equals one second. */ /* The samples will be written to the file without any conversion. */ /* Because the size of a short is 16-bit, do not use this function with BDF (24-bit) */ /* The number of samples written is equal to the sum of the samplefrequencies of all signals. */ /* Size of buf should be equal to or bigger than sizeof(short) multiplied by the sum of the samplefrequencies of all signals */ /* Returns 0 on success, otherwise -1 */ int edf_blockwrite_digital_samples(int handle, int *buf); /* Writes "raw" digital samples from *buf. */ /* buf must be filled with samples from all signals, starting with n samples of signal 0, n samples of signal 1, n samples of signal 2, etc. */ /* where n is the samplefrequency of that signal. */ /* One block equals one second. */ /* The 16 (or 24 in case of BDF) least significant bits of the sample will be written to the */ /* file without any conversion. */ /* The number of samples written is equal to the sum of the samplefrequencies of all signals. */ /* Size of buf should be equal to or bigger than sizeof(int) multiplied by the sum of the samplefrequencies of all signals */ /* Returns 0 on success, otherwise -1 */ int edfwrite_annotation_utf8(int handle, long long onset, long long duration, const char *description); /* writes an annotation/event to the file */ /* onset is relative to the starttime and startdate of the file */ /* onset and duration are in units of 100 microSeconds! resolution is 0.0001 second! */ /* for example: 34.071 seconds must be written as 340710 */ /* if duration is unknown or not applicable: set a negative number (-1) */ /* description is a null-terminated UTF8-string containing the text that describes the event */ /* This function is optional and can be called only after opening a file in writemode */ /* and before closing the file */ int edfwrite_annotation_latin1(int handle, long long onset, long long duration, const char *description); /* writes an annotation/event to the file */ /* onset is relative to the starttime and startdate of the file */ /* onset and duration are in units of 100 microSeconds! resolution is 0.0001 second! */ /* for example: 34.071 seconds must be written as 340710 */ /* if duration is unknown or not applicable: set a negative number (-1) */ /* description is a null-terminated Latin1-string containing the text that describes the event */ /* This function is optional and can be called only after opening a file in writemode */ /* and before closing the file */ int edf_set_datarecord_duration(int handle, int duration); /* Sets the datarecord duration. The default value is 1 second. */ /* ATTENTION: the argument "duration" is expressed in units of 10 microSeconds! */ /* So, if you want to set the datarecord duration to 0.1 second, you must give */ /* the argument "duration" a value of "10000". */ /* This function is optional, normally you don't need to change the default value. */ /* The datarecord duration must be in the range 0.001 to 60 seconds. */ /* Returns 0 on success, otherwise -1 */ /* This function is NOT REQUIRED but can be called after opening a */ /* file in writemode and before the first sample write action. */ /* This function can be used when you want to use a samplerate */ /* which is not an integer. For example, if you want to use a samplerate of 0.5 Hz, */ /* set the samplefrequency to 5 Hz and the datarecord duration to 10 seconds, */ /* or set the samplefrequency to 1 Hz and the datarecord duration to 2 seconds. */ /* Do not use this function if not necessary. */ int edf_set_micro_datarecord_duration(int handle, int duration); /* Sets the datarecord duration to a very small value. */ /* ATTENTION: the argument "duration" is expressed in units of 1 microSecond! */ /* This function is optional, normally you don't need to change the default value. */ /* The datarecord duration must be in the range 1 to 9999 micro-seconds. */ /* Returns 0 on success, otherwise -1 */ /* This function is NOT REQUIRED but can be called after opening a */ /* file in writemode and before the first sample write action. */ /* This function can be used when you want to use a very high samplerate. */ /* For example, if you want to use a samplerate of 5 GHz, */ /* set the samplefrequency to 5000 Hz and the datarecord duration to 1 micro-second. */ /* Do not use this function if not necessary. */ /* This function was added to accommodate for high speed ADC's e.g. Digital Sampling Oscilloscopes */ int edf_set_number_of_annotation_signals(int handle, int annot_signals); /* Sets the number of annotation signals. The default value is 1 */ /* This function is optional and can be called only after opening a file in writemode */ /* and before the first sample write action */ /* Normally you don't need to change the default value. Only when the number of annotations */ /* you want to write is higher than the number of datarecords in the recording, you can use */ /* this function to increase the storage space for annotations */ /* Minimum is 1, maximum is 64 */ /* Returns 0 on success, otherwise -1 */ #ifdef __cplusplus } /* extern "C" */ #endif #endif PaxHeader/Makefile000644 001750 000144 00000000066 13427767002 014501 xustar00guvusers000000 000000 30 mtime=1549790722.799483582 24 SCHILY.fflags=extent Makefile000644 001750 000144 00000000441 13427767002 012525 0ustar00guvusers000000 000000 CC = gcc CFLAGS = -O2 -Wall -Wextra -Wshadow -Wformat-nonliteral -Wformat-security -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE LDLIBS = -lm programs = sine_generator sweep_generator test_edflib test_generator all: $(programs) $(programs): edflib.o clean: $(RM) *.o $(programs) *.[be]df PaxHeader/README.md000644 001750 000144 00000000066 13437536347 014327 xustar00guvusers000000 000000 30 mtime=1551809767.412537167 24 SCHILY.fflags=extent README.md000644 001750 000144 00000012703 13437536347 012357 0ustar00guvusers000000 000000 # EDFlib EDFlib is a programming library for C/C++ for reading and writing EDF+ and BDF+ files. It also reads "old style" EDF and BDF files. EDF means European Data Format. BDF is the 24-bits version of EDF. ## Usage The library consists of only two files: `edflib.h` and `edflib.c`. In order to use EDFlib, copy these two files to your project. Include the file `edflib.h` in every source file from where you want to access the library. Don't forget to tell your compiler that it must compile and link `edflib.c` (add it to your makefile or buildscript). `edflib.c` needs to be compiled with the options `-D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE`. For example: `gcc -Wall -Wextra -Wshadow -Wformat-nonliteral -Wformat-security -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE test_edflib.c edflib.c -lm -o test_edflib` Compilation has been tested using GCC on Linux, Mingw-w64 on Windows, and LLVM GCC on OS X (Yosemite). To understand how to use the library, read the comments in `edflib.h`. ## Examples To build the examples: `make` Each "generator" example creates an EDF+ or BDF+ file with sample signals. `test_generator` shows how to use most of the functions provided by the library and generates an EDF+ or BDF+ testfile with several sample signals. `sine_generator` creates a BDF+ file containing the signal "sine", a 1 Hz sinoidal waveform with a sample frequency of 2048 Hz. `sweep_generator` creates a linear or logarithmic sweep through a range of frequencies in EDF+ or BDF+ format. Use EDFbrowser to view these files: http://www.teuniz.net/edfbrowser/ `test_edflib ` will print the properties of the EDF/BDF header, the annotations, and the values of 200 samples of the chosen signal. For example, running `test_generator` will produce the file `test_generator.edf`. Running `test_edflib test_generator.edf 6` will show the header and first 200 samples of the "noise" signal: `75 6 27 77 37 30 35 96 62 69 34 15 51 56 69 68 80 45 ...` ## Background info In EDF, the sensitivity (e.g. uV/bit) and offset are stored using four parameters: digital maximum and minimum, and physical maximum and minimum. Here, digital means the raw data coming from a sensor or ADC. Physical means the units like uV. The sensitivity in units/bit is calculated as follows: units per bit = (physical max - physical min) / (digital max - digital min) The digital offset is calculated as follows: offset = (physical max / units per bit) - digital max For a better explanation about the relation between digital data and physical data, read the document "Coding Schemes Used with Data Converters" (PDF): http://www.ti.com/general/docs/lit/getliterature.tsp?baseLiteratureNumber=sbaa042 An EDF file usually contains multiple so-called datarecords. One datarecord usually has a duration of one second (this is the default but it is not mandatory!). In that case a file with a duration of five minutes contains 300 datarecords. The duration of a datarecord can be freely choosen but, if possible, use values from 0.1 to 1 second for easier handling. Just make sure that the total size of one datarecord, expressed in bytes, does not exceed 10MByte (15MBytes for BDF(+)). The RECOMMENDATION of a maximum datarecordsize of 61440 bytes in the EDF and EDF+ specification was usefull in the time people were still using DOS as their main operating system. Using DOS and fast (near) pointers (16-bit pointers), the maximum allocatable block of memory was 64KByte. This is not a concern anymore so the maximum datarecord size now is limited to 10MByte for EDF(+) and 15MByte for BDF(+). This helps to accommodate for higher samplingrates used by modern Analog to Digital Converters. EDF header character encoding: The EDF specification says that only ASCII characters are allowed. EDFlib will automatically convert characters with accents, umlauts, tilde, etc. to their "normal" equivalent without the accent/umlaut/tilde/etc. The description/name of an EDF+ annotation on the other hand, is encoded in UTF-8. ## License Copyright (c) 2009 - 2019 Teunis van Beelen All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PaxHeader/sine_generator.c000644 001750 000144 00000000066 13473031610 016177 xustar00guvusers000000 000000 30 mtime=1558983560.610725804 24 SCHILY.fflags=extent sine_generator.c000644 001750 000144 00000010421 13473031610 014222 0ustar00guvusers000000 000000 /* ***************************************************************************** * * Copyright (c) 2009 - 2019 Teunis van Beelen * All rights reserved. * * Email: teuniz@protonmail.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY Teunis van Beelen ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL Teunis van Beelen BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ***************************************************************************** */ #include #include #include #include "edflib.h" #define SMP_FREQ 2048 int main(void) { int i, j, hdl, buf2[SMP_FREQ], chns; double buf[SMP_FREQ], q; chns = 2; hdl = edfopen_file_writeonly("sine.bdf", EDFLIB_FILETYPE_BDFPLUS, chns); if(hdl<0) { printf("error: edfopen_file_writeonly()\n"); return(1); } for(i=0; i #include #include #include "edflib.h" // Uncomment the next line to create a BSF+ file instead of EDF+: //#define BDF_FORMAT void remove_trailing_zeros(char *); int main(void) { int i, j, hdl, chns=1, smp_freq=8192, fileduration=300, linear=0; double buf[smp_freq], q, sine_1, startfreq=10.0, stopfreq=4095.0, freqspan, freq; long long samples, sampleswritten; char str[256]; #if defined(__APPLE__) || defined(__MACH__) || defined(__APPLE_CC__) #define expo __exp10 #else #define expo exp10 #endif #ifdef BDF_FORMAT hdl = edfopen_file_writeonly("freq_sweep.bdf", EDFLIB_FILETYPE_BDFPLUS, chns); #else hdl = edfopen_file_writeonly("freq_sweep.edf", EDFLIB_FILETYPE_EDFPLUS, chns); #endif if(hdl<0) { printf("error: edfopen_file_writeonly()\n"); return(1); } for(i=0; i '9')) { if(decimalzerofound) { if(str[i-decimalzerofound-1] == '.') { decimalzerofound++; } for(j=i; j<(len+1); j++) { str[j-decimalzerofound] = str[j]; } trailingzerofound = 1; break; } if(str[i] != '.') { numberfound = 0; dotfound = 0; decimalzerofound = 0; } } else { numberfound = 1; if(str[i] > '0') { decimalzerofound = 0; } } if((str[i] == '.') && numberfound) { dotfound = 1; } if((str[i] == '0') && dotfound) { decimalzerofound++; } } } if(decimalzerofound) { if(str[i-decimalzerofound-1] == '.') { decimalzerofound++; } for(j=i; j<(len+1); j++) { str[j-decimalzerofound] = str[j]; } } } PaxHeader/test_edflib.c000644 001750 000144 00000000066 13473031673 015470 xustar00guvusers000000 000000 30 mtime=1558983611.082576195 24 SCHILY.fflags=extent test_edflib.c000644 001750 000144 00000016551 13473031673 013525 0ustar00guvusers000000 000000 /* ***************************************************************************** * * Copyright (c) 2009 - 2019 Teunis van Beelen * All rights reserved. * * Email: teuniz@protonmail.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY Teunis van Beelen ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL Teunis van Beelen BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ***************************************************************************** */ #include #include #include "edflib.h" int main(int argc, char *argv[]) { int i, hdl, channel, n; double *buf; struct edf_hdr_struct hdr; if(argc!=3) { printf("\nusage: test_edflib \n\n"); return(1); } channel = atoi(argv[2]); if(channel<1) { printf("\nsignalnumber must be > 0\n\n"); return(1); } if(edfopen_file_readonly(argv[1], &hdr, EDFLIB_READ_ALL_ANNOTATIONS)) { switch(hdr.filetype) { case EDFLIB_MALLOC_ERROR : printf("\nmalloc error\n\n"); break; case EDFLIB_NO_SUCH_FILE_OR_DIRECTORY : printf("\ncan not open file, no such file or directory\n\n"); break; case EDFLIB_FILE_CONTAINS_FORMAT_ERRORS : printf("\nthe file is not EDF(+) or BDF(+) compliant\n" "(it contains format errors)\n\n"); break; case EDFLIB_MAXFILES_REACHED : printf("\nto many files opened\n\n"); break; case EDFLIB_FILE_READ_ERROR : printf("\na read error occurred\n\n"); break; case EDFLIB_FILE_ALREADY_OPENED : printf("\nfile has already been opened\n\n"); break; default : printf("\nunknown error\n\n"); break; } return(1); } hdl = hdr.handle; if(channel>(hdr.edfsignals)) { printf("\nerror: file has %i signals and you selected signal %i\n\n", hdr.edfsignals, channel); edfclose_file(hdl); return(1); } channel--; printf("\nlibrary version: %i.%02i\n", edflib_version() / 100, edflib_version() % 100); printf("\ngeneral header:\n\n"); printf("filetype: %i\n", hdr.filetype); printf("edfsignals: %i\n", hdr.edfsignals); #ifdef WIN32 printf("file duration: %I64d seconds\n", hdr.file_duration / EDFLIB_TIME_DIMENSION); #else printf("file duration: %lli seconds\n", hdr.file_duration / EDFLIB_TIME_DIMENSION); #endif printf("startdate: %i-%i-%i\n", hdr.startdate_day, hdr.startdate_month, hdr.startdate_year); printf("starttime: %i:%02i:%02i\n", hdr.starttime_hour, hdr.starttime_minute, hdr.starttime_second); printf("patient: %s\n", hdr.patient); printf("recording: %s\n", hdr.recording); printf("patientcode: %s\n", hdr.patientcode); printf("gender: %s\n", hdr.gender); printf("birthdate: %s\n", hdr.birthdate); printf("patient_name: %s\n", hdr.patient_name); printf("patient_additional: %s\n", hdr.patient_additional); printf("admincode: %s\n", hdr.admincode); printf("technician: %s\n", hdr.technician); printf("equipment: %s\n", hdr.equipment); printf("recording_additional: %s\n", hdr.recording_additional); printf("datarecord duration: %f seconds\n", ((double)hdr.datarecord_duration) / EDFLIB_TIME_DIMENSION); #ifdef WIN32 printf("number of datarecords in the file: %I64d\n", hdr.datarecords_in_file); printf("number of annotations in the file: %I64d\n", hdr.annotations_in_file); #else printf("number of datarecords in the file: %lli\n", hdr.datarecords_in_file); printf("number of annotations in the file: %lli\n", hdr.annotations_in_file); #endif printf("\nsignal parameters:\n\n"); printf("label: %s\n", hdr.signalparam[channel].label); #ifdef WIN32 printf("samples in file: %I64d\n", hdr.signalparam[channel].smp_in_file); #else printf("samples in file: %lli\n", hdr.signalparam[channel].smp_in_file); #endif printf("samples in datarecord: %i\n", hdr.signalparam[channel].smp_in_datarecord); printf("physical maximum: %f\n", hdr.signalparam[channel].phys_max); printf("physical minimum: %f\n", hdr.signalparam[channel].phys_min); printf("digital maximum: %i\n", hdr.signalparam[channel].dig_max); printf("digital minimum: %i\n", hdr.signalparam[channel].dig_min); printf("physical dimension: %s\n", hdr.signalparam[channel].physdimension); printf("prefilter: %s\n", hdr.signalparam[channel].prefilter); printf("transducer: %s\n", hdr.signalparam[channel].transducer); printf("samplefrequency: %f\n", ((double)hdr.signalparam[channel].smp_in_datarecord / (double)hdr.datarecord_duration) * EDFLIB_TIME_DIMENSION); struct edf_annotation_struct annot; printf("\n"); for(i=0; i #include #include #include "edflib.h" #define SMP_FREQ 200 #define SMP_FREQ_2 256 #define SMP_FREQ_3 217 #define FILE_DURATION 600 // Uncomment the next line to create a BDF+ file instead of EDF+: // #define BDF_FORMAT int main(void) { int i, j, hdl, chns; double buf[1000], q, sine_1, sine_8, sine_81777, sine_85, sine_15, sine_17, sine_50; struct{ long long samples; long long triggers[512]; int index; int code; int bitposition; int smp_in_bit; } dc_event_stat; memset(&dc_event_stat, 0, sizeof(dc_event_stat)); dc_event_stat.code = 0; dc_event_stat.triggers[0] = 1951; for(i=1; i<512; i++) { dc_event_stat.triggers[i] = (i * 1667) + 1951; } chns = 14; #ifdef BDF_FORMAT hdl = edfopen_file_writeonly("test_generator.bdf", EDFLIB_FILETYPE_BDFPLUS, chns); #else hdl = edfopen_file_writeonly("test_generator.edf", EDFLIB_FILETYPE_EDFPLUS, chns); #endif if(hdl<0) { printf("error: edfopen_file_writeonly()\n"); return(1); } for(i=0; i= 10) { dc_event_stat.smp_in_bit = 0; dc_event_stat.bitposition++; } if(dc_event_stat.bitposition > 10) { dc_event_stat.bitposition = 0; dc_event_stat.smp_in_bit = 0; dc_event_stat.code++; dc_event_stat.code &= 255; if(++dc_event_stat.index >= 512) { dc_event_stat.index = 0; dc_event_stat.code = 0; } } } else { if(dc_event_stat.samples == dc_event_stat.triggers[dc_event_stat.index]) { /* edfwrite_annotation_latin1(hdl, dc_event_stat.samples * 10LL, -1LL, "Trigger"); */ dc_event_stat.bitposition = 1; dc_event_stat.smp_in_bit = 1; buf[i] = 1.0; } else { buf[i] = 0.0; } } dc_event_stat.samples++; } if(edfwrite_physical_samples(hdl, buf)) { printf("error: edfwrite_physical_samples()\n"); return(1); } } edfwrite_annotation_latin1(hdl, 0LL, -1LL, "Recording starts"); edfwrite_annotation_latin1(hdl, 2980000LL, -1LL, "Test 1"); edfwrite_annotation_latin1(hdl, 2940000LL + (long long)((10000.0 / SMP_FREQ) * (SMP_FREQ - 2)), -1LL, "pulse 1"); edfwrite_annotation_latin1(hdl, 2950000LL + (long long)((10000.0 / SMP_FREQ_2) * (SMP_FREQ_2 - 2)), -1LL, "pulse 2"); edfwrite_annotation_latin1(hdl, 2960000LL + (long long)((10000.0 / SMP_FREQ_3) * (SMP_FREQ_3 - 2)), -1LL, "pulse 3"); edfwrite_annotation_latin1(hdl, FILE_DURATION * 10000LL, -1LL, "Recording ends"); edfclose_file(hdl); return(0); }