-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.c
198 lines (166 loc) · 5.03 KB
/
data.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
/**
* @file
* Data implementation using flat, tab-separated plain text files
*/
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#include "main.h"
#include "receiver.h"
/** Data log. */
static FILE *log_file;
/** Data format. */
static const char *format = "%f\t%f\t%d\n";
/** Sentinel value for empty data. */
static const int NODATA = -55;
struct dataset_s {
int max_strength[360], avg_strength[360], avg_count[360];
bool elevation;
};
/** Current data set for holding temporary data. */
static struct dataset_s set;
static void data_pattern_dump(int origin_az);
/**
* Translate a compass angle to an angle on a polar plot.
*/
static int compass_translate(int angle) {
return 90 - angle;
}
/**
* Zero the given data set.
*/
static void data_clear(struct dataset_s *set) {
for (int i = 0; i < 360; i++) {
set->max_strength[i] = NODATA;
set->avg_strength[i] = NODATA;
set->avg_count[i] = 0;
set->elevation = false;
}
}
/**
* Clean up data collection module. Used only internally on exit.
*/
static void data_cleanup() {
fclose(log_file);
}
/**
* Initialize data collection module. Use this function only when recording
* live data. This function exits on error.
*/
void data_init() {
log_file = fopen(config.write_file, "wx");
if (log_file == NULL) {
fprintf(stderr, "Could not create data log '%s': %s\n",
config.write_file, strerror(errno));
exit(EXIT_FAILURE);
}
atexit(data_cleanup);
fprintf(stderr, "Logging data to '%s'\n", config.write_file);
}
/**
* Add a single data point to the current data set. The caller should have
* already performed sanity checking on the data.
*/
static void data_add(float position, int strength) {
const int pos_rnd = (int)roundf(position);
// Ignore data points that are out of bounds.
if (pos_rnd < 0 || pos_rnd >= 360) {
return;
}
// Record the maximum signal strength at a given orientation.
if (set.max_strength[pos_rnd] < strength) {
set.max_strength[pos_rnd] = strength;
}
// Update average signal strength.
set.avg_strength[pos_rnd] =
(set.avg_strength[pos_rnd] * set.avg_count[pos_rnd] + strength) /
++set.avg_count[pos_rnd];
}
/**
* Load recorded data from a file.
*/
void data_load(FILE *file) {
char buf[64];
int line = 1, origin_az = 0;
while (fgets(buf, sizeof(buf), file) != NULL) {
if (buf[0] == '#') {
if (sscanf(buf, "# @%s %d,%*d\n", buf, &origin_az) >= 1) {
if (strcmp(buf, "origin") == 0) {
printf("# Origin: %d\n", origin_az);
} else if (strcmp(buf, "azimuth") == 0) {
printf("#### Azimuth ####\n");
data_clear(&set);
set.elevation = false;
} else if (strcmp(buf, "elevation") == 0) {
data_pattern_dump(origin_az);
printf("#### Elevation ####\n");
data_clear(&set);
set.elevation = true;
} else {
fprintf(stderr, "%d: warning: unknown command\n", line);
}
}
} else {
float azimuth, elevation;
int strength;
if (sscanf(buf, format, &azimuth, &elevation, &strength) != 3) {
fprintf(stderr, "%d: error: invalid data format\n", line);
exit(EXIT_FAILURE);
}
if (!set.elevation) {
data_add(azimuth, strength);
} else {
data_add(elevation, strength);
}
}
line++;
}
// Dump the data from the elevation pattern.
data_pattern_dump(origin_az);
}
/**
* Make an annotation in the current data log. This function should only be
* used when recording live data.
*/
void data_annotate(const char *format, ...) {
va_list args;
fprintf(log_file, "# ");
va_start(args, format);
vfprintf(log_file, format, args);
va_end(args);
fprintf(log_file, "\n");
fflush(log_file);
}
/**
* Log and process a data point from a live recording session.
*/
void data_record(float azimuth, float elevation, int strength) {
// Write a copy of all data to the log.
fprintf(log_file, format, azimuth, elevation, strength);
fflush(log_file);
}
/**
* Print the current data set in a Gnuplot-friendly format. Do not forget to
* clear the data set before starting another one.
*/
static void data_pattern_dump(int origin_az) {
for (int i = 0; i < 360; i++) {
int current_str = set.max_strength[i];
if (current_str != NODATA) {
int display_angle;
if (!set.elevation) {
display_angle = compass_translate(i - origin_az);
} else {
display_angle = i;
}
printf("%d\t%d\n", display_angle, current_str);
}
}
fflush(stdout);
}