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: Apr 25, 2002 */
26 #include "routines/tsa.h"
28 #define WID_STR "Estimates the maximal Lyapunov exponent; Rosenstein et al."
36 long box[NMAX][NMAX],*list;
37 unsigned int dim=2,delay=1,steps=10,mindist=0;
38 unsigned int column=1;
39 unsigned int verbosity=0xff;
40 const unsigned int nmax=NMAX-1;
41 unsigned long length=ULONG_MAX,exclude=0;
43 double eps0=1.e-3,eps,epsinv;
45 void show_options(char *progname)
47 what_i_do(progname,WID_STR);
48 fprintf(stderr," Usage: %s [options]\n",progname);
49 fprintf(stderr," Options:\n");
50 fprintf(stderr,"Everything not being a valid option will be interpreted"
52 " datafile.\nIf no datafile is given stdin is read. Just - also"
54 fprintf(stderr,"\t-l # of datapoints [default is whole file]\n");
55 fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
56 fprintf(stderr,"\t-c column to read[default 1]\n");
57 fprintf(stderr,"\t-m embedding dimension [default 2]\n");
58 fprintf(stderr,"\t-d delay [default 1]\n");
59 fprintf(stderr,"\t-t time window to omit [default 0]\n");
60 fprintf(stderr,"\t-r epsilon size to start with [default "
61 "(data interval)/1000]\n");
62 fprintf(stderr,"\t-s # of iterations [default 10]\n");
63 fprintf(stderr,"\t-o name of output file [default 'datafile'.ros]\n");
64 fprintf(stderr,"\t-V verbosity level [default 3]\n\t\t"
65 "0='only panic messages'\n\t\t"
66 "1='+ input/output messages'\n\t\t"
67 "2='+ give more detailed information about the length scales\n");
68 fprintf(stderr,"\t-h show these options\n");
73 void scan_options(int n,char **argv)
77 if ((out=check_option(argv,n,'l','u')) != NULL)
78 sscanf(out,"%lu",&length);
79 if ((out=check_option(argv,n,'x','u')) != NULL)
80 sscanf(out,"%lu",&exclude);
81 if ((out=check_option(argv,n,'c','u')) != NULL)
82 sscanf(out,"%u",&column);
83 if ((out=check_option(argv,n,'m','u')) != NULL)
84 sscanf(out,"%u",&dim);
85 if ((out=check_option(argv,n,'d','u')) != NULL)
86 sscanf(out,"%u",&delay);
87 if ((out=check_option(argv,n,'t','u')) != NULL)
88 sscanf(out,"%u",&mindist);
89 if ((out=check_option(argv,n,'r','f')) != NULL) {
91 sscanf(out,"%lf",&eps0);
93 if ((out=check_option(argv,n,'s','u')) != NULL)
94 sscanf(out,"%u",&steps);
95 if ((out=check_option(argv,n,'V','u')) != NULL)
96 sscanf(out,"%u",&verbosity);
97 if ((out=check_option(argv,n,'o','o')) != NULL)
102 void put_in_boxes(void)
111 for (i=0;i<length-del-steps;i++) {
112 x=(int)(series[i]*epsinv)&nmax;
113 y=(int)(series[i+del]*epsinv)&nmax;
119 char make_iterate(long act)
122 int x,y,i,j,i1,k,del1=dim*delay;
123 long element,minelement= -1;
126 x=(int)(series[act]*epsinv)&nmax;
127 y=(int)(series[act+delay*(dim-1)]*epsinv)&nmax;
128 for (i=x-1;i<=x+1;i++) {
130 for (j=y-1;j<=y+1;j++) {
131 element=box[i1][j&nmax];
132 while (element != -1) {
133 if (labs(act-element) > mindist) {
135 for (k=0;k<del1;k+=delay) {
136 dx += (series[act+k]-series[element+k])*
137 (series[act+k]-series[element+k]);
151 element=list[element];
155 if ((minelement != -1) ) {
158 for (i=0;i<=steps;i++) {
162 for (j=0;j<del1;j+=delay) {
163 dx += (series[act+j]-series[minelement+j])*
164 (series[act+j]-series[minelement+j]);
175 int main(int argc,char **argv)
177 char stdi=0,*done,alldone;
184 if (scan_help(argc,argv))
185 show_options(argv[0]);
187 scan_options(argc,argv);
188 #ifndef OMIT_WHAT_I_DO
189 if (verbosity&VER_INPUT)
190 what_i_do(argv[0],WID_STR);
193 infile=search_datafile(argc,argv,&column,verbosity);
197 if (outfile == NULL) {
199 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
200 strcpy(outfile,infile);
201 strcat(outfile,".ros");
204 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
205 strcpy(outfile,"stdin.ros");
208 test_outfile(outfile);
210 series=(double*)get_series(infile,&length,exclude,column,verbosity);
211 rescale_data(series,length,&min,&max);
216 check_alloc(list=(long*)malloc(length*sizeof(long)));
217 check_alloc(lyap=(double*)malloc((steps+1)*sizeof(double)));
218 check_alloc(found=(long*)malloc((steps+1)*sizeof(long)));
219 check_alloc(done=(char*)malloc(length));
221 for (i=0;i<=steps;i++) {
225 for (i=0;i<length;i++)
228 maxlength=length-delay*(dim-1)-steps-1-mindist;
230 file=fopen(outfile,"w");
231 if (verbosity&VER_INPUT)
232 fprintf(stderr,"Opened %s for writing\n",outfile);
233 for (eps=eps0;!alldone;eps*=1.1) {
237 for (n=0;n<=maxlength;n++) {
239 done[n]=make_iterate(n);
242 if (verbosity&VER_USR1)
243 fprintf(stderr,"epsilon: %e already found: %ld\n",eps*max,found[0]);
245 for (i=0;i<=steps;i++)
247 fprintf(file,"%d %e\n",i,lyap[i]/found[i]/2.0);