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: Nov 30, 2000 */
22 12/11/05: Going multivariate
30 #include "routines/tsa.h"
32 #define WID_STR "Performs simple noise reduction."
34 #define BOX (unsigned int)512
36 unsigned long length=ULONG_MAX,exclude=0;
37 unsigned int comp=1,embed=5,delay=1,iterations=1,alldim;
38 unsigned int verbosity=0x3;
40 double eps=1.0e-3,epsvar;
42 char *outfile=NULL,epsset=0,stdo=1,epsvarset=0;
44 double **series,**corr,*interval,*min,*hcor;
45 long **box,*list,**nf;
46 unsigned int **indexes;
49 void show_options(char *progname)
51 what_i_do(progname,WID_STR);
52 fprintf(stderr," Usage: %s [Options]\n",progname);
53 fprintf(stderr," Options:\n");
54 fprintf(stderr,"Everything not being a valid option will be interpreted"
56 " datafile.\nIf no datafile is given stdin is read. Just - also"
58 fprintf(stderr,"\t-l # of data to use [default: whole file]\n");
59 fprintf(stderr,"\t-x # of lines to be ignored [default: 0]\n");
60 fprintf(stderr,"\t-c column to read [default: 1]\n");
61 fprintf(stderr,"\t-m no. of comp.,embedding dim. [default: %u,%u]\n",
63 fprintf(stderr,"\t-d delay [default: 1]\n");
64 fprintf(stderr,"\t-i iterations [default: 1]\n");
65 fprintf(stderr,"\t-r neighborhoud size [default: (interval of data)/1000]\n");
66 fprintf(stderr,"\t-v neighborhoud size (in units of the std. dev. of the "
67 "data \n\t\t(overwrites -r) [default: not set]\n");
68 fprintf(stderr,"\t-o output file name [Default: 'datafile'.laz.n,"
69 "\n\t\twhere n is the number of the last iteration,"
70 "\n\t\twithout -o the last iteration is written to stdout.]\n");
71 fprintf(stderr,"\t-V verbosity level [Default: 3]\n\t\t"
72 "0='only panic messages'\n\t\t"
73 "1='+ input/output messages'\n\t\t"
74 "2='+ write output of all iterations to files'\n\t\t"
75 "4='+ write the number of neighbors found for each point\n");
76 fprintf(stderr,"\t-h show these options\n");
80 void scan_options(int n,char **in)
84 if ((out=check_option(in,n,'l','u')) != NULL)
85 sscanf(out,"%lu",&length);
86 if ((out=check_option(in,n,'x','u')) != NULL)
87 sscanf(out,"%lu",&exclude);
88 if ((out=check_option(in,n,'c','c')) != NULL) {
92 if ((out=check_option(in,n,'m','2')) != NULL)
93 sscanf(out,"%u,%u",&comp,&embed);
94 if ((out=check_option(in,n,'d','u')) != NULL)
95 sscanf(out,"%u",&delay);
96 if ((out=check_option(in,n,'i','u')) != NULL)
97 sscanf(out,"%u",&iterations);
98 if ((out=check_option(in,n,'r','f')) != NULL) {
100 sscanf(out,"%lf",&eps);
102 if ((out=check_option(in,n,'V','u')) != NULL)
103 sscanf(out,"%u",&verbosity);
104 if ((out=check_option(in,n,'v','f')) != NULL) {
106 sscanf(out,"%lf",&epsvar);
108 if ((out=check_option(in,n,'o','o')) != NULL) {
115 unsigned int correct(unsigned long n)
119 unsigned int hdel,hcomp;
121 long element,nfound=0;
125 for (i=0;i<alldim;i++)
128 i=(int)(series[0][n]*epsinv)&ibox;
129 j=(int)(series[comp-1][n-(embed-1)*delay]*epsinv)&ibox;
131 for (i1=i-1;i1<=i+1;i1++) {
133 for (j1=j-1;j1<=j+1;j1++) {
134 element=box[i2][j1&ibox];
135 while (element != -1) {
136 for (k=0;k<alldim;k++) {
139 dx=fabs(series[hcomp][n-hdel]-series[hcomp][element-hdel]);
145 for (k=0;k<alldim;k++) {
148 hcor[k] += series[hcomp][element-hdel];
151 element=list[element];
155 for (k=0;k<alldim;k++) {
158 corr[hcomp][n-hdel] += hcor[k]/nfound;
165 int main(int argc,char **argv)
172 double dav,dvar,maxinterval,maxdvar;
175 if (scan_help(argc,argv))
176 show_options(argv[0]);
178 scan_options(argc,argv);
179 #ifndef OMIT_WHAT_I_DO
180 if (verbosity&VER_INPUT)
181 what_i_do(argv[0],WID_STR);
184 infile=search_datafile(argc,argv,NULL,verbosity);
188 if (outfile == NULL) {
190 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
191 check_alloc(ofname=(char*)calloc(strlen(infile)+9,(size_t)1));
192 sprintf(outfile,"%s.laz",infile);
195 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
196 check_alloc(ofname=(char*)calloc((size_t)14,(size_t)1));
197 sprintf(outfile,"stdin.laz");
201 check_alloc(ofname=(char*)calloc(strlen(outfile)+10,(size_t)1));
205 series=(double**)get_multi_series(infile,&length,exclude,&comp,"",dimset,
208 series=(double**)get_multi_series(infile,&length,exclude,&comp,column,
211 check_alloc(interval=(double*)malloc(sizeof(double)*comp));
212 check_alloc(min=(double*)malloc(sizeof(double)*comp));
214 maxinterval=maxdvar=0.0;
215 for (i=0;i<comp;i++) {
216 rescale_data(series[i],length,&min[i],&interval[i]);
217 if (interval[i] > maxinterval) maxinterval=interval[i];
218 variance(series[i],length,&dav,&dvar);
219 if (dvar > maxdvar) maxdvar=dvar;
223 check_alloc(nmf=(unsigned int*)malloc(sizeof(int)*length));
224 check_alloc(list=(long*)malloc(sizeof(long)*length));
225 check_alloc(box=(long**)malloc(sizeof(long*)*BOX));
227 check_alloc(box[n]=(long*)malloc(sizeof(long)*BOX));
229 check_alloc(nf=(long**)malloc(sizeof(long*)*comp));
230 check_alloc(corr=(double**)malloc(sizeof(double*)*comp));
231 for (i=0;i<comp;i++) {
232 check_alloc(nf[i]=(long*)malloc(sizeof(long)*length));
233 check_alloc(corr[i]=(double*)malloc(sizeof(double)*length));
236 indexes=make_multi_index(comp,embed,delay);
246 for (iter=1;iter<=iterations;iter++) {
247 make_multi_box2(series,box,list,length,BOX,comp,embed,delay,eps);
248 for (n=0;n<length;n++) {
249 for (i=0;i<comp;i++) {
256 check_alloc(hcor=(double*)malloc(sizeof(double)*alldim));
257 for (n=(embed-1)*delay;n<length;n++)
261 for (n=0;n<length;n++)
264 series[i][n]=corr[i][n]/nf[i][n];
266 if ((verbosity&VER_USR1) && (iter < iterations)) {
267 sprintf(ofname,"%s.%d",outfile,iter);
268 test_outfile(ofname);
269 file=fopen(ofname,"w");
270 if (verbosity&VER_INPUT)
271 fprintf(stderr,"Opened %s for writing\n",ofname);
272 if (stdo && (iter == iterations)) {
273 if (verbosity&VER_INPUT)
274 fprintf(stderr,"Writing to stdout\n");
276 for (n=0;n<length;n++) {
277 if (stdo && (iter == iterations)) {
278 if (verbosity&VER_USR2) {
280 fprintf(stdout,"%e ",series[i][n]*interval[i]+min[i]);
281 fprintf(stdout,"%u\n",nmf[n]);
284 fprintf(stdout,"%e",series[0][n]*interval[0]+min[0]);
286 fprintf(stdout,"%e ",series[i][n]*interval[i]+min[i]);
287 fprintf(stdout,"\n");
290 if (verbosity&VER_USR2) {
292 fprintf(file,"%e ",series[i][n]*interval[i]+min[i]);
293 fprintf(file,"%u\n",nmf[n]);
296 fprintf(file,"%e",series[0][n]*interval[0]+min[0]);
298 fprintf(file," %e",series[i][n]*interval[i]+min[i]);
304 if (iter == iterations) {
305 if (!stdo || (verbosity&VER_USR1)) {
306 sprintf(ofname,"%s.%d",outfile,iter);
307 test_outfile(ofname);
308 file=fopen(ofname,"w");
309 if (verbosity&VER_INPUT)
310 fprintf(stderr,"Opened %s for writing\n",ofname);
311 if (stdo && (iter == iterations)) {
312 if (verbosity&VER_INPUT)
313 fprintf(stderr,"Writing to stdout\n");
316 for (n=0;n<length;n++) {
318 if (verbosity&VER_USR2) {
320 fprintf(stdout,"%e ",series[i][n]*interval[i]+min[i]);
321 fprintf(stdout,"%u\n",nmf[n]);
324 fprintf(stdout,"%e",series[0][n]*interval[0]+min[0]);
326 fprintf(stdout," %e",series[i][n]*interval[i]+min[i]);
327 fprintf(stdout,"\n");
330 if (!stdo || (verbosity&VER_USR1)) {
331 if (verbosity&VER_USR2) {
333 fprintf(file,"%e ",series[i][n]*interval[i]+min[i]);
334 fprintf(file,"%u\n",nmf[n]);
337 fprintf(file,"%e",series[0][n]*interval[0]+min[0]);
339 fprintf(file," %e",series[i][n]*interval[i]+min[i]);
344 if (!stdo || (verbosity&VER_USR1))
350 for (i=0;i<comp;i++) {
380 /* end cleaning up */