/*********************************************************************************************************************** This file contains the C/C++ source code (using Borland C++ compiler) for complexity analysis of one trace of EEG signal und DOS. It is just an example. The sampling frequency (fs) of the original EEG data is 200Hz. EEG data stored in the following format (integer (INT) type (two bytes)): mon, day, year,hour, min, sec. hundsec(1/100sec),fs, // header (first 16 bytes): date, starting time, and fs data1, data2,data3,data4,...,,dataN-1,dataN, // acquired EEG data, one by one hour, min, sec, hundsec. // ender (last 8 bytes): ending time of the EEG recording In our study, we stored 30 min-long EEG data into one file, then another 30 min-long data into another file, and so on, until the end of one case. The files are named by time. After complexity analysis, all the C(n) values belong to one case are saved into one file in the order of time. The format of the Complexity C(n) file to be created is as follows: mon, day, year, hr, min, sec, hundsec, fs //header:(8 int type data (starting date, time, and fs)) time to the start (in sec. float type(4-byte)), complexity measure(float type(4-byte))//time and C(n) value time to the start (in sec. float type), complexity measure(float type) //time and C(n) value .........and so on. For running this program, you should minorly modify it to read your EEG data file. Created by Xu-Sheng Zhang on Jan.05, 1998. For detailed priciple of the complexity analysis, see references: [1]Lempel A, Ziv J, IEEE Trans. on Information Theory 1976;IT-22:75-81. [2]Kaspar F, Schuster HG, Phys. Rev. A. 1987; 36:842-848. Disclaimer: This program is provided as it is and it is just an example for EEG analysis. The author is not responsible for any damage, directly or indirectly,caused by this program. Copyright(C) 1998-2000 by Xu-Sheng Zhang and Rob J Roy, Rensselaer Polytechnic Institute Biomedical Engineering 110 Eighth St. Troy, NY 12180 royr@rpi.edu robjroy@worldnet.att.net Permission is granted to modify and re-distribute this code in any manner as long as this notice is preserved. All standard disclaimers apply. ****************************************************************************************************************************/ #include #include #include #include #include #include #include #include #include void main( ) { unsigned i,k,kk,l,kmax,n,c;//some variables for calculating C(n) float cn,ftem; float *x=new float[6800];//to store EEG data char *s=new char[6800]; //to store 0-1 string int item; float fs;//sampling frequency int iii;//EEG file counter float ftotal, cntotal=0.; char fname00[80]="d:\\eegdata\\", //EEG data directory file[64], fname[80]; long cpold, cpoldold, timenew=0, timeold=0, timediff=0;//data counter and time variables int lp;//data pointer int nn1=6000;//Window Length int ft=5;//i.e. float(1000)/fs, shift forward 1000 data points for next C(n) calculation; FILE *fpf;//filename contains all the EEG data files belonging to one case if((fpf=fopen("filename.txt","r"))==NULL) { printf("\nCannot open input filename file\n"); exit(1); } printf("\n\n\r Input the output complexity file name (c****.***):"); scanf("%s", &file); printf("\n\r The file name is: %s", file); strcpy(fname, fname00);//save the C(n) data under the EEG data directory strcat(fname, file); fstream fpc(fname,ios::out|ios::binary|ios::trunc);//open file to save C(n) if(!fpc) {cerr<<"\n\r Can't open the complexity data file !"<ftotal) s[i]=1; else s[i]=0; } kk=0; n=nn1; do{ c=1; l=1; L10:i=0; k=1; kmax=1; L11:if(s[i+k]==s[l+k]) { k=k+1; if(l+k>n) { c=c+1; cn=float(c)*log(float(n))/(float(n)*log(2.0));//normalization kk+=1; continue;// return; } else goto L11; } else { if(k>kmax) kmax=k; i=i+1; if(i==l) { c=c+1; l=l+kmax; if(l+1>n) { cn=float(c)*log(float(n))/(float(n)*log(2.0));//normalization kk+=1; continue;//return; } else goto L10; } else { k=1; goto L11; } } }while (kk<1); lp++; ftem=float(timediff+lp*ft);//seconds to starting-time fpc.write((char *)&ftem,sizeof(float));//store the time into C(n) file fpc.write((char *)&cn, sizeof(float));//stroe the C(n) value into C(n) file cpoldold=finput.tellg(); finput.seekg(-10000,ios::cur);//move back (6000-1000), i.e. move forward 1000 int data from current pointer cntotal+=cn; if(lp%10==0) //every 10 C(n) values, display one average value { cntotal/=10.; printf(" No. %d..... %f\n", lp,cntotal); cntotal=0.; } } while(cpold-cpoldold>=2000); printf("%d %s is finished\n\n",iii, file); finput.close();//close current EEG data file } fpc.close();//close the C(n) file fclose(fpf);//close the EEG data file-name file delete x,s; printf("/n/n Press Any Key to Return to Dos!"); return; }