Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / makenoise.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 29, 2000 */
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <limits.h>
26 #include <time.h>
27 #include "routines/tsa.h"
28
29 #define WID_STR "Adds noise to a time series or just creates random numbers"
30
31 char *outfile=NULL,cgaussian,stout=1,justcreate=0;
32 char *infile=NULL;
33 char absolute=0,dimset=0;
34 unsigned long length=ULONG_MAX,exclude=0,iseed=3441341;
35 unsigned int dim=1;
36 char *column=NULL;
37 unsigned int verbosity=0xff;
38 double **array,noiselevel=0.05;
39
40 void show_options(char *progname)
41 {
42   what_i_do(progname,WID_STR);
43   fprintf(stderr," Usage: %s [Options]\n\n",progname);
44   fprintf(stderr," Options:\n");
45   fprintf(stderr,"Everything not being a valid option will be interpreted"
46           " as a possible"
47           " datafile.\nIf no datafile is given stdin is read. Just - also"
48           " means stdin\n");
49   fprintf(stderr,"\t-l # of points to be used [Default: whole file]\n");
50   fprintf(stderr,"\t-x # of lines to be ignored [Default: %lu]\n",exclude);
51   fprintf(stderr,"\t-m # of columns to read [Default: %u]\n",dim);
52   fprintf(stderr,"\t-c column(s) to read  [Default: 1]\n");
53   fprintf(stderr,"\t-%% noiselevel in %% [Default:  %.1e%%]\n",
54           noiselevel*100.0);
55   fprintf(stderr,"\t-r absolute noise level (or absolute variance in case\n"
56           "\t\tof gaussian noise) [Default: not set]\n");
57   fprintf(stderr,"\t-g (use gaussian noise)     [Default: uniform]\n");
58   fprintf(stderr,"\t-I seed for the rnd-generator (If seed=0, the time\n"
59           "\t\tcommand is used to set the seed) [Default: fixed]\n");
60   fprintf(stderr,"\t-0 do not read input, just generate random numbers\n\t\t"
61           "(needs -l and -r) [Default: not set]\n");
62   fprintf(stderr,"\t-o outfile [Without argument 'datafile'.noi;"
63           " Without -o stdout is used]\n");
64   fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t"
65           "0='only panic messages'\n\t\t"
66           "1='+ input/output messages'\n");
67   fprintf(stderr,"  -h show these options");
68   fprintf(stderr,"\n");
69   exit(0);
70 }
71
72 void scan_options(int n,char** in)
73 {
74   char *out,lengthset=0;
75   
76   if ((out=check_option(in,n,'l','u')) != NULL) {
77     sscanf(out,"%lu",&length);
78     lengthset=1;
79   }
80   if ((out=check_option(in,n,'x','u')) != NULL)
81     sscanf(out,"%lu",&exclude);
82   if ((out=check_option(in,n,'m','u')) != NULL) {
83     sscanf(out,"%u",&dim);
84     dimset=1;
85   }
86   if ((out=check_option(in,n,'c','s')) != NULL)
87     column=out;
88   if ((out=check_option(in,n,'%','f')) != NULL) {
89     sscanf(out,"%lf",&noiselevel);
90     noiselevel /= 100.0;
91   }
92   if ((out=check_option(in,n,'r','f')) != NULL) {
93     sscanf(out,"%lf",&noiselevel);
94     absolute=1;
95   }
96   if ((out=check_option(in,n,'g','n')) != NULL)
97     cgaussian=1;
98   if ((out=check_option(in,n,'I','u')) != NULL) {
99     sscanf(out,"%lu",&iseed);
100     if (iseed == 0)
101       iseed=(unsigned long)time((time_t*)&iseed);
102   }
103   if ((out=check_option(in,n,'0','n')) != NULL) {
104     if (absolute && lengthset)
105       justcreate=1;
106     else {
107       fprintf(stderr,"\nThe -0 flag requires -l and -r\n\n");
108       exit(MAKENOISE__FLAGS_REQUIRED);
109     }
110   }
111
112   if ((out=check_option(in,n,'V','u')) != NULL)
113     sscanf(out,"%u",&verbosity);
114   if ((out=check_option(in,n,'o','o')) != NULL) {
115     stout=0;
116     if (strlen(out) > 0)
117       outfile=out;
118   }
119 }
120
121 void equidistri(double sigmax,unsigned int which) 
122 {
123   int i;
124   double limit,equinorm;
125   
126   equinorm=(double)ULONG_MAX;
127   if (!absolute)
128     limit=2.0*sqrt(3.0)*sigmax*noiselevel;
129   else
130     limit=2.0*noiselevel;
131   for (i=0;i<length;i++)
132     array[which][i] += (limit*((double)rnd_1279()/equinorm-0.5));
133
134
135 void gauss(double sigmax,unsigned int which)
136 {
137   int i;
138   double glevel;
139
140   if (!absolute)
141     glevel=noiselevel*sigmax;
142   else
143     glevel=noiselevel;
144   for (i=0;i<length;i++)
145     array[which][i] += gaussian(glevel);
146 }
147
148 int main(int argc,char** argv)
149 {
150   char stdi=0;
151   unsigned long i,j;
152   double av=0.0,*sigmax;
153   FILE *fout;
154
155   if (scan_help(argc,argv))
156     show_options(argv[0]);
157
158   scan_options(argc,argv);
159 #ifndef OMIT_WHAT_I_DO
160   if (verbosity&VER_INPUT)
161     what_i_do(argv[0],WID_STR);
162 #endif
163
164   if (!justcreate) {
165     infile=search_datafile(argc,argv,NULL,verbosity);
166     if (infile == NULL)
167       stdi=1;
168   }
169   else
170     stdi=1;
171
172   if (outfile == NULL) {
173     if (!stdi) {
174       check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
175       strcpy(outfile,infile);
176       strcat(outfile,".noi");
177     }
178     else {
179       check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
180       strcpy(outfile,"stdin.noi");
181     }
182   }
183   if (!stout)
184     test_outfile(outfile);
185
186   if (!justcreate) {
187     if (column == NULL)
188       array=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
189                                        verbosity);
190     else
191       array=(double**)get_multi_series(infile,&length,exclude,&dim,column,
192                                        dimset,verbosity);
193   }
194   else {
195     check_alloc(array=(double**)malloc(sizeof(double*)*dim));
196     for (i=0;i<dim;i++) {
197       check_alloc(array[i]=(double*)malloc(sizeof(double)*length));
198       for (j=0;j<length;j++)
199         array[i][j]=0.0;
200     }
201   }
202
203   check_alloc(sigmax=(double*)malloc(sizeof(double)*dim));
204
205   if (!absolute) {
206     for (j=0;j<dim;j++)
207       variance(array[j],length,&av,&sigmax[j]);
208   }
209
210   rnd_init(iseed);
211
212   for (i=0;i<10000;i++) rnd_1279();
213
214   for (j=0;j<dim;j++) {
215     if (!cgaussian)
216       equidistri(sigmax[j],j);
217     else
218       gauss(sigmax[j],j);
219   }
220
221   if (!stout) {
222     fout=fopen(outfile,"w");
223     if (verbosity&VER_INPUT)
224       fprintf(stderr,"Opened %s for writing\n",outfile);
225     for (i=0;i<length;i++) {
226       for (j=0;j<dim-1;j++)
227         fprintf(fout,"%e ",array[j][i]);
228       fprintf(fout,"%e\n",array[dim-1][i]);
229     }
230     fclose(fout);
231   }
232   else {
233     if (verbosity&VER_INPUT)
234       fprintf(stderr,"Writing to stdout\n");
235     for (i=0;i<length;i++) {
236       for (j=0;j<dim-1;j++)
237         fprintf(stdout,"%e ",array[j][i]);
238       fprintf(stdout,"%e\n",array[dim-1][i]);
239     }
240   }
241
242   for (i=0;i<dim;i++)
243     free(array[i]);
244   free(array);
245   free(sigmax);
246   if (outfile != NULL)
247     free(outfile);
248   if (infile != NULL)
249     free(infile);
250   if (column != NULL)
251     free(column);
252
253   return 0;
254 }