aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJulian Picht <julian.picht@gmail.com>2016-12-20 02:22:17 +0100
committerJulian Picht <julian.picht@gmail.com>2016-12-20 02:22:56 +0100
commita33553ccfae5d2e7a355e666ed97aa34565d2091 (patch)
treef58d8ddf2bfb002e4272b39c34316abe468da663 /tools
parent6c3c1a5b8064cdeed483230d2d05bdfb4796c5c5 (diff)
downloadlibsurvive-a33553ccfae5d2e7a355e666ed97aa34565d2091.tar.gz
libsurvive-a33553ccfae5d2e7a355e666ed97aa34565d2091.tar.bz2
first shot at disambiguator
now it's past 2am and I need to sleeeeeeeep
Diffstat (limited to 'tools')
-rw-r--r--tools/disambiguate/.gitignore2
-rw-r--r--tools/disambiguate/Makefile5
-rw-r--r--tools/disambiguate/disambiguate.c90
3 files changed, 97 insertions, 0 deletions
diff --git a/tools/disambiguate/.gitignore b/tools/disambiguate/.gitignore
new file mode 100644
index 0000000..22f3a02
--- /dev/null
+++ b/tools/disambiguate/.gitignore
@@ -0,0 +1,2 @@
+disambiguate
+raw_light_data_from_watchman.csv
diff --git a/tools/disambiguate/Makefile b/tools/disambiguate/Makefile
new file mode 100644
index 0000000..dd5a42b
--- /dev/null
+++ b/tools/disambiguate/Makefile
@@ -0,0 +1,5 @@
+disambiguate: disambiguate.c
+ gcc -g -o $@ $^ -lm
+
+test: disambiguate
+ ./disambiguate | head -n 100
diff --git a/tools/disambiguate/disambiguate.c b/tools/disambiguate/disambiguate.c
new file mode 100644
index 0000000..30034f9
--- /dev/null
+++ b/tools/disambiguate/disambiguate.c
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+long times[64];
+int scores[64];
+
+void discard(long age) {
+ for (int i = 0; i < sizeof(times)/sizeof(times[0]); ++i) {
+ if (times[i] != 0 && times[i] < age) {
+ times[i] = 0;
+ scores[i] = 0;
+ }
+ }
+}
+
+int findNearest(long time) {
+ int diff = 1000; // max allowed diff for a match
+ int idx = -1;
+ for (int i = 0; i < sizeof(times)/sizeof(times[0]); ++i) {
+ if (times[i] == 0) continue;
+
+ int a = abs(times[i] - time);
+ if (a < diff) {
+ idx = i;
+ diff = a;
+ }
+
+ }
+
+ //printf("DIFF: %d %d\n", diff, idx);
+
+ return idx;
+}
+
+int main() {
+
+ FILE * f = fopen( "raw_light_data_from_watchman.csv", "r" );
+ if (f == NULL) {
+ fprintf(stderr, "ERROR OPENING INPUT FILE\n");
+ return -1;
+ }
+ memset(&times, 0, sizeof( times ));
+ memset(&scores, 0, sizeof( scores ));
+
+ long offset = 0, last = 0;
+
+ for (;;) {
+ char controller[10];
+ int sensor;
+ int unknown;
+ int length;
+ long time;
+ if (fscanf(f, "%s %d %d %d %li", controller, &sensor, &unknown, &length, &time) != 5) {
+ break;
+ }
+ /*if (time - last < 10000) {
+ offset += 0x100000000;
+ }*/
+ time += offset;
+ last = time;
+
+ if (length < 2750) continue;
+
+ discard(time - 10000000);
+ int idx = findNearest(time - 800000);
+ if (idx == -1) {
+ for (int i = 0; i < sizeof(times)/sizeof(times[0]); ++i) {
+ if (times[i] == 0) {
+// printf("ADD: %d %li\n", i, time);
+ times[i] = time;
+ break;
+ }
+ }
+ } else {
+ double l = length;
+ char cc = round(l / 500) - 6;
+ printf("MATCH: %li %d %d (0b%d%d%d)\n", time - times[idx], scores[idx], length, (cc >> 2) & 0x1, (cc >> 1) & 0x1, cc & 0x1);
+ scores[idx]++;
+ if (scores[idx] >= 30) {
+ printf("MATCH: %li %d\n", time - times[idx], scores[idx]);
+ return 0;
+ }
+ times[idx] = time;
+ }
+ }
+ fclose(f);
+}
+