/* * 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 Dec 4, 2005 */ /*Changes: 7/14/05: Changed borders of the sort routine to speed things up 11/25/05: Show also absolute forecast errors 12/04/05: Some more changes in sort 12/20/05: Change in increase neighborhood size loop 12/28/05: Found bug in memory allocation (index) */ #include #include #include #include #include #include #include "routines/tsa.h" #define WID_STR "Estimates the spectrum of Lyapunov exponents using the\n\t\ method of Sano and Sawada." #define OUT 10 #define BOX 512 #define EPSMAX 1.0 #define DELAY 1 char epsset=0,stdo=1; char INVERSE,*outfile=NULL; char *infile=NULL; char dimset=0; char *COLUMNS=NULL; unsigned long LENGTH=ULONG_MAX,ITERATIONS,exclude=0; unsigned int EMBED=2,DIMENSION=1/*,DELAY=1*/,MINNEIGHBORS=30; unsigned int verbosity=0xff; double EPSSTEP=1.2; double **series,*averr,avneig=0.0,aveps=0.0; double **mat,*vec,*abstand; double epsmin; long imax=BOX-1,count=0; long **box,*list; unsigned long *found; unsigned int alldim,**indexes; 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 # of datapoints [default is whole file]\n"); fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n"); fprintf(stderr,"\t-c column to read[default 1]\n"); fprintf(stderr,"\t-m # of components,embedding dimension [default %d,%d]\n", DIMENSION,EMBED); // fprintf(stderr,"\t-d delay [default %d]\n",DELAY); fprintf(stderr,"\t-r epsilon size to start with [default " "(data interval)/1000]\n"); fprintf(stderr,"\t-f factor to increase epsilon [default: 1.2]\n"); fprintf(stderr,"\t-k # of neighbors to use [default: 30]\n"); fprintf(stderr,"\t-n # of iterations [default: length]\n"); fprintf(stderr,"\t-I invert the time series [default: no]\n"); fprintf(stderr,"\t-o name of output file [default 'datafile'.lyaps]\n"); fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t" "0='only panic messages'\n\t\t" "1='+ input/output messages'\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) COLUMNS=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",&DIMENSION,&EMBED); dimset=1; } if ((out=check_option(argv,n,'n','u')) != NULL) sscanf(out,"%lu",&ITERATIONS); if ((out=check_option(argv,n,'r','f')) != NULL) { epsset=1; sscanf(out,"%lf",&epsmin); } if ((out=check_option(argv,n,'f','f')) != NULL) sscanf(out,"%lf",&EPSSTEP); if ((out=check_option(argv,n,'k','u')) != NULL) sscanf(out,"%u",&MINNEIGHBORS); if ((out=check_option(argv,n,'V','u')) != NULL) sscanf(out,"%u",&verbosity); if ((out=check_option(argv,n,'I','n')) != NULL) INVERSE=1; if ((out=check_option(argv,n,'o','o')) != NULL) { stdo=0; if (strlen(out) > 0) outfile=out; } } double sort(long act,unsigned long* nfound,char *enough) { double maxeps=0.0,dx,dswap,maxdx; long self=0,i,j,del,hf,iswap,n1; unsigned long imax=*nfound; *enough=0; for (i=0;i maxdx) maxdx=dx; } abstand[i]=maxdx; } else { self=i; } } if (self != (imax-1)) { abstand[self]=abstand[imax-1]; found[self]=found[imax-1]; } for (i=0;i= epsmin)) { *nfound=MINNEIGHBORS; *enough=1; maxeps=abstand[MINNEIGHBORS-1]; return maxeps; } for (i=MINNEIGHBORS;i epsmin) { (*nfound)=i+1; *enough=1; maxeps=abstand[i]; return maxeps; } } maxeps=abstand[imax-2]; return maxeps; } void make_dynamics(double **dynamics,long act) { long i,hi,j,hj,k,t=act,d; unsigned long nfound=0; double **hser,**imat; double foundeps=0.0,epsilon,hv,hv1; double new_vec; char got_enough; check_alloc(hser=(double**)malloc(sizeof(double*)*DIMENSION)); for (i=0;i EPSMAX) epsilon=EPSMAX; make_multi_box(series,box,list,LENGTH-DELAY,BOX,DIMENSION,EMBED, DELAY,epsilon); nfound=find_multi_neighbors(series,box,list,hser,LENGTH-DELAY,BOX, DIMENSION,EMBED,DELAY,epsilon,found); if (nfound > MINNEIGHBORS) { foundeps=sort(act,&nfound,&got_enough); if (got_enough) break; } } while (epsilon < EPSMAX); free(hser); avneig += nfound; aveps += foundeps; if (!epsset) epsmin=aveps/count; if (nfound < MINNEIGHBORS) { fprintf(stderr,"#Not enough neighbors found. Exiting\n"); exit(LYAP_SPEC_NOT_ENOUGH_NEIGHBORS); } for (i=0;i<=alldim;i++) { vec[i]=0.0; for (j=0;j<=alldim;j++) mat[i][j]=0.0; } for (i=0;i (LENGTH-DELAY*(EMBED-1)-1)) { fprintf(stderr,"Your time series is not long enough to find %d neighbors!" " Exiting.\n",MINNEIGHBORS); exit(LYAP_SPEC_DATA_TOO_SHORT); } check_alloc(min=(double*)malloc(sizeof(double)*DIMENSION)); check_alloc(interval=(double*)malloc(sizeof(double)*DIMENSION)); check_alloc(av=(double*)malloc(sizeof(double)*DIMENSION)); check_alloc(var=(double*)malloc(sizeof(double)*DIMENSION)); check_alloc(averr=(double*)malloc(sizeof(double)*DIMENSION)); maxinterval=0.0; for (i=0;i maxinterval) maxinterval=interval[i]; variance(series[i],LENGTH,&av[i],&var[i]); } if (INVERSE) { check_alloc(hseries=(double*)malloc(sizeof(double)*LENGTH)); for (j=0;j(LENGTH-DELAY)) start=LENGTH-DELAY; if (!stdo) { file=fopen(outfile,"w"); if (verbosity&VER_INPUT) fprintf(stderr,"Opened %s for writing\n",outfile); } else { if (verbosity&VER_INPUT) fprintf(stderr,"Writing to stdout\n"); } check_alloc(abstand=(double*)malloc(sizeof(double)*LENGTH)); time(&lasttime); for (i=(EMBED-1)*DELAY;i OUT) || (i == (start-1))) { time(&lasttime); if (!stdo) { fprintf(file,"%ld ",count); for (j=0;j