-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing.h
76 lines (58 loc) · 1.48 KB
/
processing.h
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
#ifndef PROCESSING_H
#define PROCESSING_H
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <cmath>
void vec_to_file(const std::vector<double>& dt, const char* f_name){
FILE* fp = fopen(f_name, "w");
if (!fp){
printf("Cant create the file!\n");
exit(1);
}
for (auto i : dt){
fprintf(fp, "%f\n", i);
}
fclose(fp);
}
std::vector<double> log_return(const std::vector<double>& dt){
std::vector<double> lg_return;
for (int i = 1; i < dt.size(); i++){
double log_return = log(dt[i]/dt[i-1]);
lg_return.push_back(log_return);
}
return lg_return;
}
std::vector<double> adj_return(const char* f_name){
std::vector<double> adj_return;
//Read the file
FILE* fp = fopen(f_name, "r");
if (!fp){
printf("Can't open file %s\n", f_name);
exit(1);
}
else{
char buffer[1024];
int row = 0;
int column = 0;
while (fgets(buffer, 1024, fp)){
column = 0;
row++;
if (row == 1)
continue;
char* value = strtok(buffer, ", ");
while (value){
if (column == 5){
float num = atof(value);
adj_return.push_back(num);
}
value = strtok(NULL, ", ");
column++;
}
}
fclose(fp);
}
return adj_return;
}
#endif