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: Mar 20, 1999 */
26 #include "routines/tsa.h"
28 #define WID_STR "Estimates the finite size Lyapunov exponent; Vulpiani et al."
37 long box[NMAX][NMAX],*list;
38 unsigned int dim=2,delay=1,mindist=0;
39 unsigned int column=1;
40 unsigned int verbosity=0xff;
41 const unsigned int nmax=NMAX-1;
42 unsigned long length=ULONG_MAX,exclude=0;
43 double eps0=1.e-3,eps,epsinv,epsmax,epsfactor;
47 double time,factor,eps;
51 void show_options(char *progname)
53 what_i_do(progname,WID_STR);
54 fprintf(stderr," Usage: %s [options]\n",progname);
55 fprintf(stderr," Options:\n");
56 fprintf(stderr,"Everything not being a valid option will be interpreted"
58 " datafile.\nIf no datafile is given stdin is read. Just - also"
60 fprintf(stderr,"\t-l # of datapoints [default is whole file]\n");
61 fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
62 fprintf(stderr,"\t-c column to read[default 1]\n");
63 fprintf(stderr,"\t-m embedding dimension [default 2]\n");
64 fprintf(stderr,"\t-d delay [default 1]\n");
65 fprintf(stderr,"\t-t time window to omit [default 0]\n");
66 fprintf(stderr,"\t-r epsilon size to start with [default "
67 "(std. dev. of data)/1000]\n");
68 fprintf(stderr,"\t-o name of output file [default 'datafile'.fsl ,"
69 "without -o: stdout]\n");
70 fprintf(stderr,"\t-V verbosity level [default 1]\n\t\t"
71 "0='only panic messages'\n\t\t"
72 "1='+ input/output messages'\n");
73 fprintf(stderr,"\t-h show these options\n");
78 void scan_options(int n,char **argv)
82 if ((out=check_option(argv,n,'l','u')) != NULL)
83 sscanf(out,"%lu",&length);
84 if ((out=check_option(argv,n,'x','u')) != NULL)
85 sscanf(out,"%lu",&exclude);
86 if ((out=check_option(argv,n,'c','u')) != NULL)
87 sscanf(out,"%u",&column);
88 if ((out=check_option(argv,n,'m','u')) != NULL)
89 sscanf(out,"%u",&dim);
90 if ((out=check_option(argv,n,'d','u')) != NULL)
91 sscanf(out,"%u",&delay);
92 if ((out=check_option(argv,n,'t','u')) != NULL)
93 sscanf(out,"%u",&mindist);
94 if ((out=check_option(argv,n,'V','u')) != NULL)
95 sscanf(out,"%u",&verbosity);
96 if ((out=check_option(argv,n,'r','f')) != NULL) {
98 sscanf(out,"%lf",&eps0);
100 if ((out=check_option(argv,n,'o','o')) != NULL) {
107 void put_in_boxes(void)
116 for (i=0;i<length-del;i++) {
117 x=(int)(series[i]*epsinv)&nmax;
118 y=(int)(series[i+del]*epsinv)&nmax;
124 char make_iterate(long act)
127 int x,y,i,j,i1,k,del1=dim*delay,which;
128 long element,minelement= -1;
129 double dx=0.0,mindx=2.0,stime;
131 x=(int)(series[act]*epsinv)&nmax;
132 y=(int)(series[act+delay*(dim-1)]*epsinv)&nmax;
133 for (i=x-1;i<=x+1;i++) {
135 for (j=y-1;j<=y+1;j++) {
136 element=box[i1][j&nmax];
137 while (element != -1) {
138 if (labs(act-element) > mindist) {
139 for (k=0;k<del1;k+=delay) {
140 dx = fabs(series[act+k]-series[element+k]);
154 element=list[element];
159 if ((minelement != -1) && (mindx < eps)) {
161 minelement += del1-delay+1;
162 which=(int)(log(mindx/eps0)/log(epsfactor));
164 while ((dx=fabs(series[act]-series[minelement])) < data[0].eps) {
167 if ((act >= length) || (minelement >= length))
171 which=(int)(log(mindx/eps0)/log(epsfactor));
173 for (i=which;i<howmany-1;i++) {
175 while ((dx=fabs(series[act]-series[minelement])) < data[i+1].eps) {
178 if ((act >= length) || (minelement >= length))
183 data[i].time += stime;
184 data[i].factor += log(dx/mindx);
193 int main(int argc,char **argv)
195 char stdi=0,*done,alldone;
199 double min,max,se_av,se_var,se0_av,se0_var;
202 if (scan_help(argc,argv))
203 show_options(argv[0]);
205 scan_options(argc,argv);
206 #ifndef OMIT_WHAT_I_DO
207 if (verbosity&VER_INPUT)
208 what_i_do(argv[0],WID_STR);
211 infile=search_datafile(argc,argv,&column,verbosity);
215 if (outfile == NULL) {
217 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
218 strcpy(outfile,infile);
219 strcat(outfile,".fsl");
222 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
223 strcpy(outfile,"stdin.fsl");
227 test_outfile(outfile);
229 series=(double*)get_series(infile,&length,exclude,column,verbosity);
230 variance(series,length,&se0_av,&se0_var);
231 rescale_data(series,length,&min,&max);
232 variance(series,length,&se_av,&se_var);
242 if (eps0 >= epsmax) {
243 fprintf(stderr,"The minimal epsilon is too large. Exiting!\n");
244 exit(FSLE__TOO_LARGE_MINEPS);
248 howmany=(int)(log(epsmax/eps0)/log(epsfactor))+1;
249 check_alloc(data=(struct fsle*)malloc(sizeof(struct fsle)*howmany));
251 for (i=0;i<howmany;i++) {
252 data[i].time=data[i].factor=0.0;
253 data[i].eps= (eps *= epsfactor);
257 check_alloc(list=(long*)malloc(length*sizeof(long)));
258 check_alloc(done=(char*)malloc(length));
260 for (i=0;i<length;i++)
263 maxlength=length-delay*(dim-1)-1-mindist;
265 for (eps=eps0;(eps<=epsmax) && (!alldone);eps*=epsfactor) {
269 for (n=0;n<=maxlength;n++) {
271 done[n]=make_iterate(n);
276 file=fopen(outfile,"w");
277 if (verbosity&VER_INPUT)
278 fprintf(stderr,"Opened %s for writing\n",outfile);
279 for (i=0;i<howmany;i++)
280 if (data[i].factor > 0.0)
281 fprintf(file,"%e %e %ld\n",data[i].eps*max,
282 data[i].factor/data[i].time,data[i].count);
286 if (verbosity&VER_INPUT)
287 fprintf(stderr,"Writing to stdout\n");
288 for (i=0;i<howmany;i++)
289 if (data[i].factor > 0.0)
290 fprintf(stdout,"%e %e %ld\n",data[i].eps*max,
291 data[i].factor/data[i].time,data[i].count);