forked from simonwunderlich/FFT_eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft_eval_json.c
404 lines (349 loc) · 11.3 KB
/
fft_eval_json.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
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
/* SPDX-License-Identifier: GPL-2.0-only
* SPDX-FileCopyrightText: 2012 Simon Wunderlich <[email protected]>
* SPDX-FileCopyrightText: 2012 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* SPDX-FileCopyrightText: 2013 Gui Iribarren <[email protected]>
* SPDX-FileCopyrightText: 2017 Nico Pace <[email protected]>
*/
/*
* This program has been created to aid open source spectrum
* analyzer development for Qualcomm/Atheros AR92xx and AR93xx
* based chipsets.
*/
#include <inttypes.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fft_eval.h"
#define MAX_RSSI_SUPPORT 100
int rssi_list[MAX_RSSI_SUPPORT] = {0};
int req_freq = 0;
int get_data(int rssi)
{
int i,handle=0,data,bins;
double sum = 0.0, sum_sq = 0.0, mean, variance, index = 0.0;
struct scanresult *result;
memset(rssi_list,0,sizeof(int)*100);
for (result = result_list; result; result = result->next) {
switch (result->sample.tlv.type) {
case ATH_FFT_SAMPLE_HT20:
if (result->sample.ht20.rssi != rssi)
continue;
for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
data = result->sample.ht20.data[i] << result->sample.ht20.max_exp;
if (data == 0)
data = 1;
}
handle=1;
break;
case ATH_FFT_SAMPLE_HT20_40:
if (result->sample.ht40.lower_rssi != rssi)
continue;
for (i = 0; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
data = result->sample.ht40.data[i] << result->sample.ht40.max_exp;
if (data == 0)
data = 1;
}
handle=1;
break;
case ATH_FFT_SAMPLE_ATH10K:
if (result->sample.ath10k.header.rssi != rssi)
continue;
bins = result->sample.tlv.length - (sizeof(result->sample.ath10k.header) - sizeof(result->sample.ath10k.header.tlv));
for (i = 0; i < bins; i++) {
data = result->sample.ath10k.data[i] << result->sample.ath10k.header.max_exp;
if (data == 0)
data = 1;
sum += data; // Sum of all elements
sum_sq += data * data; // Sum of squares of elements
}
handle=1;
break;
case ATH_FFT_SAMPLE_ATH11K:
if (result->sample.ath11k.header.rssi != rssi)
continue;
bins = result->sample.tlv.length - (sizeof(result->sample.ath11k.header) - sizeof(result->sample.ath11k.header.tlv));
for (i = 0; i < bins; i++) {
data = result->sample.ath11k.data[i] << result->sample.ath11k.header.max_exp;
if (data == 0)
data = 1;
}
handle=1;
break;
}
if (handle)
break;
}
mean = sum / bins;
variance = (sum_sq / bins) - (mean * mean);
index = (0.5 * rssi) + (0.3 * mean) + (0.2 * variance);
if ( index > 100 )
index = 100;
printf("[{\"rssi\":%d},{\"data_mean\":%.2f},{\"data_vari\":%2.f},{\"index\":%.2f}]\n", rssi, mean, variance,index);
return handle;
}
static int index_rssi(void)
{
int i,max_samples=0, rssi=0;
struct scanresult *result;
memset(rssi_list,0,sizeof(int)*100);
for (result = result_list; result; result = result->next) {
switch (result->sample.tlv.type) {
case ATH_FFT_SAMPLE_HT20:
if ((req_freq != 0) && ( req_freq != result->sample.ht20.freq))
continue;
if (result->sample.ht20.rssi < MAX_RSSI_SUPPORT)
rssi_list[result->sample.ht20.rssi]++;
break;
case ATH_FFT_SAMPLE_HT20_40:
if ((req_freq != 0) && ( req_freq != result->sample.ht40.freq))
continue;
if (result->sample.ht40.lower_rssi < MAX_RSSI_SUPPORT)
rssi_list[result->sample.ht40.lower_rssi]++;
break;
case ATH_FFT_SAMPLE_ATH10K:
if ((req_freq != 0) && ( req_freq != result->sample.ath10k.header.freq1))
continue;
if (result->sample.ath10k.header.rssi < MAX_RSSI_SUPPORT)
rssi_list[result->sample.ath10k.header.rssi]++;
break;
case ATH_FFT_SAMPLE_ATH11K:
if ((req_freq != 0) && ( req_freq != result->sample.ath11k.header.freq1))
continue;
if (result->sample.ath11k.header.rssi < MAX_RSSI_SUPPORT)
rssi_list[result->sample.ath11k.header.rssi]++;
break;
}
}
for(i=0;i<MAX_RSSI_SUPPORT;i++)
{
if (rssi_list[i] > max_samples) {
max_samples = rssi_list[i];
rssi = i;
}
}
//printf("Rssi %d has %d samples\n",rssi,max_samples);
return rssi;
}
/*
* print_values - spit out the analyzed values in text form, JSON-like.
*/
static int print_values(void)
{
int i, rnum;
struct scanresult *result;
printf("[");
rnum = 0;
if (!result_list)
printf("No data\n");
for (result = result_list; result; result = result->next) {
switch (result->sample.tlv.type) {
case ATH_FFT_SAMPLE_HT20:
{
int datamax = 0, datamin = 65536;
int datasquaresum = 0;
/* prints some statistical data about the
* data sample and auxiliary data. */
printf("\n{ \"tsf\": %" PRIu64 ", \"central_freq\": %d, \"rssi\": %d, \"noise\": %d, \"data\": [ ", result->sample.ht20.tsf, result->sample.ht20.freq, result->sample.ht20.rssi,
result->sample.ht20.noise);
for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
int data;
data = (result->sample.ht20.data[i] << result->sample.ht20.max_exp);
data *= data;
datasquaresum += data;
if (data > datamax)
datamax = data;
if (data < datamin)
datamin = data;
}
for (i = 0; i < SPECTRAL_HT20_NUM_BINS; i++) {
float freq;
float signal;
int data;
freq = result->sample.ht20.freq - 10.0 + ((20.0 * i) / SPECTRAL_HT20_NUM_BINS);
/* This is where the "magic" happens: interpret the signal
* to output some kind of data which looks useful. */
data = result->sample.ht20.data[i] << result->sample.ht20.max_exp;
if (data == 0)
data = 1;
signal = result->sample.ht20.noise + result->sample.ht20.rssi + 20 * log10(data) - log10(datasquaresum) * 10;
printf("[ %f, %f ]", freq, signal);
if (i < SPECTRAL_HT20_NUM_BINS - 1)
printf(", ");
}
}
break;
case ATH_FFT_SAMPLE_HT20_40:
{
int datamax = 0, datamin = 65536;
int datasquaresum_lower = 0;
int datasquaresum_upper = 0;
int datasquaresum;
int i;
int centerfreq;
s8 noise;
s8 rssi;
//todo build average
printf("\n{ \"tsf\": %" PRIu64 ", \"central_freq\": %d, \"rssi\": %d, \"noise\": %d, \"data\": [ ", result->sample.ht40.tsf, result->sample.ht40.freq, result->sample.ht40.lower_rssi,
result->sample.ht40.lower_noise);
for (i = 0; i < SPECTRAL_HT20_40_NUM_BINS / 2; i++) {
int data;
data = result->sample.ht40.data[i];
data <<= result->sample.ht40.max_exp;
data *= data;
datasquaresum_lower += data;
if (data > datamax)
datamax = data;
if (data < datamin)
datamin = data;
}
for (i = SPECTRAL_HT20_40_NUM_BINS / 2; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
int data;
data = result->sample.ht40.data[i];
data <<= result->sample.ht40.max_exp;
datasquaresum_upper += data;
if (data > datamax)
datamax = data;
if (data < datamin)
datamin = data;
}
switch (result->sample.ht40.channel_type) {
case NL80211_CHAN_HT40PLUS:
centerfreq = result->sample.ht40.freq + 10;
break;
case NL80211_CHAN_HT40MINUS:
centerfreq = result->sample.ht40.freq - 10;
break;
default:
return -1;
}
for (i = 0; i < SPECTRAL_HT20_40_NUM_BINS; i++) {
float freq;
int data;
freq = centerfreq - (40.0 * SPECTRAL_HT20_40_NUM_BINS / 128.0) / 2 + (40.0 * (i + 0.5) / 128.0);
if (i < SPECTRAL_HT20_40_NUM_BINS / 2) {
noise = result->sample.ht40.lower_noise;
datasquaresum = datasquaresum_lower;
rssi = result->sample.ht40.lower_rssi;
} else {
noise = result->sample.ht40.upper_noise;
datasquaresum = datasquaresum_upper;
rssi = result->sample.ht40.upper_rssi;
}
data = result->sample.ht40.data[i];
data <<= result->sample.ht40.max_exp;
if (data == 0)
data = 1;
float signal = noise + rssi + 20 * log10(data) - log10(datasquaresum) * 10;
printf("[ %f, %f ]", freq, signal);
if (i < SPECTRAL_HT20_40_NUM_BINS - 1)
printf(", ");
}
}
break;
case ATH_FFT_SAMPLE_ATH10K:
{
int datamax = 0, datamin = 65536;
int datasquaresum = 0;
int i, bins;
printf("\n{ \"tsf\": %" PRIu64 ", \"central_freq\": %d, \"rssi\": %d, \"noise\": %d, \"data\": [ ", result->sample.ath10k.header.tsf, result->sample.ath10k.header.freq1,
result->sample.ath10k.header.rssi, result->sample.ath10k.header.noise);
bins = result->sample.tlv.length - (sizeof(result->sample.ath10k.header) - sizeof(result->sample.ath10k.header.tlv));
for (i = 0; i < bins; i++) {
int data;
data = (result->sample.ath10k.data[i] << result->sample.ath10k.header.max_exp);
data *= data;
datasquaresum += data;
if (data > datamax)
datamax = data;
if (data < datamin)
datamin = data;
}
for (i = 0; i < bins; i++) {
float freq;
int data;
float signal;
freq = result->sample.ath10k.header.freq1 - (result->sample.ath10k.header.chan_width_mhz) / 2 + (result->sample.ath10k.header.chan_width_mhz * (i + 0.5) / bins);
data = result->sample.ath10k.data[i] << result->sample.ath10k.header.max_exp;
if (data == 0)
data = 1;
signal = result->sample.ath10k.header.noise + result->sample.ath10k.header.rssi + 20 * log10(data) - log10(datasquaresum) * 10;
printf("[ %f, %f ]", freq, signal);
if (i < bins - 1)
printf(", ");
}
}
break;
case ATH_FFT_SAMPLE_ATH11K:
{
int datamax = 0, datamin = 65536;
int datasquaresum = 0;
int i, bins;
printf("\n{ \"tsf\": %08d, \"central_freq\": %d, \"rssi\": %d, \"noise\": %d, \"data\": [ ", result->sample.ath11k.header.tsf, result->sample.ath11k.header.freq1,
result->sample.ath11k.header.rssi, result->sample.ath11k.header.noise);
bins = result->sample.tlv.length - (sizeof(result->sample.ath11k.header) - sizeof(result->sample.ath11k.header.tlv));
for (i = 0; i < bins; i++) {
int data;
data = result->sample.ath11k.data[i];
data *= data;
datasquaresum += data;
if (data > datamax)
datamax = data;
if (data < datamin)
datamin = data;
}
for (i = 0; i < bins; i++) {
float freq;
int data;
float signal;
freq = result->sample.ath11k.header.freq1 - (result->sample.ath11k.header.chan_width_mhz) / 2 + (result->sample.ath11k.header.chan_width_mhz * (i + 0.5) / bins);
data = result->sample.ath11k.data[i];
if (data == 0)
data = 1;
signal = result->sample.ath11k.header.noise + result->sample.ath11k.header.rssi + 20 * log10f(data) - log10f(datasquaresum) * 10;
printf("[ %f, %f ]", freq, signal);
if (i < bins - 1)
printf(", ");
}
}
break;
}
printf(" ] }");
if (result->next)
printf(",");
rnum++;
}
printf("\n]\n");
return 0;
}
static void usage(const char *prog)
{
if (!prog)
prog = "fft_eval";
fprintf(stderr, "Usage: %s scanfile\n", prog);
fft_eval_usage(prog);
}
int main(int argc, char *argv[])
{
char *ss_name = NULL;
char *prog = NULL;
if (argc >= 1)
prog = argv[0];
if (argc >= 2)
ss_name = argv[1];
if (argc >= 3)
req_freq = atoi(argv[2]);
//fprintf(stderr, "WARNING: Experimental Software! Don't trust anything you see. :)\n");
//fprintf(stderr, "\n");
if (fft_eval_init(ss_name) < 0) {
fprintf(stderr, "Couldn't read scanfile ...\n");
usage(prog);
return -1;
}
int rssi = index_rssi();
get_data(rssi);
//print_values();
fft_eval_exit();
return 0;
}