#include "stdio.h" #include "stdlib.h" #include "math.h" /* Minimal conversion program of Westmead EEG files to ascii 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 Note: this program is more complicated than it woul dneed to be if it was running from a PeeCee (or at least from a machine with the same byte ordering. In that case the binary data coule be read with a single "fread()". This code is primarily for UNIX machines with the opposite byte ordering to PeeCees, for example, an SGI. */ /* Prototytpes */ int ReadShort(FILE *, short int *, int); void SkipLine(FILE *); #define TRUE 1 #define FALSE 0 int main(int argc,char **argv) { int c,i,j,readok,offset,nevent=0,nchannels,adc; short event,channels[200]; long filesize=0; long nsamples; char s[128],label[128]; FILE *fptr; /* Do we have enough arguments */ if (argc < 2) { fprintf(stderr,"Usage: %s filename\n",argv[0]); exit(0); } /* Attempt to open the file */ if ((fptr = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"Unable to open file <%s>\n",argv[1]); exit(0); } /* Calculate the file size. There are better "non portable" ways of doing this */ while ((c = fgetc(fptr)) != EOF) filesize++; /* Check the header label should be "EEG2" */ rewind(fptr); for (i=0;i<32;i++) { s[i] = fgetc(fptr); s[i+1] = '\0'; } if (sscanf(s,"%s %d",label,&offset) != 2) { fprintf(stderr,"Failed to read identifer and offset\n"); exit(0); } /* Read the number of channels */ rewind(fptr); nchannels = 0; while (fscanf(fptr,"%s",s) == 1) { if (strstr(s,"NumChans") != NULL) { SkipLine(fptr); SkipLine(fptr); SkipLine(fptr); SkipLine(fptr); fscanf(fptr,"%s %d",s,&nchannels); break; } } nchannels--; /* Last channel holds the events */ if (nchannels < 10) { fprintf(stderr,"Unexpected number of channels <%d>\n",nchannels); exit(0); } printf("%d ",nchannels); /* Read the ADC rate */ rewind(fptr); adc = 0; while (fscanf(fptr,"%s",s) == 1) { if (strstr(s,"[SamRate") != NULL) { SkipLine(fptr); SkipLine(fptr); SkipLine(fptr); SkipLine(fptr); fscanf(fptr,"%s %d",s,&adc); break; } } if (adc < 10) { fprintf(stderr,"Unexpected ADC rate <%d>\n",adc); exit(0); } printf("%d ",adc); /* Determine the number of events, not done correctly! */ printf("%d ",nevent); /* Calculate the number of expected samples */ nsamples = (filesize - 512*offset) / (2*(nchannels + 1)); printf("%ld\n",nsamples); if (nsamples < 1) { fprintf(stderr,"Unexpected number of samples <%ld>\n",nsamples); exit(0); } /* Read the electrode names */ printf("sample event "); rewind(fptr); while (fscanf(fptr,"%s",s) == 1) { if (strstr(s,"[Label") != NULL) { SkipLine(fptr); SkipLine(fptr); SkipLine(fptr); SkipLine(fptr); for (i=0;i