/* file: sampen.c Doug Lake 2 August 2002 Last revised: 6 January 2004 (by george@mit.edu) ------------------------------------------------------------------------------- sampen: calculate Sample Entropy Copyright (C) 2002 Doug Lake This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. You may also view the agreement at http://www.fsf.org/copyleft/gpl.html. You may contact the author via electronic mail (dlake@virginia.edu). For updates to this software, please visit PhysioNet (http://www.physionet.org/). _______________________________________________________________________________ Compile this program using any standard C compiler, linking with the standard C math library. For example, if your compiler is gcc, use: gcc -o sampen -O sampen.c -lm For brief instructions, use the '-h' option: sampen -h Additional information is available at: http://www.physionet.org/physiotools/sampen/. */ #include #include #include float *readdata(char *filenm, int *filelen); void sampen(float *y, int mm, float r, int n); void sampen2(float *y, int mm, float r, int n); void normalize(float *data, int n); void help(void); int main(int argc, char* argv[]) { float *data = NULL; float r = .2; int n, j, mm = 2; char *filenm=NULL; int nflag = 0; int vflag = 0; int i; for (i = 1; i < argc; i++) /* Read input and optional arguments. */ { if(argv[i][0] == '-') { switch(argv[i][1]) { case 'h': help(); exit(0); break; case 'r': if(argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } if(i+1 < argc) { i++; r = atof(argv[i]); break; } fprintf(stderr, "sampen: No value specified for option '-t'\n"); help(); exit(1); break; case 'm': if(argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } if(i+1 < argc) { i++; mm = atoi(argv[i]); break; } fprintf(stderr, "sampen: No value specified for option '-t'\n"); help(); exit(1); break; case 'n': if(argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } nflag = 1; break; case 'v': if(argv[i][2] != '\0') { fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } vflag = 1; break; default: fprintf(stderr, "sampen: Unrecognized option '%s'\n", argv[i]); help(); exit(1); } } else { if(!filenm) filenm = argv[i]; else { fprintf(stderr, "sampen: Too many inputs\n"); exit(1); } } } if(!filenm) filenm = "-"; /* use the standard input if no input file specified */ data=readdata(filenm, &n); if(mm > n/2) {fprintf(stderr, "sampen: m too large for time series of length %d\n", n); exit(1);} if(nflag) normalize(data, n); if(vflag) sampen2(data,mm,r,n); else sampen(data, mm, r, n); free(data); return 0; } float *readdata(char *filenm, int *filelen) { FILE *ifile; long maxdat = 0L, npts = 0L; float *data = NULL, y, yp = 0.0; if (strcmp(filenm, "-") == 0) { filenm = "standard input"; ifile = stdin; } else if ((ifile = fopen(filenm, "rt")) == NULL) { fprintf(stderr, "sampen: Could not open %s \n", filenm); exit(1); } while (fscanf(ifile, "%f", &y) == 1) { if (++npts >= maxdat) { float *s; maxdat += 5000; /* allow the input buffer to grow (the increment is arbitrary) */ if ((s = realloc(data, maxdat * sizeof(float))) == NULL) { fprintf(stderr, "sampen: insufficient memory, truncating input at row %d\n", npts); break; } data = s; } data[npts] = y; } fclose(ifile); if (npts < 1) { fprintf(stderr, "sampen: %s contains no data\n", filenm); help(); exit(1); } *filelen = npts; return (data); } void normalize(float *data, int n) /* subtracts the mean from data, then divides data by its variance. */ { int i; float mean = 0; float var = 0; for (i = 0; i < n; i++) mean += data[i]; mean = mean/n; for (i = 0; i < n; i++) data[i] = data[i]-mean; for (i = 0; i MM-1) for (j=MM; j 0; m--) B[m]=B[m-1]; B[0]= (float)n*(n-1)/2; for (m=0; m=m+1) nm1--; if (R2[i1+n*j] >=m+1) nm4--; } for (j=0; j<2*(d+1); j++) if (R2[i1+n*j]>=m+1) nm2--; for (j=0; j<(2*d+1); j++) if (R1[i2+n*j] >=m+1) nm3--; K[d+1+(mm+1)*m]+=(float)2*(nm1+nm2)*(nm3+nm4); } } } n1[0] = (float)n*(n-1)*(n-2); for (m=0; m0) v1[m]+=dv; s1[m]=(float)sqrt((double)(v1[m])); } for (m=0; m