-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.c
244 lines (184 loc) · 4.89 KB
/
common.c
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
#include "common.h"
#include "debug.h"
#include <stdarg.h>
#include <stdio.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
//
// CONTROL MESSAGES (TCP)
//
int send_control_message(int fd, uint32_t code, uint32_t value)
{
uint32_t ctl_message = ((code & 0xff) << 24) | (value & 0xffffff);
if ( fd < 0 )
return 0;
ulog(LOG_DEBUG, "[S>] M=%u C=%u V=%u\n", ctl_message, code, value);
ctl_message = htonl(ctl_message);
if (write(fd, (char *)&ctl_message, sizeof(uint32_t)) != sizeof(uint32_t))
return 1;
return 0;
}
int receive_control_message(int fd, uint32_t *code, uint32_t *value)
{
uint32_t ctl_message = 0;
uint32_t ctl_code = 0;
uint32_t ctl_value = 0;
int bytes_read = 0;
// assume a fail
int ret = 1;
if ( fd < 0 )
return 0;
bytes_read = read(fd, (char *)&ctl_message, sizeof(uint32_t));
if ( bytes_read == sizeof(uint32_t))
{
ctl_message = ntohl(ctl_message);
ctl_code = (ctl_message & 0xff000000) >> 24;
ctl_value = (ctl_message & 0x00ffffff);
ret = 0;
}
else if ( bytes_read < 0 )
{
ret = 2;
ulog(LOG_DEBUG, "errno: %d\n", errno);
}
else
{
ulog(LOG_DEBUG, "Bytes Read: %d %d\n", bytes_read, sizeof(uint32_t));
}
if ( bytes_read == 0 )
ret = 2;
if ( NULL != code )
*code = (ctl_code);
if ( NULL != value )
*value = (ctl_value);
ulog(LOG_DEBUG, "[R<] M=%u C=%u V=%u\n", ctl_message, ctl_code, ctl_value);
return ret;
}
//
// TIME TOOLS
//
double time_delta_us(struct timeval t1, struct timeval t2)
{
return (double)( (t2.tv_sec-t1.tv_sec)*1e6 + (t2.tv_usec-t1.tv_usec));
}
//
// ARRAY MANIPULATION
//
void array_sort(double array[], double array_ordered[], unsigned int elements)
{
double array_temp[elements];
int i, j;
for (i=0; i<elements; i++)
array_temp[i] = array[i];
for (i=1; i<elements; i++)
for (j=i-1; j>=0; j--)
{
if ( array_temp[j+1] < array_temp[j] )
{
array_ordered[j] = array_temp[j];
array_temp[j] = array_temp[j+1];
array_temp[j+1] = array_ordered[j];
}
}
for (i=0; i<elements; i++)
array_ordered[i] = array_temp[i];
}
void array_print(double array[], unsigned int elements)
{
int i;
printf("Array:\n");
for (i=0; i<elements; i++)
printf(" %u => %.4f\n", i, array[i]);
}
//
// ARRAY STATISTICS
//
double stat_array_interquartile_mean(double array[], unsigned int elements)
{
int i;
double array_sorted[elements];
double array_total = 0.0;
array_sort(array, array_sorted, elements);
double quartile_observations = elements / 4;
double quartile_range = elements / 2;
int quartile_observations_truncated = (int)floor(quartile_observations);
int quartile_observations_whole = quartile_observations_truncated + 1;
// we want at least one whole observation
if ( quartile_observations_whole < 1 )
return -1;
// add whole observations
for ( i = quartile_observations_whole; i < elements - quartile_observations_whole; i++)
array_total += array[i];
// add truncated observations with weighting
array_total += (array[quartile_observations_truncated] + array[elements - quartile_observations_truncated]) * ((double)quartile_observations_whole - quartile_observations);
return array_total / quartile_range;
}
double stat_array_mean(double array[], unsigned int elements)
{
int i;
double array_total = 0.0;
for (i=0; i<elements; i++)
array_total += array[i];
return array_total / (double)elements;
}
double stat_array_std(double array[], unsigned int elements)
{
int i;
double array_total = 0.0;
double array_mean = stat_array_mean(array, elements);
for (i=0; i<elements; i++)
array_total += pow(array[i] - array_mean, 2);
return sqrt(array_total / (double)elements);
}
double stat_array_median(double array[], unsigned int elements)
{
int i,j;
double array_sorted[elements];
double swap;
for (i=0; i<elements; i++)
array_sorted[i] = array[i];
for (i=0; i<elements; i++)
{
for (j=elements-1; j>i; j--)
{
if ( array_sorted[i] > array_sorted[i] )
{
swap = array_sorted[j];
array_sorted[j] = array_sorted[j+1];
array_sorted[j+1] = swap;
}
}
}
return array_sorted[(elements-(elements%2))/2];
}
double stat_array_kurtosis(double array[], unsigned int elements)
{
int i;
double array_total = 0.0;
double array_variance = 0.0;
double array_mean = stat_array_mean(array, elements);
if ( elements < 3 )
return -99999;
for (i=0; i<elements; i++)
array_total += pow(array[i] - array_mean, 2);
array_variance = array_total / elements;
if ( array_variance == 0 )
return -99999;
array_total = 0.0;
for (i=0; i<elements; i++)
array_total += pow(array[i] - array_mean, 4);
return array_total / pow(array_variance, 2);
}
//
// MISC
//
int int_min(int a, int b)
{
return (a < b) ? a : b;
}
int int_max(int a, int b)
{
return (a > b) ? a : b;
}