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: Jul 26, 2004 */
26 #include "routines/tsa.h"
28 #define WID_STR "Performs a global PCA"
30 unsigned long LENGTH=ULONG_MAX,exclude=0;
31 unsigned int DIM=2,EMB=1,dimemb,LDIM=2,DELAY=1;
32 unsigned int verbosity=0xff;
33 char *outfile=NULL,stout=1,dim_set=0;
34 unsigned int what_to_write=0,write_values=1,write_vectors=0;
35 unsigned int write_comp=0,write_proj=0;
36 unsigned int projection_set=0;
37 char *infile=NULL,dimset=0,*column=NULL;
40 void show_options(char *progname)
42 what_i_do(progname,WID_STR);
43 fprintf(stderr," Usage: %s [options]\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 data to use [Default: whole file]\n");
50 fprintf(stderr,"\t-x # of lines to be ignore [Default: 0]\n");
51 fprintf(stderr,"\t-c columns to read [Default: 2]\n");
52 fprintf(stderr,"\t-m columns,embedding dim. to use [Default: 2,1]\n");
53 fprintf(stderr,"\t-d delay to use [Default: 1]\n");
54 fprintf(stderr,"\t-q projection dimension [Default: no projection]\n");
55 fprintf(stderr,"\t-W # what to write: [Default: 0]\n"
56 "\t\t0 write eigenvalues only\n"
57 "\t\t1 write eigenvectors\n"
58 "\t\t2 write (projected) pca components\n"
59 "\t\t3 write projected data\n");
60 fprintf(stderr,"\t-o output file name \n\t\t[Default: stdout; -o without "
61 "value means 'datafile'.pca]\n");
62 fprintf(stderr,"\t-V verbosity level [Default: 1]\n\t\t"
63 "0='only panic messages'\n\t\t"
64 "1='+ input/output messages'\n");
65 fprintf(stderr,"\t-h show these options\n");
69 void scan_options(int n,char **in)
73 if ((out=check_option(in,n,'l','u')) != NULL)
74 sscanf(out,"%lu",&LENGTH);
75 if ((out=check_option(in,n,'x','u')) != NULL)
76 sscanf(out,"%lu",&exclude);
77 if ((out=check_option(in,n,'c','s')) != NULL)
79 if ((out=check_option(in,n,'m','2')) != NULL) {
80 sscanf(out,"%u,%u",&DIM,&EMB);
83 if ((out=check_option(in,n,'d','u')) != NULL)
84 sscanf(out,"%u",&DELAY);
85 if ((out=check_option(in,n,'q','u')) != NULL) {
86 sscanf(out,"%u",&LDIM);
89 if ((out=check_option(in,n,'V','u')) != NULL)
90 sscanf(out,"%u",&verbosity);
91 if ((out=check_option(in,n,'W','u')) != NULL) {
92 sscanf(out,"%u",&what_to_write);
93 switch(what_to_write) {
94 case 0: write_values=1;break;
95 case 1: write_values=0;write_vectors=1;break;
96 case 2: write_values=0;write_comp=1;break;
97 case 3: write_values=0;write_proj=1;break;
99 fprintf(stderr,"Wrong value for the -W flag. Exiting!\n");
104 if ((out=check_option(in,n,'o','o')) != NULL) {
111 void ordne(double *lyap,int *ord)
116 for (i=0;i<dimemb;i++)
119 for (i=0;i<dimemb-1;i++)
120 for (j=i+1;j<dimemb;j++)
121 if (lyap[i] < lyap[j]) {
131 void make_pca(double *av)
133 unsigned int i,j,k,i1,i2,j1,j2,k1,k2;
135 double **mat,*matarray,*eig,*sp,hsp=0.0;
138 check_alloc(ord=(int*)malloc(sizeof(int)*dimemb));
139 check_alloc(eig=(double*)malloc(sizeof(double)*dimemb));
140 check_alloc(matarray=(double*)malloc(sizeof(double)*dimemb*dimemb));
141 check_alloc(mat=(double**)malloc(sizeof(double*)*dimemb));
142 for (i=0;i<dimemb;i++)
143 mat[i]=(double*)(matarray+i*dimemb);
146 for (i=0;i<dimemb;i++) {
149 for (j=i;j<dimemb;j++) {
153 for (k=(EMB-1)*DELAY;k<LENGTH;k++)
154 mat[i][j] += series[i1][k-i2]*series[j1][k-j2];
155 mat[j][i]=(mat[i][j] /= (double)(LENGTH-(EMB-1)*DELAY));
159 eigen(mat,(unsigned long)dimemb,eig);
163 fout=fopen(outfile,"w");
164 if (verbosity&VER_INPUT)
165 fprintf(stderr,"Opened %s for writing\n",outfile);
168 if (verbosity&VER_INPUT)
169 fprintf(stderr,"Writing to stdout\n");
172 for (i=0;i<dimemb;i++)
175 fprintf(stdout,"%d %e\n",i,eig[i]);
177 fprintf(fout,"%d %e\n",i,eig[i]);
182 fprintf(stdout,"#%d %e\n",i,eig[i]);
184 fprintf(fout,"#%d %e\n",i,eig[i]);
188 for (i=0;i<dimemb;i++) {
189 for (j=0;j<dimemb;j++) {
192 fprintf(stdout,"%e ",mat[i][j1]);
194 fprintf(fout,"%e ",mat[i][j1]);
197 fprintf(stdout,"\n");
204 for (i=(EMB-1)*DELAY;i<LENGTH;i++) {
205 for (j=0;j<LDIM;j++) {
208 for (k=0;k<dimemb;k++) {
211 hsp += mat[k][j1]*(series[k1][i-k2]+av[k1]);
214 fprintf(stdout,"%e ",hsp);
216 fprintf(fout,"%e ",hsp);
219 fprintf(stdout,"\n");
226 check_alloc(sp=(double*)malloc(sizeof(double)*LDIM));
227 for (i=0;i<(EMB-1)*DELAY;i++) {
230 fprintf(stdout,"%e ",series[j][i]+av[j]);
232 fprintf(fout,"%e ",series[j][i]+av[j]);
234 fprintf(stdout,"\n");
238 for (i=(EMB-1)*DELAY;i<LENGTH;i++) {
239 for (j=0;j<LDIM;j++) {
242 for (k=0;k<dimemb;k++) {
245 sp[j] += mat[k][j1]*series[k1][i-k2];
248 for (j=0;j<DIM;j++) {
250 for (k=0;k<LDIM;k++) {
252 hsp += mat[j*EMB][k1]*sp[k];
255 fprintf(stdout,"%e ",hsp+av[j]);
257 fprintf(fout,"%e ",hsp+av[j]);
260 fprintf(stdout,"\n");
271 int main(int argc,char **argv)
277 if (scan_help(argc,argv))
278 show_options(argv[0]);
280 scan_options(argc,argv);
282 #ifndef OMIT_WHAT_I_DO
283 if (verbosity&VER_INPUT)
284 what_i_do(argv[0],WID_STR);
287 infile=search_datafile(argc,argv,NULL,verbosity);
291 if (outfile == NULL) {
293 check_alloc(outfile=(char*)calloc(strlen(infile)+5,(size_t)1));
294 strcpy(outfile,infile);
295 strcat(outfile,".pca");
298 check_alloc(outfile=(char*)calloc((size_t)10,(size_t)1));
299 strcpy(outfile,"stdin.pca");
303 test_outfile(outfile);
306 series=(double**)get_multi_series(infile,&LENGTH,exclude,&DIM,"",dimset,
309 series=(double**)get_multi_series(infile,&LENGTH,exclude,&DIM,column,
315 if (LDIM < 1) LDIM=1;
316 if (LDIM > dimemb) LDIM=dimemb;
319 check_alloc(av=(double*)malloc(sizeof(double)*DIM));
320 for (j=0;j<DIM;j++) {
322 variance(series[j],LENGTH,&av[j],&rms);
323 for (i=0;i<LENGTH;i++)
324 series[j][i] -= av[j];