-
Notifications
You must be signed in to change notification settings - Fork 0
/
signaldata.cpp
241 lines (217 loc) · 5.95 KB
/
signaldata.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "signaldata.h"
SignalData::SignalData()
{
vector_x.clear();
for(int i=1;i<9;i++) vector_x.push_back(i*5);
symbol_table.add_constants();
symbol_table.add_vector("X",vector_x);
expression.register_symbol_table(symbol_table);
update_status = false;
show_enable = false;
filter_enable = false;
max_data_len = 60000000;
SetFilterLengthMax(1,1000);
}
SignalData::~SignalData()
{
symbol_table.clear();
expression.release();
}
/*
*
*
*/
bool SignalData::isExpreesionValue(QString express_str)
{
parser.compile(express_str.toStdString(), expression);
vector_x.clear();
for(int i=1;i<9;i++) vector_x.push_back(i);
qDebug()<< express_str;
qDebug()<<"\n-----------------------------------------";
qDebug()<<"expression.value()"<<expression.value();
qDebug()<<"-----------------------------------------\n";
if(isnan(expression.value())) return false;
else return true;
}
// can date
void SignalData::AddData(double time,QByteArray data)
{
if(!time_list.isEmpty() && time< time_list.last()) return;
double val = EvaluateExpress(data);
time_list.append(time);
message_list.append(data);
data_list.append(val);
if(filter_enable) filter_enable = false;
Filter(time,val);
update_status = true;
}
//adc data fliter set true
void SignalData::AddData(double time,int data)
{
if(!time_list.isEmpty() && time< time_list.last()) return;
double voltage = data*(10.0/65536.0);
double val = EvaluateExpress(voltage);
time_list.append(time);
val_list.append(voltage);
data_list.append(val);
if(filter_enable==false) filter_enable = true;
Filter(time,val);
update_status = true;
}
// io data fliter set false
void SignalData::AddIOData(double time,int data)
{
if(!time_list.isEmpty() && time< time_list.last()) return;
//if(!show_data.isEmpty() && time< show_data.last().rx()) return;
double voltage = data;
double val = EvaluateExpress(voltage);
//qDebug()<<"AddIOData"<<data<<show_data.size()<< update_status;
// if(show_data.isEmpty() )show_data.append(QPointF(time,val));
// else if(time>= show_data.last().rx()) show_data.append(QPointF(time,val));
time_list.append(time);
val_list.append(data);
data_list.append(val);
if(filter_enable) filter_enable = false;
Filter(time,val);
update_status = true;
}
double SignalData::EvaluateExpress(QByteArray data)
{
vector_x.clear();
for(int i=0;i<data.size();i++)
{
vector_x.push_back((uchar(data.at(i))));
// qDebug()<<"EvaluateExpress"<<i<<<<double(data.at(i));
}
return expression.value();
}
double SignalData::EvaluateExpress(double data)
{
vector_x.clear();
vector_x.push_back(double(data));
return expression.value();
}
void SignalData::UpdateExpress()
{
if(!val_list.isEmpty())
{
data_list.clear();
for(int i=0;i<val_list.size();i++)
{
data_list.append(EvaluateExpress(val_list.at(i)));
}
}
else if(!message_list.isEmpty())
{
data_list.clear();
for(int i=0;i<message_list.size();i++)
{
data_list.append(EvaluateExpress(message_list.at(i)));
}
}
}
void SignalData::ClearPlotData()
{
show_data.clear();
show_data.resize(0);
update_status = true;
}
void SignalData::ClearAllData()
{
time_list.clear();
data_list.clear();
val_list.clear();
message_list.clear();
//ClearPlotData();
UpdateAllData();
}
/*
* 滑动平均滤波
*/
double SignalData::MoveAverageFilter(double value)
{
if(filter_length>filter_list.size())
{
filter_list.append(value);
filter_sum += value;
return value;
}
filter_list.append(value);
filter_sum += value;
filter_sum -= filter_list.first();
filter_list.removeFirst();
return filter_sum/filter_list.length();
}
void SignalData::SetFilterLengthMax(int length, double max)
{
max_change_data = max;
if(length==0) length = 1;
filter_length = length;
int size = filter_list.size();
if(size>filter_length)
{
for(int i=0;i<size-filter_length;i++) filter_list.removeFirst();
}
filter_sum = 0;
for(int i=0;i<filter_list.length();i++)
{
filter_sum += filter_list.at(i);
}
UpdateAllData();
}
double SignalData::MaxFilter(double old_data, double new_data)
{
if((old_data - new_data)>max_change_data ||((new_data - old_data)>max_change_data)) return old_data;
return new_data;
}
void SignalData::Filter(double time, double new_data)
{
if(filter_enable)
{
double old_data;
if(show_data.isEmpty()) old_data = new_data;
else old_data = show_data.last().y();
double val = MaxFilter(old_data, new_data);
double result = MoveAverageFilter(val);
show_data.append(QPointF(time,result));
}
else show_data.append(QPointF(time,new_data));
RemoveDataLen();
}
void SignalData::UpdateAllData()
{
UpdateExpress();
show_data.clear();
show_data.resize(time_list.size());
filter_sum=0;
filter_list.clear();
for(int i=0;i<time_list.size();i++)
{
Filter(time_list.at(i),data_list.at(i));
}
// if(!show_data.isEmpty()) update_status = true;
update_status = true;
}
void SignalData::SettingAttra(QColor color, QString name, QString express_str)
{
this->color = color;
this->name = name;
this->express_str = express_str;
UpdateAllData();
this->update_status = true;
this->show_enable = true;
}
void SignalData::RemoveDataLen()
{
if(max_data_len == 0) return;
if(time_list.size()>max_data_len)time_list.remove(0,max_data_len/2);
else return;
if(data_list.size()>max_data_len)time_list.remove(0,max_data_len/2);
if(val_list.size()>max_data_len)time_list.remove(0,max_data_len/2);
if(message_list.size()>max_data_len)time_list.remove(0,max_data_len/2);
show_data.remove(0,max_data_len/2);
}
void SignalData::SetMaxDataLen(int len)
{
max_data_len = len;
}