this is messey - dont hate me later

This commit is contained in:
Lucas Oskorep
2019-11-01 16:46:21 -05:00
parent c9ed476230
commit 6b6455ee2c
9 changed files with 1112 additions and 923 deletions
+29
View File
@@ -0,0 +1,29 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
graph_data = open('example.txt','r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(float(x))
ys.append(float(y))
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval=100)
plt.show()
+916 -915
View File
File diff suppressed because it is too large Load Diff
+68 -6
View File
@@ -1,11 +1,73 @@
import numpy as np
import struct
import pandas as pd
import beacontools
from beacontools import parse_packet
from converters import BinToFloat, BinToInt
from numpy import dtype
from bitstring import BitArray
test = b"5\x01\'\x02\xfd\x02/\x02e\x01%\xfd8\xc8Xn<\xc3"
# test = b'\x0f\x00'
# print(len(test))
# first = test[:4]
#
# last = test[14:]
#
print(BitArray(test).bin[:1])
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
test = [16, -12, 1, 0, 3, 2, 999,0, -1277, -1, 1, 0]
# accelerom_string = b"".join([struct.pack("h", x) for x in test])
# print(b"".join([struct.pack("h", x) for x in test]))
def test_bin_float_converstion():
val = 100
test = struct.pack(">f", val)
binary = float_to_bin(val)
print(binary)
print(BinToFloat().process(test))
print(struct.unpack(">f", test))
print(BinToFloat().process(test, True))
print(struct.unpack("f", test))
def test_bin_int_converstion():
val = 100
test = struct.pack(">i", val)
binary = float_to_bin(val)
print(binary)
print(BinToInt().process(test))
print(struct.unpack(">i", test))
print(BinToInt().process(test, True))
print(struct.unpack("i", test))
val = -100
test = struct.pack(">i", val)
binary = float_to_bin(val)
print(binary)
print(BinToInt().process(test))
print(struct.unpack(">i", test))
print(BinToInt().process(test, True))
print(struct.unpack("i", test))
def float_to_bin(num):
return format(struct.unpack('!I', struct.pack('!f', num))[0], '032b')
def convert_row_to_bytes(row):
row = b"".join([struct.pack("h", x) for x in row])
b2f = BinToFloat(15, 32)
print([b2f.process(x) for x in chunker(row, 6)])
# structure = "q"
# i = 8
# print(len(accelerom_string))
# print(struct.unpack(structure, accelerom_string[:i]), struct.unpack(structure, accelerom_string[i:2*i]),
# struct.unpack(structure, accelerom_string[2*i:]))
data = pd.read_csv("accelerometer.data")
print(data)
test_bin_float_converstion()
test_bin_int_converstion()
# for index, row in data.iterrows():
# convert_row_to_bytes(row)
+54
View File
@@ -0,0 +1,54 @@
from bitstring import BitArray
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
class BinToFloat(object):
def __init__(self, exponent=8, mantissa=23):
# self.signed = signed
self.signed = True
self.exponent = exponent
self.mantissa = mantissa
self.tot_len = exponent + mantissa + (1 if self.signed else 0)
def process(self, bin_string, reverse_marshalling=False):
var = BitArray(bin_string)
chunked_bytes = [x for x in chunker(var, 8)]
ordered_bytes = []
for group in reversed(chunked_bytes) if reverse_marshalling else chunked_bytes:
ordered_bytes.extend(group)
# ordered_bytes = [x for x in reversed(ordered_bytes)]
# print(
# "".join(["1" if x else "0" for x in ordered_bytes[0:1]]),
# "".join(["1" if x else "0" for x in ordered_bytes[1:1+self.exponent]]),
# "".join(["1" if x else "0" for x in ordered_bytes[1+self.exponent:1+self.exponent+self.mantissa]])
# )
mant = [x for x in reversed(ordered_bytes[:self.mantissa])]
exp = ordered_bytes[self.mantissa:self.mantissa + self.exponent]
sign = ordered_bytes[self.mantissa + self.exponent] if self.signed else True
exp = self.convert_exp(exp)
mant = self.convert_mantissa(mant)
val = 2.0 ** exp * mant
return -val if sign else val
def convert_exp(self, exp):
total_val = 0
digit_val = 1
for i in exp:
total_val += digit_val if i else 0
digit_val *= 2
tot = (2 ** (self.exponent-1))-1
# print(tot)
# print(total_val)
return total_val - (tot)
def convert_mantissa(self, mant):
total_val = 1
digit_val = .5
for i in mant:
total_val += digit_val if i else 0
digit_val /= 2
return total_val
+41
View File
@@ -0,0 +1,41 @@
from bitstring import BitArray
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
class BinToInt(object):
def __init__(self, size = 32, signed=True):
self.signed = signed
def process(self, bin_string, reverse_marshalling=False):
var = BitArray(bin_string)
print(bin_string)
chunked_bytes = [x for x in chunker(var, 8)]
ordered_bytes = []
for group in reversed(chunked_bytes) if reverse_marshalling else chunked_bytes:
ordered_bytes.extend(group)
ordered_bytes = [not x for x in reversed(ordered_bytes)]
# print(
# "".join(["1" if x else "0" for x in ordered_bytes[0:1]]),
# "".join(["1" if x else "0" for x in ordered_bytes[1:1+self.exponent]]),
# "".join(["1" if x else "0" for x in ordered_bytes[1+self.exponent:1+self.exponent+self.mantissa]])
# )
# sign =
# return -val if sign else val
print([1 if x else 0 for x in ordered_bytes[:-1]])
if self.signed:
return self.convert_pos_int(ordered_bytes[:-1]) * (1 if False else -1)
else:
return self.convert_pos_int(ordered_bytes)
def convert_pos_int(self, val):
total_val = 0
digit_val = 1
for i in val:
total_val += digit_val if i else 0
digit_val *= 2
return total_val
+2
View File
@@ -0,0 +1,2 @@
from .BinToFloat import BinToFloat
from .BinToInt import BinToInt
Binary file not shown.
Binary file not shown.
Binary file not shown.