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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/**
* latenc.c
* --------------------------------
* (c) Manuel Traut <mail@manut.de>
* 06/11/08 - version 0.98
* --------------------------------
* This program is free software;
* you can redistribute it and/or
* modify it under the terms of
* the GNU General Public License
* Version 2 as published by the
* Free Software Foundation.
**/
#include <fstream>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#include <math.h>
#include <list>
/**
* structure for latency-list
* --------------------------------
* 1st latency
* 2nd counter
*/
struct latency_counter{
float latency;
unsigned int counter;
latency_counter(float _latency){
counter = 1;
latency = _latency;
}
} typedef lat_count;
/**
* seperates values of one line
* convert time
* --------------------------------
* 1st time
* 2nd reference signal
* 3rd delayed signal
* 4th another delayed signal
**/
void sep_values(std::string cpp_string, float* time, int* val1, int* val2, int* val3){
int posStart = 0;
int posEnd = cpp_string.find(",", posStart);
std::string time_str = cpp_string.substr(posStart, posEnd);
int posOfExp = time_str.find("e", 0);
std::string expo = time_str;
std::string base = time_str.substr(0, posOfExp);
expo = expo.substr(posOfExp+1, expo.length());
float fbase = atof(base.c_str());
float fexpo = atof(expo.c_str());
*time = fbase*pow(10.0,fexpo);
posStart = posEnd+1;
posEnd = cpp_string.find(",", posStart)-1;
*val1 = atoi(cpp_string.substr(posStart, posEnd).c_str());
posStart = posEnd+2;
posEnd = cpp_string.find(",", posStart)-1;
if(posEnd > cpp_string.length()){
posEnd = cpp_string.length()-1;
*val2 = atoi(cpp_string.substr(posStart, posEnd).c_str());
*val3 = 0;
} else {
*val2 = atoi(cpp_string.substr(posStart, posEnd).c_str());
posStart = posEnd+2;
posEnd = cpp_string.find(",", posStart)-1;
if(posEnd < cpp_string.length()) posEnd = cpp_string.length()-1;
*val3 = atoi(cpp_string.substr(posStart, posEnd).c_str());
}
}
/**
* adds a latencie to a latency-
* counter-list
* --------------------------------
* 1st latency
* 2nd latencycounterlist
* ret counter state
**/
int add_latency(float latency, std::list<lat_count>* lat_list){
latency = latency*1000;
std::cout.precision(20);
// latency already in list?
for(std::list<lat_count>::iterator it = lat_list->begin(); it != lat_list->end(); it++){
if(it->latency == latency){
it->counter++;
return it->counter;
}
}
// add latency
lat_count new_lat_count(latency);
lat_list->push_back(new_lat_count);
return new_lat_count.counter;
}
/**
* calculates jitter of a latency-
* counter-list
* --------------------------------
* 1st latencycounterlist
* ret jitter
**/
float calc_jitter(std::list<lat_count>* lat_list){
double avg = 0;
int sum = 0;
for(std::list<lat_count>::iterator it = lat_list->begin(); it != lat_list->end(); it++){
avg += it->counter * it->latency;
sum += it->counter;
}
avg = (double)(avg/sum);
std::cout<<"Mittelwert:\t"<<avg<<" sec";
double jitter = 0;
for(std::list<lat_count>::iterator it = lat_list->begin(); it != lat_list->end(); it++){
jitter += ((it->latency-avg)*it->counter);
}
jitter = (double)(jitter/sum);
return (float)jitter;
}
/**
* calculates modalvalues and ranges
* of a latency-counter-list
* --------------------------------
* 1st latencycounterlist
* 2nd debug
**/
void calc_modal(std::list<lat_count>* lat_list, bool debug=false){
lat_count modal(0);
lat_count min(0);
min = *lat_list->begin();
lat_count max(0);
max = *lat_list->begin();
for(std::list<lat_count>::iterator it = lat_list->begin(); it != lat_list->end(); it++){
if(it->latency < min.latency){ // for difference min -> modal (best case)
min = *it;
}
if(it->latency > max.latency){ // for difference max -> modal (worst case)
max = *it;
}
if(it->counter>modal.counter){ // most occurences
modal = *it;
} else if (it->counter == modal.counter) { // if same occurence, lower value
if (it->latency < modal.latency){
modal = *it;
}
}
}
std::cout<<"\nModalwert:\t"<<modal.latency<<" sec - Haeufigkeit: "<< modal.counter;
std::cout<<"\nSpannweite:\t"<<min.latency-modal.latency<<" sec (best case)\n\t\t "<<max.latency-modal.latency<<" sec (worst case)\n";
}
/**
* prints out latency-counter-list
* --------------------------------
* 1st latencycounterlist
* 2nd debug
**/
void print_lat_count(std::list<lat_count>* lat_list, int* ch_counter, bool display=false){
int i = 0;
std::stringstream ss;
ss<<*ch_counter;
std::string file = "result"+ss.str()+".txt";
std::ofstream w(file.c_str());
if(!w.is_open()){
std::cerr<<"cannot write output file\n";
} else {
w.precision(20);
for(std::list<lat_count>::iterator it = lat_list->begin(); it != lat_list->end(); it++){
if(display) std::cout<<it->latency<<" "<<it->counter<<std::endl;
w<<it->latency<<" "<<it->counter<<std::endl;
i++;
}
if(i>0) *ch_counter = *ch_counter+1;
}
}
/**
* main routine
* --------------------------------
* 1st filename of the csv-file
**/
int main(int argc, char** argv){
if(argc < 2){
std::cout<<"\nlatenc by Manuel Traut <mail@manut.de>\n\ndraws a graphical visualization and some statistical informations about latencies out of a Tektronix TDS 3034B generated csv file\n\nPhysical Configuration:\nCH1: reference signal\nCH2: delayed signal \nCH3: (optional) delayed signal\n\nOsciloscope Configuration:\n1) measure\n2) delay \n3) save \n4) all channels \n5) to FDD\n\nSYNOPSIS:\nlatenc <TEKxxxx.CSV> [debug]\n\n";
return EXIT_FAILURE;
}
bool debug = false;
// open file
std::ifstream f(argv[1]);
if(!f.is_open()){
std::cerr<<"file open failed\n";
return EXIT_FAILURE;
}
std::string d = "debug";
if(argc > 2) if(d==(argv[2])){
debug = true;
}
// var decl
int* val1; // actual value reference signal
int* val2; // actual value 1st delayed signal
int* val3; // actual value 2nd delayed signal
int voltage1, voltage2, voltage3; // state of the signal (+/-)
float* time; // actual time (1st csv)
float refTimeDown, refTimeUp; // timestamp ref signal raised/falled
bool firstFound = true; // to find a proper start
bool latStartUp = false;
bool latStartDown = false;
std::string linenr; // one csv line
int ch_counter = 0;
std::list<lat_count>* latencies1st;
std::list<lat_count>* latencies2nd;
val1 = new int;
val2 = new int;
val3 = new int;
time = new float;
latencies1st = new std::list<lat_count>;
latencies2nd = new std::list<lat_count>;
while(!f.eof()){
getline(f, linenr);
if(!f.eof()){
sep_values(linenr, time, val1, val2, val3);
if(firstFound){ // to find a proper start
firstFound = false;
voltage1 = *val1;
} else {
if ( (voltage1 > 0) && (*val1 <= 0) ){ // if ref state changed
refTimeDown = *time;
if(debug) std::cout<<*time<<": ref Down: "<<voltage1<<"->"<<*val1<<std::endl;
voltage1 = *val1;
latStartDown = true;
} else if ( (voltage1 <= 0) && (*val1 > 0) ){ // if ref state changed
refTimeUp = *time;
if(debug) std::cout<<*time<<": ref Up: "<<voltage1<<"->"<<*val1<<std::endl;
voltage1 = *val1;
latStartUp = true;
}
if ( (voltage2 > 0) && (*val2 <= 0) && latStartDown ){ // if delayed state changed
float latency2 = *time - refTimeDown; // calc latency
voltage2 = *val2; // remember state
if(debug) std::cout<<*time<<": CH1 latency Down: "<<latency2<<std::endl;
if(latency2 != 0){ // may happen at begining of cvs
add_latency(latency2, latencies1st);
}
} else if ( (voltage2 <= 0) && (*val2 > 0) && latStartUp ){ // see above!
float latency2 = *time - refTimeUp;
voltage2 = *val2;
if(debug) std::cout<<*time<<": CH1 latency Up: "<<latency2<<std::endl;
if(latency2 != 0) add_latency(latency2, latencies1st);
}
if ( (voltage3 > 0) && (*val3 <= 0) && latStartDown ){ // see above!
float latency3 = *time - refTimeDown;
voltage3 = *val3;
if(debug) std::cout<<*time<<": CH2 latency Down: "<<latency3<<std::endl;
if(latency3 != 0) add_latency(latency3, latencies2nd);
} else if ( (voltage3 <= 0) && (*val3 > 0) && latStartUp ){ // see above!
float latency3 = *time - refTimeUp;
voltage3 = *val3;
if(debug) std::cout<<*time<<": CH2 latency Up: "<<latency3<<std::endl;
if(latency3 != 0) add_latency(latency3, latencies2nd);
}
}
}
}
delete val1;
delete val2;
delete val3;
delete time;
f.close();
std::cout<<"\n\ndelayed 1st\n---------------\n\n";
print_lat_count(latencies1st, &ch_counter, debug);
std::cout<<"\nJitter:\t\t"<<calc_jitter(latencies1st)<<" sec";
calc_modal(latencies1st, debug);
std::cout<<"\n\ndelayed 2nd\n---------------\n\n";
print_lat_count(latencies2nd, &ch_counter, debug);
if(ch_counter == 1){
system("sh lat.sh");
} else {
std::cout<<"\nJitter:\t\t"<<calc_jitter(latencies2nd)<<" sec";
calc_modal(latencies2nd, debug);
system("sh lats.sh");
}
return EXIT_SUCCESS;
}
|