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: Sep 16, 2004 */
21 /* Sep 16, 2004: Change of index in output. before 0->N-1 now 1->N
28 #include "routines/tsa.h"
30 #define WID_STR "This programs makes a recurrence plot for the data."
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;
40 char *outfile=NULL,stdo=1;
47 void show_options(char *progname)
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"
54 " datafile.\nIf no datafile is given stdin is read. Just - also"
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: "
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");
74 void scan_options(int n,char **in)
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)
84 if ((out=check_option(in,n,'m','2')) != NULL) {
85 sscanf(out,"%u,%u",&dim,&embed);
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) {
94 sscanf(out,"%lf",&eps);
96 if ((out=check_option(in,n,'%','f')) != NULL) {
97 sscanf(out,"%lf",&fraction);
100 if ((out=check_option(in,n,'o','o')) != NULL) {
107 void lfind_neighbors(void)
109 int i,i1,i2,j,j1,ke,ked,kd;
117 rnd_init(0x9834725L);
120 fout=fopen(outfile,"w");
121 if (verbosity&VER_INPUT)
122 fprintf(stderr,"Opened %s for writing\n",outfile);
125 if (verbosity&VER_INPUT)
126 fprintf(stderr,"Writing to stdout\n");
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++) {
134 for (j1=j-1;j1<=j+1;j1++) {
135 element=box[i2][j1&ibox];
136 while (element > n) {
138 for (ke=0;ke<embed;ke++) {
140 for (kd=0;kd<dim;kd++) {
141 dx=fabs(series[kd][n-ked]-series[kd][element-ked]);
151 if (((double)rnd69069()/ULONG_MAX) <= fraction) {
153 fprintf(fout,"%ld %ld\n",n+1,element+1);
155 fprintf(stdout,"%ld %ld\n",n+1,element+1);
157 element=list[element];
166 int main(int argc,char **argv)
170 double min,max,maxmax;
172 if (scan_help(argc,argv))
173 show_options(argv[0]);
175 scan_options(argc,argv);
176 #ifndef OMIT_WHAT_I_DO
177 if (verbosity&VER_INPUT)
178 what_i_do(argv[0],WID_STR);
181 infile=search_datafile(argc,argv,NULL,verbosity);
185 if (outfile == NULL) {
187 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
188 strcpy(outfile,infile);
189 strcat(outfile,".rec");
192 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
193 strcpy(outfile,"stdin.rec");
197 test_outfile(outfile);
200 series=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
203 series=(double**)get_multi_series(infile,&length,exclude,&dim,columns,
207 for (i=0;i<dim;i++) {
208 rescale_data(series[i],length,&min,&max);
216 check_alloc(list=(long*)malloc(sizeof(long)*length));
217 check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
219 check_alloc(box[i]=(long*)malloc(sizeof(long)*BOX));
221 make_multi_box(series,box,list,length,BOX,dim,embed,delay,eps);