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 May 27, 2000 */
26 #include "routines/tsa.h"
28 #define WID_STR "Savitzky-Golay filter: Filters the data or estimates\n\t\
29 filtered derivatives, respectively."
31 unsigned long length=ULONG_MAX,exclude=0;
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);
41 void show_options(char *progname)
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"
48 " datafile.\nIf no datafile is given stdin is read. Just - also"
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");
63 fprintf(stderr,"\t-h show these options\n");
68 void scan_options(int n,char **argv)
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)
78 if ((out=check_option(argv,n,'m','u')) != NULL) {
79 sscanf(out,"%u",&dim);
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) {
97 double** make_coeff(void)
100 double **mat,**imat,**rmat;
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)));
109 for (i=0;i<=power;i++)
110 for (j=0;j<=power;j++) {
112 for (k= -(int)nb;k<=(int)nf;k++)
113 mat[i][j] += pow((double)k,(double)(i+j));
116 imat=invert_matrix(mat,(power+1));
118 for (i=0;i<=power;i++)
119 for (j=0;j<=(nb+nf);j++) {
121 for (k=0;k<=power;k++)
122 rmat[i][j] += imat[i][k]*pow((double)(j-(int)nb),(double)k);
125 for (i=0;i<=power;i++) {
135 double make_norm(void)
140 for (i=2;i<=deriv;i++)
146 int main(int argc,char **argv)
150 double **coeff,help,norm;
153 if (scan_help(argc,argv))
154 show_options(argv[0]);
156 scan_options(argc,argv);
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);
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);
169 #ifndef OMIT_WHAT_I_DO
170 if (verbosity&VER_INPUT)
171 what_i_do(argv[0],WID_STR);
174 infile=search_datafile(argc,argv,NULL,verbosity);
178 if (outfile == NULL) {
180 check_alloc(outfile=(char*)calloc(strlen(infile)+4,(size_t)1));
181 sprintf(outfile,"%s.sg",infile);
184 check_alloc(outfile=(char*)calloc((size_t)9,(size_t)1));
185 sprintf(outfile,"stdin.sg");
189 test_outfile(outfile);
192 series=(double**)get_multi_series(infile,&length,exclude,&dim,"",
195 series=(double**)get_multi_series(infile,&length,exclude,&dim,
196 columns,dimset,verbosity);
204 fprintf(stdout,"%e ",(deriv==0)?series[d][i]:0.0);
205 fprintf(stdout,"\n");
207 for (i=(long)nb;i<length-(long)nf;i++) {
208 for (d=0;d<dim;d++) {
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);
214 fprintf(stdout,"\n");
216 for (i=length-(long)nf;i<length;i++) {
218 fprintf(stdout,"%e ",(deriv==0)?series[d][i]:0.0);
219 fprintf(stdout,"\n");
223 fout=fopen(outfile,"w");
226 fprintf(fout,"%e ",(deriv==0)?series[d][i]:0.0);
229 for (i=(long)nb;i<length-(long)nf;i++) {
230 for (d=0;d<dim;d++) {
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);
238 for (i=length-(long)nf;i<length;i++) {
240 fprintf(fout,"%e ",(deriv==0)?series[d][i]:0.0);
252 for (i=0;i<=power;i++)