#include "stdio.h" #include "stdlib.h" #include "math.h" #include "time.h" #include "sethead.h" /* Minimal conversion program for continuous EEG files The output file is as follows First line: nchannels adcrate nevents nsamples Second line: "sample event elec_1 elec_2 .... elec_nchannels" Rest of file: sample event# sample_1 sample_2 .... sample_nchannels Be warned: check whether the machine being used is big or small endian Also check the size of short and int, shoult be 2 and 4 bytes respectively */ #define TRUE 1 #define FALSE 0 #define SWAPENDIAN FALSE #define MAXEVENT 1000 int event[MAXEVENT]; int nevent = 0; int main(argc,argv) int argc; char **argv; { int i,j,readok,foundevent,showheader = TRUE; short sample; short unsigned nchannels; short unsigned adc; int c,filesize=0; int nsamples; int eventoffset,offset,eventsize; char ename[16]; int eventtype; FILE *fptr; /* Do we have enough arguments */ if (argc < 2) { fprintf(stderr,"Usage: %s cntfile [-h]\n",argv[0]); exit(0); } /* Do we want to show the header ? */ if (argc >= 3) { if (strcmp(argv[2],"-h") == 0) showheader = FALSE; } /* Attempt to open the continuous EEG file */ if ((fptr = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"Unable to open file <%s>\n",argv[1]); exit(0); } /* Calculate the file size */ while ((c = fgetc(fptr)) != EOF) filesize++; fseek(fptr,0L,SEEK_SET); /* Read the number of channels */ fseek(fptr,370L,SEEK_SET); readok = ReadUShort(fptr,&nchannels,SWAPENDIAN); if (!readok || nchannels < 1 || nchannels > 64) { fprintf(stderr,"Unexpected number of channels <%d>\n",nchannels); exit(0); } if (showheader) printf("%d ",nchannels); /* Read the ADC rate */ fseek(fptr,376L,SEEK_SET); readok = ReadUShort(fptr,&adc,SWAPENDIAN); if (!readok || adc < 1) { fprintf(stderr,"Unexpected error reading adc rate\n",adc); exit(0); } if (showheader) printf("%d ",adc); /* Read the event table offset */ fseek(fptr,886L,SEEK_SET); readok = ReadInt(fptr,&eventoffset,SWAPENDIAN); if (!readok || eventoffset > filesize) { fprintf(stderr,"Event offset %ld is greater than the file size\n", eventoffset); exit(0); } /* Read the events */ fseek(fptr,eventoffset,SEEK_SET); eventtype = fgetc(fptr); if (eventtype < 1 || eventtype > 2) { fprintf(stderr,"Unexpected eventtype <%d>\n",eventtype); exit(0); } readok = ReadInt(fptr,&eventsize,SWAPENDIAN); if (!readok || eventsize < 0) { fprintf(stderr,"Unexpected event size <%ld>\n",eventsize); exit(0); } if (eventtype == 1) nevent = eventsize / 8; else nevent = eventsize / 19; for (i=0;i\n",offset); exit(0); } event[i] = (offset - 900 - 75 * nchannels) / (nchannels * 2); if (event[i] < 0) { fprintf(stderr,"Unexpected event sample <%ld>\n",event[i]); exit(0); } } if (showheader) printf("%d ",nevent); /* Calculate the number of expected samples */ nsamples = (eventoffset - (900L + 75 * nchannels)) / (2 * nchannels); if (showheader) printf("%ld\n",nsamples); if (nsamples < 1) { fprintf(stderr,"Unexpected number of samples <%ld>\n",nsamples); exit(0); } /* Read the electrode names */ if (showheader) printf("sample event "); for (i=0;i