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: Mar 11, 2002 */
26 #include "routines/tsa.h"
28 #define WID_STR "Resample the data"
30 unsigned long length=ULONG_MAX,exclude=0;
31 unsigned int column=1,order=4;
32 unsigned int verbosity=0xff;
33 char *outfile=NULL,stdo=1;
35 double *series,sampletime=0.5;
37 void show_options(char *progname)
39 what_i_do(progname,WID_STR);
40 fprintf(stderr," Usage: %s [options]\n",progname);
41 fprintf(stderr," Options:\n");
42 fprintf(stderr,"Everything not being a valid option will be interpreted"
44 " datafile.\nIf no datafile is given stdin is read. Just - also"
46 fprintf(stderr,"\t-l length of file [default is whole file]\n");
47 fprintf(stderr,"\t-x # of lines to be ignored [default is 0]\n");
48 fprintf(stderr,"\t-c column to read [default is 1]\n");
49 fprintf(stderr,"\t-s new sampling time (in units of the old one)"
50 " [default is %f]\n",sampletime);
51 fprintf(stderr,"\t-p order of the interpolation [default is %d]\n",order);
52 fprintf(stderr,"\t-o output file name [default is 'datafile'.rs]\n");
53 fprintf(stderr,"\t-V verbosity level [default is 1]\n\t\t"
54 "0='only panic messages'\n\t\t"
55 "1='+ input/output messages'\n");
56 fprintf(stderr,"\t-h show these options\n\n");
60 void scan_options(int argc,char **argv)
64 if ((out=check_option(argv,argc,'s','f')) != NULL)
65 sscanf(out,"%lf",&sampletime);
66 if ((out=check_option(argv,argc,'l','u')) != NULL)
67 sscanf(out,"%lu",&length);
68 if ((out=check_option(argv,argc,'x','u')) != NULL)
69 sscanf(out,"%lu",&exclude);
70 if ((out=check_option(argv,argc,'c','u')) != NULL)
71 sscanf(out,"%u",&column);
72 if ((out=check_option(argv,argc,'p','u')) != NULL)
73 sscanf(out,"%u",&order);
74 if ((out=check_option(argv,argc,'V','u')) != NULL)
75 sscanf(out,"%u",&verbosity);
76 if ((out=check_option(argv,argc,'o','o')) != NULL) {
83 int main(int argc,char **argv)
86 long i,j,itime,itime_old;
88 double **mat,*vec,**imat,*coef;
89 double time,htime,new_el;
92 if (scan_help(argc,argv))
93 show_options(argv[0]);
95 scan_options(argc,argv);
96 #ifndef OMIT_WHAT_I_DO
97 if (verbosity&VER_INPUT)
98 what_i_do(argv[0],WID_STR);
101 infile=search_datafile(argc,argv,&column,verbosity);
105 if (outfile == NULL) {
107 check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1));
108 strcpy(outfile,infile);
109 strcat(outfile,".rs");
112 check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1));
113 strcpy(outfile,"stdin.rs");
117 test_outfile(outfile);
119 series=(double*)get_series(infile,&length,exclude,column,verbosity);
122 horder2=(horder+1)/2-horder;
124 check_alloc(mat=(double**)malloc(sizeof(double*)*horder));
125 for (i=0;i<horder;i++)
126 check_alloc(mat[i]=(double*)malloc(sizeof(double)*horder));
127 check_alloc(vec=(double*)malloc(sizeof(double)*horder));
128 check_alloc(coef=(double*)malloc(sizeof(double)*horder));
130 for (i=0;i<horder;i++)
131 for (j=0;j<horder;j++)
132 mat[i][j]=pow((double)(horder2+i),(double)j);
134 imat=invert_matrix(mat,(unsigned int)horder);
137 file=fopen(outfile,"w");
138 if (verbosity&VER_INPUT)
139 fprintf(stderr,"Opened %s for writing\n",outfile);
142 if (verbosity&VER_INPUT)
143 fprintf(stderr,"Writing to stdout\n");
148 while (time < (double)(length-horder/2)) {
149 itime=(int)time+horder2;
150 if (itime != itime_old) {
151 for (i=0;i<horder;i++)
152 vec[i]=series[i+itime];
153 for (i=0;i<horder;i++) {
155 for (j=0;j<horder;j++)
156 coef[i] += imat[i][j]*vec[j];
160 htime=time-itime+horder2;
162 for (i=1;i<horder;i++)
163 new_el += coef[i]*pow(htime,(double)i);
165 fprintf(stdout,"%e\n",new_el);
167 fprintf(file,"%e\n",new_el);