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 29, 2000 */
27 #include "routines/tsa.h"
29 #define WID_STR "Adds noise to a time series or just creates random numbers"
31 char *outfile=NULL,cgaussian,stout=1,justcreate=0;
33 char absolute=0,dimset=0;
34 unsigned long length=ULONG_MAX,exclude=0,iseed=3441341;
37 unsigned int verbosity=0xff;
38 double **array,noiselevel=0.05;
40 void show_options(char *progname)
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"
47 " datafile.\nIf no datafile is given stdin is read. Just - also"
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",
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");
72 void scan_options(int n,char** in)
74 char *out,lengthset=0;
76 if ((out=check_option(in,n,'l','u')) != NULL) {
77 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,'m','u')) != NULL) {
83 sscanf(out,"%u",&dim);
86 if ((out=check_option(in,n,'c','s')) != NULL)
88 if ((out=check_option(in,n,'%','f')) != NULL) {
89 sscanf(out,"%lf",&noiselevel);
92 if ((out=check_option(in,n,'r','f')) != NULL) {
93 sscanf(out,"%lf",&noiselevel);
96 if ((out=check_option(in,n,'g','n')) != NULL)
98 if ((out=check_option(in,n,'I','u')) != NULL) {
99 sscanf(out,"%lu",&iseed);
101 iseed=(unsigned long)time((time_t*)&iseed);
103 if ((out=check_option(in,n,'0','n')) != NULL) {
104 if (absolute && lengthset)
107 fprintf(stderr,"\nThe -0 flag requires -l and -r\n\n");
108 exit(MAKENOISE__FLAGS_REQUIRED);
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) {
121 void equidistri(double sigmax,unsigned int which)
124 double limit,equinorm;
126 equinorm=(double)ULONG_MAX;
128 limit=2.0*sqrt(3.0)*sigmax*noiselevel;
130 limit=2.0*noiselevel;
131 for (i=0;i<length;i++)
132 array[which][i] += (limit*((double)rnd_1279()/equinorm-0.5));
135 void gauss(double sigmax,unsigned int which)
141 glevel=noiselevel*sigmax;
144 for (i=0;i<length;i++)
145 array[which][i] += gaussian(glevel);
148 int main(int argc,char** argv)
152 double av=0.0,*sigmax;
155 if (scan_help(argc,argv))
156 show_options(argv[0]);
158 scan_options(argc,argv);
159 #ifndef OMIT_WHAT_I_DO
160 if (verbosity&VER_INPUT)
161 what_i_do(argv[0],WID_STR);
165 infile=search_datafile(argc,argv,NULL,verbosity);
172 if (outfile == NULL) {
174 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
175 strcpy(outfile,infile);
176 strcat(outfile,".noi");
179 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
180 strcpy(outfile,"stdin.noi");
184 test_outfile(outfile);
188 array=(double**)get_multi_series(infile,&length,exclude,&dim,"",dimset,
191 array=(double**)get_multi_series(infile,&length,exclude,&dim,column,
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++)
203 check_alloc(sigmax=(double*)malloc(sizeof(double)*dim));
207 variance(array[j],length,&av,&sigmax[j]);
212 for (i=0;i<10000;i++) rnd_1279();
214 for (j=0;j<dim;j++) {
216 equidistri(sigmax[j],j);
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]);
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]);