-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrelation-main.c
256 lines (178 loc) · 6.8 KB
/
correlation-main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
FILE NAME: correlation-main.c
DESCRIPTION: This Code calculates the correlation dimention of the data
stored on a given file passed by arguments on argv[1], and
with his correlation dimension passed as argv[2].
The Maximum data input is 25000, it can be modified on the
#define MAXDATA
AUTHOR: Daniel Mejia Raigosa
DATE: 17, May 2011 (Date of Creation)
VERSION: 2.2.2
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "commonroutines.c"
#define ARGS 7 // Number of Maximum Arguments (1 means no arguments!)
#define TITLESCN "Correlation Dimension" // Program Title
#define VER "2.2.1" // Version of the code
#define ANO "2011" // Date of code
#define MAXEMB 10 // Maximum Embedding Dimension
#define THEILER 10 // Theiler Correction for Correlation Algorithm
double corsum(double data[],int size, int dim, int tau,double e)
{
/** i recorre la SdT con j un paso más adelante como
dice la ecuación
C(e)=(2/(N(N-1)))Sum_i Sum_{j=i+1}
*/
double csum,norm=0,sum=0;
int i,j=1,d;
for(i=0;i<size;i+=1)
{
for(j=(i+THEILER);j<size;j+=THEILER)
{
for(d=0;d<dim;d++)
{
norm+=pow(data[i+(dim-d)*tau]-data[j+(dim-d)*tau],2);
// Apply the Theiler Correction W(THEILER) such that |j-i| > W
if( abs(j-i)<THEILER )
{
j=j+THEILER;
}
}
sum+=heaviside(e-norm);
norm=0;
}
}
csum=( 2.0/(size*(size-1.0)) )*sum;
//printf("sum = %lf size=%d \t csum=%.19lf",sum,size,csum );
return csum;
}
/* ************************** MAIN FUNCTION ************************** */
main(int argc,char *argv[]) // for input arguments, remeber argv[0]=program name argv[1]= first argument and so on
{
/* This section is made for argument validation */
if(argc > ARGS)
{
printf("Too many arguments supplied.\n");
printf(" Usage\'$ %s file-with-data.ext\' dim t r R e\n",argv[0]);
printf("\n \t dim - Embedding Dimension\n \t t Tau Sepparation\n \t r - Minimum neighbor radious\n \t R - Maximum neighbor radious\n \t e - Epsilon step\n");
printf(" If embedding-dimension = 0 the program makes various embeddings on \n file-with-data\n\n");
exit(1);
}
else if (argc<ARGS)
{
printf("Some arguments expected.\n");
printf(" Usage\'$ %s file-with-data.ext\' dim t r R e\n",argv[0]);
printf("\n \t dim - Embedding Dimension\n \t t Tau Sepparation\n \t r - Minimum neighbor radious\n \t R - Maximum neighbor radious\n \t e - Epsilon step\n");
printf(" If embedding-dimension = 0 the program makes various embeddings on \n file-with-data\n\n");
exit(1);
}
/* Here begins the interesting part */
//variable definition
double epsilon,csum,x[MAXDATA],MINE,PASO,LIMITE;
int cont,N,i,j,dim,tau; // int max value 2,147,483,647 use long int for more data
char originfn[CHAPER],filename[CHAPER],fileext[CHAPER],dimarg[2];
time_t t1,t2,t3,t4;
FILE *salida;
characternumber(argv[1],CHAPER); //Confirm Character allowed values
strcpy(originfn,argv[1]); //Convert file names
dim=atoi(argv[2]);
tau=atoi(argv[3]);
MINE=atof(argv[4]);
LIMITE=atof(argv[5]);
PASO=atof(argv[6]);
if(PASO<=0)
{
PASO=0.01;
}
if(MINE<=0)
{
MINE=0.01;
}
if(LIMITE<=0 || LIMITE>1100)
{
LIMITE=20;
}
system("clear"); //clear screen
pimpi(TITLESCN,25); //pimps start up
/* ************************** IF NO DIMENSION GIVEN ************************** */
if (dim==0)
{
printf("\n This tool will calculate the correlation sum \n And the correlation dimension...\n\n");
printf(" * Data origin file: \'%s\'\n\n",originfn);
printf(" * Maximal Embedding dimension: %d\n",MAXEMB);
printf(" [!] I'll make different embeddings and then calculate the convergence\n of the correlation sum for various epsilon values\n\n");
printf(" * Epsilon values range : %lf to %lf \t step = %lf\n",MINE,LIMITE,PASO);
(void)time(&t3);
datadquire(originfn,x,&N);
printf("\n * Total Readed Data Lines : %d \n",N);
dim=1;
strcpy(filename,strtok(argv[1],".")); // separate data files
strcpy(fileext,strstr(originfn,"."));
strcpy(filename,strcat(filename,"-cor")); //create a filename
strcat(filename,dimarg);
strcat(filename,fileext);
opendatafile(&salida,filename,"w");
while(dim<MAXEMB+1)
{
(void) time(&t1);
printf("\n * Making Embedding %d of %d \n",dim,MAXEMB);
printf("\n * Calculating correlation sum now...\n \t It will took a while, be patient...\n");
fprintf(salida,"### Embedding dimension = %d\n### log(e) log(C(e))\n",dim);
epsilon=LIMITE;
while (epsilon>MINE)
{
csum=corsum(x,N,dim,5,epsilon);
fprintf(salida,"%.6E %.6E\n",log(epsilon),log(csum));
epsilon=epsilon-PASO;
}
fprintf(salida,"\n");
dim=dim+1;
(void)time(&t2);
printf("\n * It took me %ld seconds to process the data",(t2-t1));
}
fclose(salida);
(void)time(&t4);
printf("\n\n * Results saved on file \'%s\'\n",filename);
printf("\n * It took %ld minutes to overall process \n",(t4-t3)/60);
pimpe(TITLESCN,25);
exit(0);
}
/* ************************** DIMENSION GIVEN ************************** */
else
{
printf("\n This tool will calculate the correlation sum \n And the correlation dimension,\n Making a Time Delay Embbeding");
printf(" * Data origin file: \'%s\'\n\n",originfn);
printf(" * Embedding dimension: %d\n",dim);
printf(" * Time Lag : %d\n",tau);
printf(" [!] I'll calculate the convergence of the correlation sum \n for various epsilon values\n\n");
printf(" * Epsilon values range : %lf to %lf - step = %lf\n",MINE,LIMITE,PASO);
(void)time(&t3);
datadquire(originfn,x,&N);
printf("\n * Total Readed Data Lines : %d \n",N);
strcpy(filename,strtok(argv[1],".")); // separate data files
strcpy(fileext,strstr(originfn,"."));
strcpy(filename,strcat(filename,"-cor")); //create a filename
strcat(filename,fileext);
opendatafile(&salida,filename,"w");
(void) time(&t1);
printf("\n * Calculating correlation sum now...\n \t It will took a while, be patient...\n");
fprintf(salida,"### Embedding dimension = %d\n### log(e) log(C(e))\n",dim);
epsilon=LIMITE;
while (epsilon>MINE)
{
csum=corsum(x,N,dim,5,epsilon);
fprintf(salida,"%.6E %.6E\n",log(epsilon),log(csum));
epsilon=epsilon-PASO;
}
fclose(salida);
(void)time(&t2);
printf("\n\n * Results saved on file \'%s\'\n",filename);
(void)time(&t4);
printf("\n * It took %ld minutes to overall process \n",(t4-t3)/60);
pimpe(TITLESCN,25);
exit(0);
}
} //End of Main Code