/* * 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: Jun 10, 2006 */ #include #include #include #include #include #include "routines/tsa.h" #define WID_STR "Multivariate noise reduction using the GHKSS algorithm" #define BOX (unsigned int)1024 unsigned long length=ULONG_MAX,exclude=0; unsigned int dim,qdim=2,delay=1,minn=50,iterations=1,comp=1,embed=5; unsigned int verbosity=0xff; double mineps,epsfac; char *column=NULL; char eps_set=0,euclidean=0,dimset=0,resize_eps; char *outfile=NULL,stdo=1; char *infile=NULL; double *d_min,*d_max,d_max_max; double **series,**delta,**corr; double *metric,trace; long **box,*list; unsigned long *flist; int emb_offset; unsigned int ibox=BOX-1; unsigned int *index_comp,*index_embed; /*these are global to save time*/ int *sorted; double *av,**mat,*matarray,*eig; 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 data to use [Default: whole file]\n"); fprintf(stderr,"\t-x # of lines to be ignored [Default: 0]\n"); fprintf(stderr,"\t-c column to read [Default: 1,..,# of components]\n"); fprintf(stderr,"\t-m # of components,embedding dimension [Default: 1,5]\n"); fprintf(stderr,"\t-d delay [Default: 1]\n"); fprintf(stderr,"\t-q dimension to project to [Default: 2]\n"); fprintf(stderr,"\t-k minimal number of neighbours [Default: 50]\n"); fprintf(stderr,"\t-r minimal neighbourhood size \n\t\t" "[Default: (interval of data)/1000]\n"); fprintf(stderr,"\t-i # of iterations [Default: 1]\n"); fprintf(stderr,"\t-2 use euklidean metric [Default: non euklidean]\n"); fprintf(stderr,"\t-o name of output file \n\t\t" "[Default: 'datafile'.opt.n, where n is the iteration.\n\t\t" " If no -o is given, the last iteration is also" " written to stdout]\n"); fprintf(stderr,"\t-V verbosity level [Default: 7]\n\t\t" "0='only panic messages'\n\t\t" "1='+ input/output messages'\n\t\t" "2='+ average correction and trend'\n\t\t" "4='+ how many points for which epsilon'\n"); fprintf(stderr,"\t-h show these options\n"); exit(0); } void scan_options(int n,char **in) { char *out; if ((out=check_option(in,n,'l','u')) != NULL) sscanf(out,"%lu",&length); if ((out=check_option(in,n,'x','u')) != NULL) sscanf(out,"%lu",&exclude); if ((out=check_option(in,n,'c','s')) != NULL) { column=out; dimset=1; } if ((out=check_option(in,n,'m','2')) != NULL) sscanf(out,"%u,%u",&comp,&embed); if ((out=check_option(in,n,'d','u')) != NULL) sscanf(out,"%u",&delay); if ((out=check_option(in,n,'q','u')) != NULL) sscanf(out,"%u",&qdim); if ((out=check_option(in,n,'k','u')) != NULL) sscanf(out,"%u",&minn); if ((out=check_option(in,n,'r','f')) != NULL) { eps_set=1; sscanf(out,"%lf",&mineps); } if ((out=check_option(in,n,'i','u')) != NULL) sscanf(out,"%u",&iterations); if ((out=check_option(in,n,'V','u')) != NULL) sscanf(out,"%u",&verbosity); if ((out=check_option(in,n,'2','n')) != NULL) euclidean=1; if ((out=check_option(in,n,'o','o')) != NULL) { stdo=0; if (strlen(out) > 0) outfile=out; } } void sort(double *x,int *n) { long i,j,iswap; double dswap; for (i=0;i x[i]) { dswap=x[i]; x[i]=x[j]; x[j]=dswap; iswap=n[i]; n[i]=n[j]; n[j]=iswap; } } void mmb(double eps) { long i,x,y; double ieps=1.0/eps; for (x=0;x eps) break; } if (dx > eps) break; } if (dx <= eps) flist[nf++]=element; element=list[element]; } } } return nf; } void make_correction(unsigned long n,unsigned long nf) { long i,i1,i2,j,j1,j2,k,k1,k2,hs; double help; for (i=0;i d_max_max) d_max_max=d_max[i]; } if (!eps_set) mineps=1./1000.; else mineps /= d_max_max; epsfac=sqrt(2.0); check_alloc(box=(long**)malloc(sizeof(long*)*BOX)); for (i=0;i= comp) && (i < ((long)dim-(long)comp))) metric[i]=1.0; else metric[i]=1.0e3; trace += 1./metric[i]; } } check_alloc(corr=(double**)malloc(sizeof(double*)*length)); for (i=0;i= minn) { make_correction(n,nfound); ok[n]=epscount; if (epscount == 1) resize_eps=1; allfound++; } else all_done=0; } if (verbosity&VER_USR2) fprintf(stderr,"Corrected %ld points with epsilon= %e\n",allfound, epsilon*d_max_max); epsilon *= epsfac; epscount++; } if (verbosity&VER_USR2) fprintf(stderr,"Start evaluating the trend\n"); epsilon=mineps; allfound=0; for (i=1;i