-
Notifications
You must be signed in to change notification settings - Fork 3
/
wavelet.cpp
431 lines (333 loc) · 10.7 KB
/
wavelet.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <caputils/caputils.h>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <csignal>
#include <iostream>
#include <iomanip>
#include <getopt.h>
#include "extract.hpp"
using namespace std;
static int show_zero = 0;
static const char* iface = NULL;
const char* program_name = NULL;
static void handle_sigint(int signum){
if ( !keep_running ){
fprintf(stderr, "\rGot SIGINT again, terminating.\n");
abort();
}
fprintf(stderr, "\rAborting capture.\n");
keep_running = false;
}
void WaveletSpectrumFunc(vector <int> timeSeriesWavelet, double ts)
{
// FILE *f = popen("/Applications/Gnuplot.app/Contents/Resources/bin/gnuplot", "w");
// fprintf(f, "plot '-' w lp ");
// cout << "Band" << "\t" << "Spectrum value" << endl;
// int instantNumber;
// int packetRate;
int n;
int l;
double dataPoints;
double meu,meuC;
// char pathToFile[200];
// int *timeSeries=NULL;
// int indexToTimeSeries=0;
int count=0;
// vector <int> timeSeries;
// cout << "Enter filename:" << endl ;
// cin >> pathToFile;
// cout << endl;
n = (int) timeSeriesWavelet.size();
l = log2(n);
dataPoints = pow(2.0,l); // Limiting time series to multiple of two data points
// cout << dataPoints <<endl;
// *** Declaring Difference and Scaling vectors ***
// waveletSpectrum(l, dataPoints, timeSeries);
// cout << "The end";
// void waveletSpectrum(int l, int dataPoints, vector<int> timeSeries)
// {
// TraceFile.append("_wavelet.txt");
// ofstream outPutFile(TraceFile.c_str(), ios::out);
int i;
int j;
double sum; //Made it double
double diff; //Made it double
double sumDsqr=0;
double sumCsqr=0;
int band=0;
double Dsqr=0;
double Csqr=0;
vector<vector <double> > D;
vector <double> Drow;
vector<vector <double> > C;
vector <double> Crow;
double Dspectrum [l];
double Cspectrum [l];
fprintf(stdout, "Ts\tBand\tD coeff\tC Coeff\n");
for (i=l; i>0; i--){
// int k=0;
// *** Calculation of Difference and Scaling coefficients **
for (j=0; j<dataPoints; j++) {
Dsqr=0;
Csqr=0;
diff=0;
sum=0;
if(i==l){
sum = timeSeriesWavelet[j] + timeSeriesWavelet[j+1]; // At finest level
sum= sum/sqrt(2);
Crow.push_back(sum);
Csqr= sum*sum;
diff = timeSeriesWavelet[j] - timeSeriesWavelet[j+1];
diff = diff/sqrt(2);
Drow.push_back(diff);
Dsqr=diff*diff;
count++;
// outPutFile << "D: " << diff << "\t";
// outPutFile << "C: " << sum << "\t" ;
} else {
sum = (C[band-1][j]) + (C[band-1][j+1]); // At coarser levels
sum= sum/sqrt(2);
Crow.push_back(sum);
Csqr= sum*sum;
diff = (C[band-1][j]) - (C[band-1][j+1]);
diff= diff/sqrt(2);
Drow.push_back(diff);
Dsqr=diff*diff;
count++;
// outPutFile << "D: " << diff << "\t";
// outPutFile << "C: " << sum << "\t" ;
}
sumDsqr=sumDsqr+Dsqr;
sumCsqr+=Csqr;
j++;
}
meu=sumDsqr/count;
meuC=sumCsqr/count;
Dspectrum[band] = log2(meu);
Cspectrum[band] = log(meuC);
sumDsqr=0;
fprintf(stdout, "%5.5g\t%d\t%f\t%f\n", ts*pow(2,l-i),i-1, Dspectrum[band],Cspectrum[band]);
D.push_back(Drow);
C.push_back(Crow);
dataPoints = C[band].size();
Drow.clear();
Crow.clear();
band++;
count=0;
}
// FILE* Gplt = popen(" -persist","w");
// fprintf(Gplt,"plot '' w lp");
// pclose(Gplt);
// fputs("plot '/Users/junaidjunaid/Desktop/UnusedDESKTOPItems/Ramu/wavelet_emacs/ducks_140_14_sc.txt' w lp", f);
// fprintf(f, "set terminal x11");
// fprintf(f, "e");
//fflush(f);
// pclose(f);
}
class Output {
public:
virtual void write_header(double sampleFrequency, double tSample){};
virtual void write_trailer(){};
virtual void write_sample(double t, unsigned long pkts) = 0;
};
class DefaultOutput: public Output {
public:
virtual void write_header(double sampleFrequency, double tSample){
fprintf(stdout, "sampleFrequency: %.2fHz\n", sampleFrequency);
fprintf(stdout, "tSample: %fs\n", tSample);
fprintf(stdout, "\n");
fprintf(stdout, "Time \t Packets\n");
}
virtual void write_sample(double t, unsigned long pkts){
fprintf(stdout, "%.15f\t%10ld\n", t, pkts);
}
};
class CSVOutput: public Output {
public:
CSVOutput(char delimiter, bool show_header)
: delimiter(delimiter)
, show_header(show_header){
}
virtual void write_header(double sampleFrequency, double tSample){
if ( show_header ){
fprintf(stdout, "\"Time (tSample: %f)\"%c\"Packets\"\n", tSample, delimiter);
}
}
virtual void write_sample(double t, unsigned long pkts){
fprintf(stdout, "%.15f%c%ld\n", t, delimiter, pkts);
}
private:
char delimiter;
bool show_header;
};
class PacketRate: public Extractor {
public:
PacketRate()
: Extractor()
, pkts(0){
set_formatter(FORMAT_DEFAULT);
}
virtual void set_formatter(enum Formatter format){
switch (format){
case FORMAT_DEFAULT: output = new DefaultOutput; break;
case FORMAT_CSV: output = new CSVOutput(';', false); break;
case FORMAT_TSV: output = new CSVOutput('\t', false); break;
case FORMAT_MATLAB: output = new CSVOutput('\t', true); break;
/*
// Does not really make sence, to push to influx... multiple values; different meanings.
case FORMAT_INFLUX: output = new InfluxOutput(influx_url, influx_user, influx_pwd); break;
*/
}
}
using Extractor::set_formatter;
virtual void reset(){
pkts = 0;
Extractor::reset();
}
void wavelet(void){
WaveletSpectrumFunc(timeSeries, to_double(tSample));
}
protected:
virtual void write_header(int index){
output->write_header(sampleFrequency, to_double(tSample));
}
virtual void write_trailer(int index){
output->write_trailer();
}
virtual void write_sample(double t){
if ( show_zero || pkts > 0 ){
//output->write_sample(t, pkts);
}
timeSeries.push_back(pkts);
pkts = 0;
}
virtual void accumulate(qd_real fraction, unsigned long packet_bits, const cap_head* cp, int counter){
if ( counter == 1 ){
pkts += 1;
}
}
private:
Output* output;
unsigned long pkts;
vector <int> timeSeries;
};
static const char* short_options = "p:i:q:m:f:zxtTh";
static struct option long_options[]= {
{"packets", required_argument, 0, 'p'},
{"iface", required_argument, 0, 'i'},
{"level", required_argument, 0, 'q'},
{"sampleFrequency", required_argument, 0, 'm'},
{"format", required_argument, 0, 'f'},
{"show-zero", no_argument, 0, 'z'},
{"no-show-zero", no_argument, 0, 'x'},
{"relative-time", no_argument, 0, 't'},
{"absolute-time", no_argument, 0, 'T'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0} /* sentinel */
};
static void show_usage(void){
printf("%s-" VERSION " (libcap_utils-%s)\n", program_name, caputils_version(NULL));
printf("(C) 2012 David Sveningsson <[email protected]>\n");
printf("Usage: %s [OPTIONS] STREAM\n", program_name);
printf(" -i, --iface For ethernet-based streams, this is the interface to listen\n"
" on. For other streams it is ignored.\n"
" -m, --sampleFrequency Sampling frequency in Hz. Valid prefixes are 'k', 'm' and 'g'.\n"
" -q, --level Level to calculate bitrate {physical (default), link, network, transport and application}\n"
" At level N , payload of particular layer is only considered, use filters to select particular streams.\n"
" To calculate the bitrate at physical , use physical layer, Consider for Network layer use [-q network]\n"
" It shall contain transport protocol header + payload\n"
" - link: all bits captured at physical level, i.e link + network + transport + application\n"
" - network: payload field at link layer , network + transport + application\n"
" - transport: payload at network layer, transport + application\n"
" - application: The payload field at transport leve , ie.application\n"
" Default is link\n"
" -p, --packets=N Stop after N packets.\n"
" -z, --show-zero Show bitrate when zero.\n"
" -x, --no-show-zero Don't show bitrate when zero [default]\n"
" -f, --format=FORMAT Set a specific output format. See below for list of supported formats.\n"
" -t, --relative-time Show timestamps relative to the first packet.\n"
" -T, --absolute-time Show timestamps with absolute values (default).\n"
" -h, --help This text.\n\n");
output_format_list();
filter_from_argv_usage();
}
int main(int argc, char **argv){
/* extract program name from path. e.g. /path/to/MArCd -> MArCd */
const char* separator = strrchr(argv[0], '/');
if ( separator ){
program_name = separator + 1;
} else {
program_name = argv[0];
}
struct filter filter;
if ( filter_from_argv(&argc, argv, &filter) != 0 ){
return 0; /* error already shown */
}
PacketRate app;
int op, option_index = -1;
while ( (op = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1 ){
switch (op){
case 0: /* long opt */
case '?': /* unknown opt */
break;
case 'f': /* --format */
app.set_formatter(optarg);
break;
case 'p':
app.set_max_packets(atoi(optarg));
break;
case 'm' : /* --sampleFrequency */
app.set_sampling_frequency(optarg);
break;
case 'q': /* --level */
app.set_extraction_level(optarg);
break;
case 'l': /* --link */
app.set_link_capacity(optarg);
break;
case 'i':
iface = optarg;
break;
case 'z':
show_zero = 1;
break;
case 'x':
show_zero = 0;
break;
case 't': /* --relative-time */
app.set_relative_time(true);
break;
case 'T': /* --absolute-time */
app.set_relative_time(false);
break;
case 'h':
show_usage();
return 0;
default:
fprintf (stderr, "%s: ?? getopt returned character code 0%o ??\n", program_name, op);
}
}
/* handle C-c */
signal(SIGINT, handle_sigint);
int ret;
/* Open stream(s) */
stream_t stream;
if ( (ret=stream_from_getopt(&stream, argv, optind, argc, iface, "-", program_name, 0)) != 0 ) {
return ret; /* Error already shown */
}
stream_print_info(stream, stderr);
app.reset();
app.process_stream(stream, &filter);
app.wavelet();
/* Release resources */
stream_close(stream);
filter_close(&filter);
return 0;
}