www.pudn.com > ica_v0.04.rar > io.c
/* io.c Time-stamp:Routines for reading and writing to files. Programmer: Peter Stepien pstepien@sedal.usyd.edu.au CEL, School of Electrical & Information Engineering The University of Sydney SYDNEY NSW 2006 Australia COPYRIGHT 1997-2001 */ /* System headers */ #include #include #include #include #include /* Local headers */ #include "ica.h" /* Input file information if it is EDF */ int EDF_header_size; int EDF_num_records; int EDF_num_signals; int EDF_samples_per_record; int EDF_current_record; int EDF_current_sample; float EDF_offset[EDF_MAX_SIGNALS]; float EDF_scale[EDF_MAX_SIGNALS]; /* Routine to swap bytes in a short. */ void swapbytes(unsigned short *value) { unsigned short temp_short; temp_short = (*value >> 8) & 0xff; *value = (unsigned short)(((*value << 8) & 0xff00) | temp_short); } /* Resets the file pointer to the start of the file. More complicated if the file is an EDF one. Need to then load parameters from the header for scaling and then move to the start of the data. The EDF file must have the same number of samples for each data record. This routine also returns the number of channels so that the users does not manually have to indicate how channels there are since the information is within the file. */ int io_reset_file_pointer(int file, int file_type) { int num_read; char temp_string[100]; EDF_header_struct EDF_header; char *EDF_header_rest; FLOAT_TYPE physical_min; FLOAT_TYPE physical_max; FLOAT_TYPE digital_min; FLOAT_TYPE digital_max; int i; int samples_per_record; lseek(file, 0, SEEK_SET); if (file_type == EDF) { /* Read the header information first */ num_read = read(file, &EDF_header, sizeof(EDF_header)); if (num_read != sizeof(EDF_header)) { printf("ERROR: reading header of EDF file.\n"); exit(-1); } /* Get some global parameters from header */ memcpy(temp_string, &(EDF_header.header_size), sizeof(EDF_header.header_size)); temp_string[sizeof(EDF_header.header_size)] = 0; sscanf(temp_string,"%d", &EDF_header_size); memcpy(temp_string, &(EDF_header.num_records), sizeof(EDF_header.num_records)); temp_string[sizeof(EDF_header.num_records)] = 0; sscanf(temp_string,"%d", &EDF_num_records); memcpy(temp_string, &(EDF_header.num_signals), sizeof(EDF_header.num_signals)); temp_string[sizeof(EDF_header.num_signals)] = 0; sscanf(temp_string,"%d", &EDF_num_signals); #ifdef DEBUG printf("DEBUG: EDF header size = %d\n", EDF_header_size); printf("DEBUG: number of records = %d\n", EDF_num_records); printf("DEBUG: number of signals = %d\n", EDF_num_signals); #endif /* DEBUG */ if (EDF_num_signals > EDF_MAX_SIGNALS) { printf("ERROR: too many signals in EDF file.\n"); exit(-1); } /* Allocate memory for the rest of the header */ if ((EDF_header_rest = (char *)malloc(EDF_header_size-sizeof(EDF_header))) == 0) { printf("ERROR: allocating memory for header.\n"); exit(-1); } /* Read in the rest of the header */ num_read = read(file, EDF_header_rest, EDF_header_size-sizeof(EDF_header)); if (num_read != (EDF_header_size-sizeof(EDF_header))) { printf("ERROR: reading rest of header of EDF file.\n"); exit(-1); } /* Get more information about the samples */ for (i=0;i EDF_samples_per_record) { EDF_current_record++; EDF_current_sample = 1; lseek(file, (EDF_num_signals-1)*EDF_samples_per_record*2, SEEK_CUR); } if (EDF_current_record > EDF_num_records) { num_read = 0; } else { for (i=0;i