-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_mode0.cpp
222 lines (194 loc) · 8.04 KB
/
plot_mode0.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
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
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include "matplotlibcpp.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <thread>
#include <iomanip>
#include <fstream>
#include <sstream>
namespace plt = matplotlibcpp;
using namespace std;
std::vector<string> pre_colors = {"b","g","r","c","m","y","k","b","g","r","c","m","y","k"};
std::vector<string> pre_lines = {"-","-","-","-","-","-","-",":",":",":",":",":",":",":"};
std::vector<string> pre_points = {"^","1","2","s","p","*","h","+","x","D","d","v","^","o"};
int main(int argc, char **argv)
{
if (argc == 1)
{
cout << "usage: "<<endl;
cout << " ./data_plot data1.txt data2.txt data3.txt plot_type win_size" << endl;
cout << "data file formate: "<<endl;
cout << " first line of the data: # [x-axis name] [y-axis name] [data name] plot_type \n"
<< " second line of the data(optional): * [line color] [mark type] \n"
"plot_type: \n"
" 1-----plot merg in one fig,"
" 2-----plot respectively \n"<<endl;
return -1;
}
//TODO load input param
int file_num = argc-3;
vector<string> file_path(file_num);
cout<<"plot file:"<<endl;
for (int i = 1; i < argc - 2; ++i)
{
file_path[i-1] = argv[i];
cout<<" "<<file_path[i-1]<<endl;
}
cout<<"plot type:"<<endl;
int plot_type = atoi(argv[argc-2]);
if(plot_type == 1)
cout<<" plot merg"<<endl;
else if(plot_type == 2)
cout<<" plot respectively"<<endl;
else{
cout<<" wrong input formate,please input plot type in lase param"<<endl;
return -1;
}
int win_size = atoi(argv[argc-1]);
//TODO 准备绘图信息变量
vector<vector<double>> x_data(file_num);
vector<vector<double>> x_inorder_data(file_num);
vector<vector<double>> y_data(file_num);
vector<vector<string>> data_label(file_num);
//TODO 准备读取文件流
vector<ifstream> file_in(file_num);
double tmp;
std::vector<double> x, max, mean, median, min, rmse, sse, std;
std::string config_file_line;
std::string cout_line;
double axis_iter = 0;
string s_data;
for (int file_idx = 0; file_idx < file_num; ++file_idx)
{
file_in[file_idx].open(file_path[file_idx]);
double data_cnt = 0;
bool config_axis = false;
bool config_linetype = false;
if(file_in[file_idx].is_open())
cout << "loading file:\n" << file_path[file_idx] << endl;
else{
cout << "can not open file:\n" << file_path[file_idx] << endl;
return -1;
}
int win_cnt = 0;
double _time_stemp = 0;
double _data = 0;
bool first = true;
double init_time;
while (std::getline(file_in[file_idx], config_file_line) && !file_in[file_idx].eof())
{
if (config_file_line.empty())
continue;
double last_time;
std::istringstream iStream(config_file_line);
iStream >> s_data; //不管三七二十一先把第一个读进来
if(!config_axis && s_data == "#"){ //读取配置轴名称行
string x_axis,y_axis,data_name;
iStream >> x_axis >> y_axis >> data_name;
data_label[file_idx].push_back(x_axis); //0
data_label[file_idx].push_back(y_axis); //1
data_label[file_idx].push_back(data_name); //2
config_axis = true;
cout<<" x_axis:"<<x_axis<<endl;
cout<<" y_axis:"<<y_axis<<endl;
cout<<" data_name:"<<data_name<<endl;
continue;
}
if (!config_linetype && s_data == "*") //读取配置曲线颜色和点的类型行
{
string line_type, color, point_type;
iStream >> line_type >> color >> point_type;
if (line_type == "" || color == "" || point_type == "")
continue;
data_label[file_idx].push_back(line_type); //3
data_label[file_idx].push_back(point_type); //4
data_label[file_idx].push_back(color); //5
cout << " line_type:" << line_type << endl;
cout << " point_type:" << point_type << endl;
cout << " color:" << color << endl;
config_linetype = true;
continue;
}
if(first) {
init_time = atof(s_data.c_str());
first = false;
}
//写入数据
_time_stemp += atof(s_data.c_str());
double tmp_data;
iStream >>tmp_data;
_data += tmp_data;
win_cnt++;
if(win_cnt != win_size ){
cout<<"win_size:"<<win_size<<" win_cnt:"<<win_cnt<<endl;
continue;
}
cout<<"win_size:"<<win_size<<" win_cnt:"<<win_cnt<<endl;
x_data[file_idx].push_back(_time_stemp / win_size);// - init_time);//是否要设置第一帧时刻为0?
y_data[file_idx].push_back(_data / win_size);
cout << "mean data:" << to_string(_time_stemp / win_size) << " " << _data / win_size << endl;
x_inorder_data[file_idx].push_back(data_cnt);
data_cnt++;
//TODO reset tmp data:
win_cnt = 0;
_time_stemp = 0;
_data = 0;
// cout << "frame time" << to_string(time_stemp) << endl;
}
if( win_cnt !=0 ){ //有余数
x_data[file_idx].push_back(_time_stemp / win_cnt);// - init_time);//是否要设置第一帧时刻为0?
y_data[file_idx].push_back(_data / win_cnt);
cout << "mean data:" << to_string(_time_stemp / win_cnt) << " " << _data / win_cnt << endl;
x_inorder_data[file_idx].push_back(data_cnt);
}
file_in[file_idx].close();
if(!config_linetype){ //如果文件中没有指明需要绘图的类型和颜色,装载默认的参数
data_label[file_idx].push_back( pre_colors [file_idx]); //3
data_label[file_idx].push_back( pre_lines [file_idx]); //4
data_label[file_idx].push_back( pre_points [file_idx]); //5
}
}
//TODO 求平均值
for (int file_idx = 0; file_idx < file_num; ++file_idx)
{
double sum = std::accumulate(std::begin(y_data[file_idx]), std::end(y_data[file_idx]), 0.0);
double mean = sum / y_data[file_idx].size(); //均值
cout << "mean of "<< data_label[file_idx][2] <<" "<< data_label[file_idx][1] << " is " << mean << endl;
}
if(plot_type == 2){
for (int file_idx = 0; file_idx < file_num; ++file_idx){
plt::subplot(1, 1, 1);
plt::named_plot(data_label[file_idx][2], x_data[file_idx], y_data[file_idx], data_label[file_idx][3]+data_label[file_idx][4]+data_label[file_idx][5]);
plt::plot(x_data[file_idx], y_data[file_idx],data_label[file_idx][3]+data_label[file_idx][4]);
plt::xlabel(data_label[file_idx][0]);
plt::ylabel(data_label[file_idx][1]);
plt::legend();
plt::show();
}
}else if(plot_type == 1){
plt::subplot(1, 1, 1);
for (int file_idx = 0; file_idx < file_num; ++file_idx)
{
plt::named_plot(data_label[file_idx][2],
x_data[file_idx],
y_data[file_idx],
data_label[file_idx][3] + data_label[file_idx][4] + data_label[file_idx][5]);
plt::plot(x_data[file_idx], y_data[file_idx], data_label[file_idx][3] + data_label[file_idx][4]);
plt::xlabel(data_label[file_idx][0]);
plt::ylabel(data_label[file_idx][1]);
plt::legend();
}
plt::show();
}
}