2 * This file is part of TISEAN
4 * Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
6 * TISEAN is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * TISEAN is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with TISEAN; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /*Author: Rainer Hegger. Last modified May 10, 2000 */
27 #include "routines/tsa.h"
29 #define WID_STR "Estimates the correlation sum, -dimension and -entropy"
31 /* output is written every WHEN seconds */
33 /* Size of the field for box assisted neighbour searching
34 (has to be a power of 2)*/
36 /* Size of the box for the scramble routine */
41 char dimset=0,rescale_set=0,eps_min_set=0,eps_max_set=0;
43 double epsfactor,epsinv,lneps,lnfac;
44 double EPSMAX=1.0,EPSMIN=1.e-3;
46 int imax=NMAX-1,howoften1,imin;
47 long box[NMAX][NMAX],*list,boxc1[NMAX],*listc1;
50 unsigned long MINDIST=0,MAXFOUND=1000;
51 unsigned long length=ULONG_MAX,exclude=0;
52 unsigned int DIM=1,EMBED=10,HOWOFTEN=100,DELAY=1;
53 unsigned int verbosity=0x1;
57 void show_options(char *progname)
59 what_i_do(progname,WID_STR);
60 fprintf(stderr," Usage: %s [options]\n",progname);
61 fprintf(stderr," Options:\n");
62 fprintf(stderr,"Everything not being a valid option will be interpreted"
64 " datafile.\nIf no datafile is given stdin is read. Just - also"
66 fprintf(stderr,"\t-l datapoints [default is whole file]\n");
67 fprintf(stderr,"\t-x exclude # points [default 0]\n");
68 fprintf(stderr,"\t-d delay [default 1]\n");
69 fprintf(stderr,"\t-M # of components, max. embedding dim. [default 1,10]\n");
70 fprintf(stderr,"\t-c columns [default 1,...,# of components]\n");
71 fprintf(stderr,"\t-t theiler-window [default 0]\n");
72 fprintf(stderr,"\t-R max-epsilon "
73 "[default: max data interval]\n");
74 fprintf(stderr,"\t-r min-epsilon [default: (max data interval)/1000]\n");
75 fprintf(stderr,"\t-# #-of-epsilons [default 100]\n");
76 fprintf(stderr,"\t-N max-#-of-pairs (0 means all) [default 1000]\n");
77 fprintf(stderr,"\t-E use rescaled data [default: not rescaled]\n");
78 fprintf(stderr," \t-o outfiles"
79 " [without exts.! default datafile[.d2][.h2][.stat][.c2]]\n");
80 fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
81 "0='only panic messages'\n\t\t"
82 "1='+ input/output messages'\n\t\t"
83 "2='+ output message each time output is done\n");
85 fprintf(stderr,"\t-h show these options\n");
90 void scan_options(int n,char **argv)
94 if ((out=check_option(argv,n,'l','u')) != NULL)
95 sscanf(out,"%lu",&length);
96 if ((out=check_option(argv,n,'x','u')) != NULL)
97 sscanf(out,"%lu",&exclude);
98 if ((out=check_option(argv,n,'c','s')) != NULL)
100 if ((out=check_option(argv,n,'d','u')) != NULL)
101 sscanf(out,"%u",&DELAY);
102 if ((out=check_option(argv,n,'M','2')) != NULL) {
103 sscanf(out,"%u,%u",&DIM,&EMBED);
106 if ((out=check_option(argv,n,'t','u')) != NULL)
107 sscanf(out,"%lu",&MINDIST);
108 if ((out=check_option(argv,n,'R','f')) != NULL) {
109 sscanf(out,"%lf",&EPSMAX);
112 if ((out=check_option(argv,n,'r','f')) != NULL) {
113 sscanf(out,"%lf",&EPSMIN);
116 if ((out=check_option(argv,n,'#','u')) != NULL)
117 sscanf(out,"%u",&HOWOFTEN);
118 if ((out=check_option(argv,n,'N','u')) != NULL) {
119 sscanf(out,"%lu",&MAXFOUND);
123 if ((out=check_option(argv,n,'E','n')) != NULL)
125 if ((out=check_option(argv,n,'V','u')) != NULL)
126 sscanf(out,"%u",&verbosity);
127 if ((out=check_option(argv,n,'o','o')) != NULL)
135 unsigned long rnd,rndf,hlength,allscr=0;
136 long *scfound,*scnhelp,scnfound;
137 long scbox[SCBOX],lswap,element,scbox1=SCBOX-1;
138 double *rz,*schelp,sceps=(double)SCBOX-0.001,swap;
140 hlength=length-(EMBED-1)*DELAY;
142 if (sizeof(long) == 8) {
144 rndf=rndf*rndf*rndf*13;
154 check_alloc(rz=(double*)malloc(sizeof(double)*hlength));
155 check_alloc(scfound=(long*)malloc(sizeof(long)*hlength));
156 check_alloc(scnhelp=(long*)malloc(sizeof(long)*hlength));
157 check_alloc(schelp=(double*)malloc(sizeof(double)*hlength));
159 for (i=0;i<hlength;i++)
160 rz[i]=(double)(rnd=rnd*rndf+1)/ULONG_MAX;
162 for (i=0;i<SCBOX;i++)
164 for (i=0;i<hlength;i++) {
165 m=(int)(rz[i]*sceps)&scbox1;
169 for (i=0;i<SCBOX;i++) {
172 while(element != -1) {
173 scnhelp[scnfound]=element;
174 schelp[scnfound++]=rz[element];
175 element=scfound[element];
178 for (j=0;j<scnfound-1;j++)
179 for (k=j+1;k<scnfound;k++)
180 if (schelp[k] < schelp[j]) {
185 scnhelp[k]=scnhelp[j];
188 for (j=0;j<scnfound;j++)
189 scr[allscr+j]=scnhelp[j];
198 void make_c2_dim(int n)
201 long i,j,k,x,y,i1,i2,j1,element,n1,maxi,count,hi;
204 check_alloc(hs=(double*)malloc(sizeof(double)*EMBED*DIM));
208 for (i1=0;i1<EMBED;i1++) {
211 hs[count++]=series[j][n1+i2];
214 x=(int)(hs[0]*epsinv)&imax;
215 y=(int)(hs[1]*epsinv)&imax;
217 for (i1=x-1;i1<=x+1;i1++) {
219 for (j1=y-1;j1<=y+1;j1++) {
220 element=box[i2][j1&imax];
221 while (element != -1) {
222 if (labs((long)(element-n1)) > MINDIST) {
227 for (i=0;i<EMBED;i++) {
229 for (j=0;j<DIM;j++) {
230 dx=fabs(hs[count]-series[j][element+hi]);
238 maxi=(lneps-log(max))/lnfac;
242 for (k=imin;k<=maxi;k++)
243 found[count][k] += 1.0;
255 element=list[element];
263 void make_c2_1(int n)
272 x=(int)(hs*epsinv)&imax;
274 for (i1=x-1;i1<=x+1;i1++) {
275 element=boxc1[i1&imax];
276 while (element != -1) {
277 if (labs(element-n1) > MINDIST) {
278 max=fabs(hs-series[0][element]);
283 maxi=(lneps-log(max))/lnfac;
284 for (i=imin;i<=maxi;i++)
288 element=listc1[element];
293 int main(int argc,char **argv)
297 char *outd1,*outc1,*outh1,*outstat;
299 long i1,j1,x,y,sn,n,i,j,n1,n2;
302 double eps,*epsm,EPSMAX1,maxinterval;
303 time_t mytime,lasttime;
305 if (scan_help(argc,argv))
306 show_options(argv[0]);
308 scan_options(argc,argv);
309 #ifndef OMIT_WHAT_I_DO
310 if (verbosity&VER_INPUT)
311 what_i_do(argv[0],WID_STR);
314 infile=search_datafile(argc,argv,NULL,verbosity);
320 check_alloc(FOUT=calloc(strlen(infile)+1,(size_t)1));
324 check_alloc(FOUT=calloc((size_t)6,(size_t)1));
325 strcpy(FOUT,"stdin");
329 series=(double**)get_multi_series(infile,&length,exclude,&DIM,"",dimset,
332 series=(double**)get_multi_series(infile,&length,exclude,&DIM,column,
337 rescale_data(series[i],length,&min,&interval);
342 for (i=0;i<DIM;i++) {
343 min=interval=series[i][0];
344 for (j=1;j<length;j++) {
345 if (min > series[i][j])
347 if (interval < series[i][j])
348 interval=series[i][j];
351 if (interval > maxinterval)
352 maxinterval=interval;
356 EPSMAX *= maxinterval;
358 EPSMIN *= maxinterval;
359 EPSMAX=(fabs(EPSMAX)<maxinterval) ? fabs(EPSMAX) : maxinterval;
360 EPSMIN=(fabs(EPSMIN)<EPSMAX) ? fabs(EPSMIN) : EPSMAX/2.;
363 howoften1=HOWOFTEN-1;
364 maxembed=DIM*EMBED-1;
366 check_alloc(outd1=(char*)calloc(strlen(FOUT)+4,(size_t)1));
367 check_alloc(outc1=(char*)calloc(strlen(FOUT)+4,(size_t)1));
368 check_alloc(outh1=(char*)calloc(strlen(FOUT)+4,(size_t)1));
369 check_alloc(outstat=(char*)calloc(strlen(FOUT)+6,(size_t)1));
373 strcpy(outstat,FOUT);
377 strcat(outstat,".stat");
381 test_outfile(outstat);
383 check_alloc(list=(long*)malloc(length*sizeof(long)));
384 check_alloc(listc1=(long*)malloc(length*sizeof(long)));
385 if ((long)(length-(EMBED-1)*DELAY) <= 0) {
386 fprintf(stderr,"Embedding dimension and delay are too large.\n"
387 "The delay vector would be longer than the whole series."
389 exit(VECTOR_TOO_LARGE_FOR_LENGTH);
391 check_alloc(scr=(long*)malloc(sizeof(long)*(length-(EMBED-1)*DELAY)));
392 check_alloc(oscr=(long*)malloc(sizeof(long)*(length-(EMBED-1)*DELAY)));
393 check_alloc(found=(double**)malloc(DIM*EMBED*sizeof(double*)));
394 for (i=0;i<EMBED*DIM;i++)
395 check_alloc(found[i]=(double*)malloc(HOWOFTEN*sizeof(double)));
396 check_alloc(norm=(double*)malloc(HOWOFTEN*sizeof(double)));
397 check_alloc(epsm=(double*)malloc(HOWOFTEN*sizeof(double)));
400 epsfactor=pow(EPSMAX/EPSMIN,1.0/(double)howoften1);
402 lnfac=log(epsfactor);
406 for (i=1;i<HOWOFTEN;i++) {
408 epsm[i]=epsm[i-1]/epsfactor;
413 for (i=0;i<(length-(EMBED-1)*DELAY);i++)
416 for (i=0;i<DIM*EMBED;i++)
417 for (j=0;j<HOWOFTEN;j++)
420 nmax=length-DELAY*(EMBED-1);
422 for (i1=0;i1<NMAX;i1++) {
424 for (j1=0;j1<NMAX;j1++)
430 for (n=1;n<nmax;n++) {
434 x=(long)(series[0][sn]*epsinv)&imax;
435 y=(long)(series[1][sn]*epsinv)&imax;
438 x=(long)(series[0][sn]*epsinv)&imax;
439 y=(long)(series[0][sn+DELAY]*epsinv)&imax;
447 while (found[maxembed][i] >= MAXFOUND) {
454 if (imin <= howoften1) {
457 for (i1=0;i1<NMAX;i1++) {
459 for (j1=0;j1<NMAX;j1++)
462 for (i1=0;i1<n;i1++) {
465 x=(long)(series[0][sn]*epsinv)&imax;
466 y=(long)(series[1][sn]*epsinv)&imax;
469 x=(long)(series[0][sn]*epsinv)&imax;
470 y=(long)(series[0][sn+DELAY]*epsinv)&imax;
480 if (imin <= howoften1) {
484 n1=(sn-(long)MINDIST>=0)?sn-(long)MINDIST:0;
485 n2=(sn+MINDIST<length-(EMBED-1)*DELAY)?sn+MINDIST:
486 length-(EMBED-1)*DELAY-1;
487 for (i1=n1;i1<=n2;i1++)
495 for (i=imin;i<HOWOFTEN;i++)
496 norm[i] += (double)(lnorm);
499 if (((time(&mytime)-lasttime) > WHEN) || (n == (nmax-1)) ||
500 (imin > howoften1)) {
502 fstat=fopen(outstat,"w");
503 if (verbosity&VER_USR1)
504 fprintf(stderr,"Opened %s for writing\n",outstat);
505 fprintf(fstat,"Center points treated so far= %ld\n",n);
506 fprintf(fstat,"Maximal epsilon in the moment= %e\n",epsm[imin]);
508 fout=fopen(outc1,"w");
509 if (verbosity&VER_USR1)
510 fprintf(stderr,"Opened %s for writing\n",outc1);
511 fprintf(fout,"#center= %ld\n",n);
512 for (i=0;i<EMBED*DIM;i++) {
513 fprintf(fout,"#dim= %ld\n",i+1);
514 eps=EPSMAX1*epsfactor;
515 for (j=0;j<HOWOFTEN;j++) {
518 fprintf(fout,"%e %e\n",eps,found[i][j]/norm[j]);
520 fprintf(fout,"\n\n");
523 fout=fopen(outh1,"w");
524 if (verbosity&VER_USR1)
525 fprintf(stderr,"Opened %s for writing\n",outh1);
526 fprintf(fout,"#center= %ld\n",n);
527 fprintf(fout,"#dim= 1\n");
528 eps=EPSMAX1*epsfactor;
529 for (j=0;j<HOWOFTEN;j++) {
531 if (found[0][j] > 0.0)
532 fprintf(fout,"%e %e\n",eps,-log(found[0][j]/norm[j]));
534 fprintf(fout,"\n\n");
535 for (i=1;i<DIM*EMBED;i++) {
536 fprintf(fout,"#dim= %ld\n",i+1);
537 eps=EPSMAX1*epsfactor;
538 for (j=0;j<HOWOFTEN;j++) {
540 if ((found[i-1][j] > 0.0) && (found[i][j] > 0.0))
541 fprintf(fout,"%e %e\n",eps,log(found[i-1][j]/found[i][j]));
543 fprintf(fout,"\n\n");
546 fout=fopen(outd1,"w");
547 if (verbosity&VER_USR1)
548 fprintf(stderr,"Opened %s for writing\n",outd1);
549 fprintf(fout,"#center= %ld\n",n);
550 for (i=0;i<DIM*EMBED;i++) {
551 fprintf(fout,"#dim= %ld\n",i+1);
553 for (j=1;j<HOWOFTEN;j++) {
555 if ((found[i][j] > 0.0) && (found[i][j-1] > 0.0))
556 fprintf(fout,"%e %e\n",eps,log(found[i][j-1]/found[i][j]
557 /norm[j-1]*norm[j])/lnfac);
559 fprintf(fout,"\n\n");
562 if (imin > howoften1)
579 for (i=0;i<EMBED*DIM;i++)