Fix core WST file
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / sav_gol.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 May 27, 2000 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include <limits.h>
26 #include "routines/tsa.h"
27
28 #define WID_STR "Savitzky-Golay filter: Filters the data or estimates\n\t\
29 filtered derivatives, respectively."
30
31 unsigned long length=ULONG_MAX,exclude=0;
32 unsigned int dim=1;
33 char dimset=0;
34 char *columns=NULL;
35 unsigned int nf=2,nb=2,power=2,deriv=0;
36 char *infile=NULL,*outfile=NULL,stdo=1;
37 unsigned int verbosity=(VER_INPUT|VER_FIRST_LINE);
38
39 double **series;
40
41 void show_options(char *progname)
42 {
43   what_i_do(progname,WID_STR);
44   fprintf(stderr,"  Usage: %s [options]\n",progname);
45   fprintf(stderr,"  Options:\n");
46   fprintf(stderr,"Everything not being a valid option will be interpreted"
47           " as a possible"
48           " datafile.\nIf no datafile is given stdin is read. Just - also"
49           " means stdin\n");
50   fprintf(stderr,"\t-l datapoints [default is whole file]\n");
51   fprintf(stderr,"\t-x exclude # points [default %ld]\n",exclude);
52   fprintf(stderr,"\t-c columns [default 1]\n");
53   fprintf(stderr,"\t-m no. of components [default %d]\n",dim);
54   fprintf(stderr,"\t-n nb,nf [default %u,%u]\n",nb,nf);
55   fprintf(stderr,"\t-p power of the polynomial [default %u]\n",power);
56   fprintf(stderr,"\t-D order of the estimated derivative [default %u]\n",deriv);
57   fprintf(stderr," \t-o outfile [default 'datafile'.sg; Without -o data"
58           " is written to stdout]\n");
59   fprintf(stderr,"\t-V verbosity level [default: 1]\n\t\t"
60           "0='only panic messages'\n\t\t"
61           "1='+ input/output messages'\n");
62   
63   fprintf(stderr,"\t-h show these options\n");
64   fprintf(stderr,"\n");
65   exit(0);
66 }
67
68 void scan_options(int n,char **argv)
69 {
70   char *out;
71
72   if ((out=check_option(argv,n,'l','u')) != NULL)
73     sscanf(out,"%lu",&length);
74   if ((out=check_option(argv,n,'x','u')) != NULL)
75     sscanf(out,"%lu",&exclude);
76   if ((out=check_option(argv,n,'c','s')) != NULL)
77     columns=out;
78   if ((out=check_option(argv,n,'m','u')) != NULL) {
79     sscanf(out,"%u",&dim);
80     dimset=1;
81   }
82   if ((out=check_option(argv,n,'n','2')) != NULL)
83     sscanf(out,"%u,%u",&nb,&nf);
84   if ((out=check_option(argv,n,'p','u')) != NULL)
85     sscanf(out,"%u",&power);
86   if ((out=check_option(argv,n,'D','u')) != NULL)
87     sscanf(out,"%u",&deriv);
88   if ((out=check_option(argv,n,'V','u')) != NULL)
89     sscanf(out,"%u",&verbosity);
90   if ((out=check_option(argv,n,'o','o')) != NULL) {
91     stdo=0;
92     if (strlen(out) > 0)
93       outfile=out;
94   }
95 }
96
97 double** make_coeff(void)
98 {
99   long i,j,k;
100   double **mat,**imat,**rmat;
101   
102   check_alloc(mat=(double**)malloc(sizeof(double*)*(power+1)));
103   for (i=0;i<=power;i++)
104     check_alloc(mat[i]=(double*)malloc(sizeof(double)*(power+1)));
105   check_alloc(rmat=(double**)malloc(sizeof(double*)*(power+1)));
106   for (i=0;i<=power;i++)
107     check_alloc(rmat[i]=(double*)malloc(sizeof(double)*(nb+nf+1)));
108   
109   for (i=0;i<=power;i++)
110     for (j=0;j<=power;j++) {
111       mat[i][j]=0.0;
112       for (k= -(int)nb;k<=(int)nf;k++)
113         mat[i][j] += pow((double)k,(double)(i+j));
114     }
115
116   imat=invert_matrix(mat,(power+1));
117   
118   for (i=0;i<=power;i++)
119     for (j=0;j<=(nb+nf);j++) {
120       rmat[i][j]=0.0;
121       for (k=0;k<=power;k++)
122         rmat[i][j] += imat[i][k]*pow((double)(j-(int)nb),(double)k);
123     }
124   
125   for (i=0;i<=power;i++) {
126     free(mat[i]);
127     free(imat[i]);
128   }
129   free(mat);
130   free(imat);
131
132   return rmat;
133 }
134
135 double make_norm(void)
136 {
137   double ret=1.0;
138   long i;
139
140   for (i=2;i<=deriv;i++)
141     ret *= (double)i;
142
143   return 1.0/ret;
144 }
145
146 int main(int argc,char **argv)
147 {
148   char stdi=0;
149   long i,j,d;
150   double **coeff,help,norm;
151   FILE *fout;
152
153   if (scan_help(argc,argv)) 
154     show_options(argv[0]);
155   
156   scan_options(argc,argv);
157
158   if (power >= (nb+nf+1)) {
159     fprintf(stderr,"With these settings for the -n and -p flags,\nthe"
160             " system is underdetermined. Exiting\n\n");
161     exit(SAV_GOL__UNDERDETERMINED);
162   }
163   if (deriv > power) {
164     fprintf(stderr,"The order of the derivative must not be larger\nthan"
165             " the power of polynomial. Exiting\n\n");
166     exit(SAV_GOL__TOO_LARGE_DERIVATIVE);
167   }
168
169 #ifndef OMIT_WHAT_I_DO
170   if (verbosity&VER_INPUT)
171     what_i_do(argv[0],WID_STR);
172 #endif
173
174   infile=search_datafile(argc,argv,NULL,verbosity);
175   if (infile == NULL)
176     stdi=1;
177
178   if (outfile == NULL) {
179     if (!stdi) {
180       check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1));
181       sprintf(outfile,"%s.sg",infile);
182     }
183     else {
184       check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1));
185       sprintf(outfile,"stdin.sg");
186     }
187   }
188   if (!stdo)
189     test_outfile(outfile);
190
191   if (columns == NULL)
192     series=(double**)get_multi_series(infile,&length,exclude,&dim,"",
193                                       dimset,verbosity);
194   else
195     series=(double**)get_multi_series(infile,&length,exclude,&dim,
196                                       columns,dimset,verbosity);
197   
198   coeff=make_coeff();
199   norm=make_norm();
200
201   if (stdo) {
202     for (i=0;i<nb;i++) {
203       for (d=0;d<dim;d++)
204         fprintf(stdout,"%e ",(deriv==0)?series[d][i]:0.0);
205       fprintf(stdout,"\n");
206     }
207     for (i=(long)nb;i<length-(long)nf;i++) {
208       for (d=0;d<dim;d++) {
209         help=0.0;
210         for (j= -(long)nb;j<=(long)nf;j++)
211           help += coeff[deriv][j+nb]*series[d][i+j];
212         fprintf(stdout,"%e ",help*norm);
213       }
214       fprintf(stdout,"\n");
215     }
216     for (i=length-(long)nf;i<length;i++) {
217       for (d=0;d<dim;d++)
218         fprintf(stdout,"%e ",(deriv==0)?series[d][i]:0.0);
219       fprintf(stdout,"\n");
220     }
221   }
222   else {
223     fout=fopen(outfile,"w");
224     for (i=0;i<nb;i++) {
225       for (d=0;d<dim;d++)
226         fprintf(fout,"%e ",(deriv==0)?series[d][i]:0.0);
227       fprintf(fout,"\n");
228     }
229     for (i=(long)nb;i<length-(long)nf;i++) {
230       for (d=0;d<dim;d++) {
231         help=0.0;
232         for (j= -(long)nb;j<=(long)nf;j++)
233           help += coeff[deriv][j+nb]*series[d][i+j];
234         fprintf(fout,"%e ",help*norm);
235       }
236       fprintf(fout,"\n");
237     }
238     for (i=length-(long)nf;i<length;i++) {
239       for (d=0;d<dim;d++)
240         fprintf(fout,"%e ",(deriv==0)?series[d][i]:0.0);
241       fprintf(fout,"\n");
242     }
243     fclose(fout);
244   }
245
246   for (i=0;i<dim;i++)
247     free(series[i]);
248   free(series);
249   free(outfile);
250   if (!stdi)
251     free(infile);
252   for (i=0;i<=power;i++)
253     free(coeff[i]);
254   free(coeff);
255
256   return 0;
257 }