import struct
import matplotlib.pyplot as plt
struct_fmt = '<H' # int[5], float, byte[255]
struct_len = struct.calcsize(struct_fmt)
struct_unpack = struct.Struct(struct_fmt).unpack_from
samp = []
with open("C:\\Users\\William\\Downloads\\test.u16", "rb") as f:
while True:
data = f.read(struct_len)
if not data: break
s = struct_unpack(data)
samp.append(s[0])
def dactolin(dacval):
mantissa = ((dacval & 0x1FF8) >> 3)
exponent = (~(dacval >> 13) & 7)
return (mantissa << 6) >> exponent
def better_dactolin(dacval):
mantissa = (dacval >> 3) & 0b111111111
sign = (dacval >> 12) & 0b1
exponent = (dacval >> 13) & 0b111
high_bits = 0b0000000_000000000 if sign else 0b0111111_000000000
high_mant = high_bits | mantissa
sample = (sign << 15) | ((high_mant << (exponent - 1)) & 0x7fff)
return sample
lin_samp = []
for s in samp:
lin_samp.append(better_dactolin(s))
_ = plt.plot(lin_samp[0:10000], '-.')