/* * This file is part of TISEAN * * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber * * TISEAN 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. * * TISEAN 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 TISEAN; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ /*Author: Rainer Hegger. Last modified May 10, 2000 */ #include #include #include #include #include #include #include "routines/tsa.h" #define WID_STR "Estimates the correlation sum, -dimension and -entropy" /* output is written every WHEN seconds */ #define WHEN 120 /* Size of the field for box assisted neighbour searching (has to be a power of 2)*/ #define NMAX 256 /* Size of the box for the scramble routine */ #define SCBOX 4096 double **series; long *scr; char dimset=0,rescale_set=0,eps_min_set=0,eps_max_set=0; char *FOUT=NULL; double epsfactor,epsinv,lneps,lnfac; double EPSMAX=1.0,EPSMIN=1.e-3; double min,interval; int imax=NMAX-1,howoften1,imin; long box[NMAX][NMAX],*list,boxc1[NMAX],*listc1; unsigned long nmax; double **found,*norm; unsigned long MINDIST=0,MAXFOUND=1000; unsigned long length=ULONG_MAX,exclude=0; unsigned int DIM=1,EMBED=10,HOWOFTEN=100,DELAY=1; unsigned int verbosity=0x1; char *column=NULL; char *infile=NULL; void show_options(char *progname) { what_i_do(progname,WID_STR); fprintf(stderr," Usage: %s [options]\n",progname); fprintf(stderr," Options:\n"); fprintf(stderr,"Everything not being a valid option will be interpreted" " as a possible" " datafile.\nIf no datafile is given stdin is read. Just - also" " means stdin\n"); fprintf(stderr,"\t-l datapoints [default is whole file]\n"); fprintf(stderr,"\t-x exclude # points [default 0]\n"); fprintf(stderr,"\t-d delay [default 1]\n"); fprintf(stderr,"\t-M # of components, max. embedding dim. [default 1,10]\n"); fprintf(stderr,"\t-c columns [default 1,...,# of components]\n"); fprintf(stderr,"\t-t theiler-window [default 0]\n"); fprintf(stderr,"\t-R max-epsilon " "[default: max data interval]\n"); fprintf(stderr,"\t-r min-epsilon [default: (max data interval)/1000]\n"); fprintf(stderr,"\t-# #-of-epsilons [default 100]\n"); fprintf(stderr,"\t-N max-#-of-pairs (0 means all) [default 1000]\n"); fprintf(stderr,"\t-E use rescaled data [default: not rescaled]\n"); fprintf(stderr," \t-o outfiles" " [without exts.! default datafile[.d2][.h2][.stat][.c2]]\n"); fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t" "0='only panic messages'\n\t\t" "1='+ input/output messages'\n\t\t" "2='+ output message each time output is done\n"); fprintf(stderr,"\t-h show these options\n"); fprintf(stderr,"\n"); exit(0); } void scan_options(int n,char **argv) { char *out; if ((out=check_option(argv,n,'l','u')) != NULL) sscanf(out,"%lu",&length); if ((out=check_option(argv,n,'x','u')) != NULL) sscanf(out,"%lu",&exclude); if ((out=check_option(argv,n,'c','s')) != NULL) column=out; if ((out=check_option(argv,n,'d','u')) != NULL) sscanf(out,"%u",&DELAY); if ((out=check_option(argv,n,'M','2')) != NULL) { sscanf(out,"%u,%u",&DIM,&EMBED); dimset=1; } if ((out=check_option(argv,n,'t','u')) != NULL) sscanf(out,"%lu",&MINDIST); if ((out=check_option(argv,n,'R','f')) != NULL) { sscanf(out,"%lf",&EPSMAX); eps_max_set=1; } if ((out=check_option(argv,n,'r','f')) != NULL) { sscanf(out,"%lf",&EPSMIN); eps_min_set=1; } if ((out=check_option(argv,n,'#','u')) != NULL) sscanf(out,"%u",&HOWOFTEN); if ((out=check_option(argv,n,'N','u')) != NULL) { sscanf(out,"%lu",&MAXFOUND); if (MAXFOUND == 0) MAXFOUND=ULONG_MAX; } if ((out=check_option(argv,n,'E','n')) != NULL) rescale_set=1; if ((out=check_option(argv,n,'V','u')) != NULL) sscanf(out,"%u",&verbosity); if ((out=check_option(argv,n,'o','o')) != NULL) if (strlen(out) > 0) FOUT=out; } void scramble(void) { long i,j,k,m; unsigned long rnd,rndf,hlength,allscr=0; long *scfound,*scnhelp,scnfound; long scbox[SCBOX],lswap,element,scbox1=SCBOX-1; double *rz,*schelp,sceps=(double)SCBOX-0.001,swap; hlength=length-(EMBED-1)*DELAY; if (sizeof(long) == 8) { rndf=13*13*13*13; rndf=rndf*rndf*rndf*13; rnd=0x849178L; } else { rndf=69069; rnd=0x234571L; } for (i=0;i<1000;i++) rnd=rnd*rndf+1; check_alloc(rz=(double*)malloc(sizeof(double)*hlength)); check_alloc(scfound=(long*)malloc(sizeof(long)*hlength)); check_alloc(scnhelp=(long*)malloc(sizeof(long)*hlength)); check_alloc(schelp=(double*)malloc(sizeof(double)*hlength)); for (i=0;i MINDIST) { count=0; max=0.0; maxi=howoften1; small=0; for (i=0;i max) { max=dx; if (max < EPSMIN) { maxi=howoften1; } else { maxi=(lneps-log(max))/lnfac; } } if (count > 0) for (k=imin;k<=maxi;k++) found[count][k] += 1.0; } else { small=1; break; } count++; } if (small) break; } } element=list[element]; } } } free(hs); } void make_c2_1(int n) { int i,x,i1,maxi; long element,n1; double hs,max; n1=scr[n]; hs=series[0][n1]; x=(int)(hs*epsinv)&imax; for (i1=x-1;i1<=x+1;i1++) { element=boxc1[i1&imax]; while (element != -1) { if (labs(element-n1) > MINDIST) { max=fabs(hs-series[0][element]); if (max <= EPSMAX) { if (max < EPSMIN) maxi=howoften1; else maxi=(lneps-log(max))/lnfac; for (i=imin;i<=maxi;i++) found[0][i] += 1.0; } } element=listc1[element]; } } } int main(int argc,char **argv) { char smaller,stdi=0; FILE *fout,*fstat; char *outd1,*outc1,*outh1,*outstat; int maxembed; long i1,j1,x,y,sn,n,i,j,n1,n2; long *oscr; long lnorm; double eps,*epsm,EPSMAX1,maxinterval; time_t mytime,lasttime; if (scan_help(argc,argv)) show_options(argv[0]); scan_options(argc,argv); #ifndef OMIT_WHAT_I_DO if (verbosity&VER_INPUT) what_i_do(argv[0],WID_STR); #endif infile=search_datafile(argc,argv,NULL,verbosity); if (infile == NULL) stdi=1; if (FOUT == NULL) { if (!stdi) { check_alloc(FOUT=calloc(strlen(infile)+1,(size_t)1)); strcpy(FOUT,infile); } else { check_alloc(FOUT=calloc((size_t)6,(size_t)1)); strcpy(FOUT,"stdin"); } } if (column == NULL) series=(double**)get_multi_series(infile,&length,exclude,&DIM,"",dimset, verbosity); else series=(double**)get_multi_series(infile,&length,exclude,&DIM,column, dimset,verbosity); if (rescale_set) { for (i=0;i series[i][j]) min=series[i][j]; if (interval < series[i][j]) interval=series[i][j]; } interval -= min; if (interval > maxinterval) maxinterval=interval; } } if (!eps_max_set) EPSMAX *= maxinterval; if (!eps_min_set) EPSMIN *= maxinterval; EPSMAX=(fabs(EPSMAX) 1) { x=(long)(series[0][sn]*epsinv)&imax; y=(long)(series[1][sn]*epsinv)&imax; } else { x=(long)(series[0][sn]*epsinv)&imax; y=(long)(series[0][sn+DELAY]*epsinv)&imax; } list[sn]=box[x][y]; box[x][y]=sn; listc1[sn]=boxc1[x]; boxc1[x]=sn; i=imin; while (found[maxembed][i] >= MAXFOUND) { smaller=1; if (++i > howoften1) break; } if (smaller) { imin=i; if (imin <= howoften1) { EPSMAX=epsm[imin]; epsinv=1.0/EPSMAX; for (i1=0;i1 1) { x=(long)(series[0][sn]*epsinv)&imax; y=(long)(series[1][sn]*epsinv)&imax; } else { x=(long)(series[0][sn]*epsinv)&imax; y=(long)(series[0][sn+DELAY]*epsinv)&imax; } list[sn]=box[x][y]; box[x][y]=sn; listc1[sn]=boxc1[x]; boxc1[x]=sn; } } } if (imin <= howoften1) { lnorm=n; if (MINDIST > 0) { sn=scr[n]; n1=(sn-(long)MINDIST>=0)?sn-(long)MINDIST:0; n2=(sn+MINDIST 1) make_c2_dim(n); make_c2_1(n); for (i=imin;i WHEN) || (n == (nmax-1)) || (imin > howoften1)) { time(&lasttime); fstat=fopen(outstat,"w"); if (verbosity&VER_USR1) fprintf(stderr,"Opened %s for writing\n",outstat); fprintf(fstat,"Center points treated so far= %ld\n",n); fprintf(fstat,"Maximal epsilon in the moment= %e\n",epsm[imin]); fclose(fstat); fout=fopen(outc1,"w"); if (verbosity&VER_USR1) fprintf(stderr,"Opened %s for writing\n",outc1); fprintf(fout,"#center= %ld\n",n); for (i=0;i 0.0) fprintf(fout,"%e %e\n",eps,found[i][j]/norm[j]); } fprintf(fout,"\n\n"); } fclose(fout); fout=fopen(outh1,"w"); if (verbosity&VER_USR1) fprintf(stderr,"Opened %s for writing\n",outh1); fprintf(fout,"#center= %ld\n",n); fprintf(fout,"#dim= 1\n"); eps=EPSMAX1*epsfactor; for (j=0;j 0.0) fprintf(fout,"%e %e\n",eps,-log(found[0][j]/norm[j])); } fprintf(fout,"\n\n"); for (i=1;i 0.0) && (found[i][j] > 0.0)) fprintf(fout,"%e %e\n",eps,log(found[i-1][j]/found[i][j])); } fprintf(fout,"\n\n"); } fclose(fout); fout=fopen(outd1,"w"); if (verbosity&VER_USR1) fprintf(stderr,"Opened %s for writing\n",outd1); fprintf(fout,"#center= %ld\n",n); for (i=0;i 0.0) && (found[i][j-1] > 0.0)) fprintf(fout,"%e %e\n",eps,log(found[i][j-1]/found[i][j] /norm[j-1]*norm[j])/lnfac); } fprintf(fout,"\n\n"); } fclose(fout); if (imin > howoften1) exit(0); } } if (infile != NULL) free(infile); free(outd1); free(outh1); free(outc1); free(outstat); free(list); free(listc1); free(scr); free(oscr); free(norm); free(epsm); for (i=0;i