-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
179 lines (155 loc) · 5.17 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <readline/readline.h>
#include <getopt.h>
#include <string.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#define PRODUCT "TSutils - Time Series Generator\n\
Copyright (C) 2012 University of Trento\n\n"
#define STD 1 // Standard deviation
void z_normalize(float *ts, int size);
void inline z_normalize(float *ts, int size) {
int i;
float mean = 0;//gsl_stats_mean(ts, 1, size);
float std = 0;//gsl_stats_sd(ts, 1, size);
for (i=0; i<size; i++)
{
mean += ts[i];
}
mean /= size;
for (i=0; i<size; i++)
{
std += (ts[i] - mean) * (ts[i] - mean);
}
std /= size;
std = sqrt(std);
for (i = 0; i < size; i++)
{
ts[i] = (ts[i] - mean) / std;
}
}
float * generate (float *ts, int size, gsl_rng * r, char normalize) {
int i;
float x = 0, dx;
for (i = 0; i < size; i++)
{
dx = gsl_ran_gaussian (r, STD); // mean=0, std=STD
x += dx;
ts[i] = x;
}
if(normalize == 1)
{
z_normalize(ts, size);
}
return ts;
}
/**
Parses the command line arguments.
**/
void parse_args (int argc, char **argv, int *length, int *number_of_timeseries,
float *skew_frequency, char *normalize, char ** filename) {
while (1)
{
static struct option long_options[] = {
{"skew-frequency", required_argument, 0, 'f'},
{"length", required_argument, 0, 'l'},
{"size", required_argument, 0, 's'},
{"filename", required_argument, 0, 'o'},
{"z-normalize", no_argument, 0, 'z'},
{"help", no_argument, 0, 'h'},
{NULL, 0, NULL, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
int c = getopt_long (argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'f':
*skew_frequency = atof(optarg);
break;
case 's':
*number_of_timeseries = atoi(optarg);
break;
case 'l':
*length = atoi(optarg);
break;
case 'z':
*normalize = 1;
break;
case 'o':
*filename = optarg;
break;
case 'h':
printf(PRODUCT);
printf("Usage:\n\
\t--size XX \t\tThe number of time series to generate\n\
\t--length XX \t\tThe length of each time series\n\
\t--skew-frequency XX \tThe skewness frequency\n\
\t--z-normalize \t\tUse to enable z-normalization\n\
\t--filename \t\t The output filename\n\
\t--help\n\n");
exit(-1);
break;
default:
exit(-1);
break;
}
}
}
/**
Generates a set of random time series.
**/
void generate_random_timeseries(int length, int number_of_timeseries,
char normalize, int repetition, char * filename) {
// Initialize random number generation
const gsl_rng_type * T;
gsl_rng * r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);// creates an instance of the Tausworthe generator
FILE * data_file;
data_file = fopen (filename,"w");
float *ts = (float *)(malloc(sizeof(float) * length));
int i, j, rep;
for (i=1; i<=number_of_timeseries; i+=repetition)
{
generate(ts, length, r, normalize);
for(rep=0; rep<repetition; rep++)
{
fwrite(ts, sizeof(float), length,data_file);
}
if(i % (1000 * repetition) == 0) {
fprintf(stderr,"\r\x1b[m>> Generating: \x1b[36m%2.2lf%%\x1b[0m",(float) ((float)i/(float)number_of_timeseries) * 100);
}
}
fprintf(stderr, "\n");
// Finalize random number generator
fclose (data_file);
gsl_rng_free (r);
}
int main(int argc, char **argv) {
// Initialize variables
int length = 0; // Length of a single time series
int number_of_timeseries = 0; // Number of time series to generate
float skew_frequency = 0; // The skew frequency
int repetition = 1; // How many times each time series is repeated
char normalize = 0; // Normalize or not.
char * filename = "./output.bin";
// Parse command line arguments
parse_args(argc, argv, &length, &number_of_timeseries, &skew_frequency, &normalize,&filename);
fprintf(stderr,PRODUCT);
if((1-skew_frequency) > 0)
repetition = number_of_timeseries / (number_of_timeseries * (1-skew_frequency));
else
repetition = number_of_timeseries;
fprintf(stderr, ">> Generating random time series...of size = %uKB each\n",sizeof(float)*256/1024);
fprintf(stderr, ">> Data Filename: %s\n", filename);
generate_random_timeseries(length, number_of_timeseries, normalize, repetition,filename);
fprintf(stderr, ">> Done.\n");
return 0;
}