Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / recurr.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
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.
10  *
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.
15  *
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
19  */
20 /*Author: Rainer Hegger Last modified:  Sep 16, 2004 */
21 /* Sep 16, 2004: Change of index in output. before 0->N-1 now 1->N
22  */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <limits.h>
28 #include "routines/tsa.h"
29
30 #define WID_STR "This programs makes a recurrence plot for the data."
31
32 #define BOX 1024
33
34 unsigned long length=ULONG_MAX,exclude=0;
35 unsigned int embed=2,dim=1,delay=1;
36 unsigned int verbosity=0xff;
37 double eps=1.e-3,fraction=1.0;
38 char dimset=0;
39 char *columns;
40 char *outfile=NULL,stdo=1;
41 char *infile=NULL;
42 char epsset=0;
43
44 double **series;
45 long **box,*list;
46
47 void show_options(char *progname)
48 {
49   what_i_do(progname,WID_STR);
50   fprintf(stderr,"Usage: %s [options]\n",progname);
51   fprintf(stderr,"Options:\n");
52   fprintf(stderr,"Everything not being a valid option will be interpreted"
53           " as a possible"
54           " datafile.\nIf no datafile is given stdin is read. Just - also"
55           " means stdin\n");
56   fprintf(stderr,"\t-l # of data to use [Default: whole file]\n");
57   fprintf(stderr,"\t-x # of lines to be ignored [Default: 0]\n");
58   fprintf(stderr,"\t-c columns to read [Default: 1]\n");
59   fprintf(stderr,"\t-m # of components,embedding dimension [Default: 1,2]\n");
60   fprintf(stderr,"\t-d delay [Default: 1]\n");
61   fprintf(stderr,"\t-r size of the neighbourhood "
62           "[Default: (data interval)/1000]\n");
63   fprintf(stderr,"\t-%% print only a percentage of points found [Default: "
64           " 100.0]\n");
65   fprintf(stderr,"\t-o output file name [Default: 'datafile'.rec\n"
66           "\t\twithout -o: stdout]\n");
67   fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t"
68           "0='only panic messages'\n\t\t"
69           "1='+ input/output messages'\n");
70   fprintf(stderr,"\t-h show these options\n");
71   exit(0);
72 }
73
74 void scan_options(int n,char **in)
75 {
76   char *out;
77
78   if ((out=check_option(in,n,'l','u')) != NULL)
79     sscanf(out,"%lu",&length);
80   if ((out=check_option(in,n,'x','u')) != NULL)
81     sscanf(out,"%lu",&exclude);
82   if ((out=check_option(in,n,'c','s')) != NULL)
83     columns=out;
84   if ((out=check_option(in,n,'m','2')) != NULL) {
85     sscanf(out,"%u,%u",&dim,&embed);
86     dimset=1;
87   }
88   if ((out=check_option(in,n,'d','u')) != NULL)
89     sscanf(out,"%u",&delay);
90   if ((out=check_option(in,n,'V','u')) != NULL)
91     sscanf(out,"%u",&verbosity);
92   if ((out=check_option(in,n,'r','f')) != NULL) {
93     epsset=1;
94     sscanf(out,"%lf",&eps);
95   }
96   if ((out=check_option(in,n,'%','f')) != NULL) {
97     sscanf(out,"%lf",&fraction);
98     fraction /= 100.0;
99   }
100   if ((out=check_option(in,n,'o','o')) != NULL) {
101     stdo=0;
102     if (strlen(out) > 0)
103       outfile=out;
104   }
105 }
106
107 void lfind_neighbors(void)
108 {
109   int i,i1,i2,j,j1,ke,ked,kd;
110   int ibox=BOX-1;
111   long n,element;
112   double dx,epsinv;
113   char toolarge;
114   FILE *fout=NULL;
115
116   epsinv=1./eps;
117   rnd_init(0x9834725L);
118
119   if (!stdo) {
120     fout=fopen(outfile,"w");
121     if (verbosity&VER_INPUT)
122       fprintf(stderr,"Opened %s for writing\n",outfile);
123   }
124   else {
125     if (verbosity&VER_INPUT)
126       fprintf(stderr,"Writing to stdout\n");
127   }
128
129   for (n=(embed-1)*delay;n<length;n++) {
130     i=(int)(series[0][n]*epsinv)&ibox;
131     j=(int)(series[dim-1][n]*epsinv)&ibox;
132     for (i1=i-1;i1<=i+1;i1++) {
133       i2=i1&ibox;
134       for (j1=j-1;j1<=j+1;j1++) {
135         element=box[i2][j1&ibox];
136         while (element > n) {
137           toolarge=0;
138           for (ke=0;ke<embed;ke++) {
139             ked=ke*delay;
140             for (kd=0;kd<dim;kd++) {
141               dx=fabs(series[kd][n-ked]-series[kd][element-ked]);
142               if (dx >= eps) {
143                 toolarge=1;
144                 break;
145               }
146             }
147             if (toolarge)
148               break;
149           }
150           if (!toolarge)
151             if (((double)rnd69069()/ULONG_MAX) <= fraction) {
152               if (!stdo)
153                 fprintf(fout,"%ld %ld\n",n+1,element+1);
154               else
155                 fprintf(stdout,"%ld %ld\n",n+1,element+1);
156             }
157           element=list[element];
158         }
159       }
160     }
161   }
162   if (!stdo)
163     fclose(fout);
164 }
165
166 int main(int argc,char **argv)
167 {
168   long i;
169   char stdi=0;
170   double min,max,maxmax;
171
172   if (scan_help(argc,argv))
173     show_options(argv[0]);
174   
175   scan_options(argc,argv);
176 #ifndef OMIT_WHAT_I_DO
177   if (verbosity&VER_INPUT)
178     what_i_do(argv[0],WID_STR);
179 #endif
180
181   infile=search_datafile(argc,argv,NULL,verbosity);
182   if (infile == NULL)
183     stdi=1;
184
185   if (outfile == NULL) {
186     if (!stdi) {
187       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
188       strcpy(outfile,infile);
189       strcat(outfile,".rec");
190     }
191     else {
192       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
193       strcpy(outfile,"stdin.rec");
194     }
195   }
196   if (!stdo)
197     test_outfile(outfile);
198
199   if (columns == NULL)
200     series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
201                                       verbosity);
202   else
203     series=(double**)get_multi_series(infile,&length,exclude,&dim,columns,
204                                       dimset,verbosity);
205
206   maxmax=0.0;
207   for (i=0;i<dim;i++) {
208     rescale_data(series[i],length,&min,&max);
209     if (max > maxmax)
210       maxmax=max;
211   }
212
213   if (epsset)
214     eps /= maxmax;
215
216   check_alloc(list=(long*)malloc(sizeof(long)*length));
217   check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
218   for (i=0;i<BOX;i++)
219     check_alloc(box[i]=(long*)malloc(sizeof(long)*BOX));
220
221   make_multi_box(series,box,list,length,BOX,dim,embed,delay,eps);
222   lfind_neighbors();
223
224   return 0;
225 }