import time import unittest import os import tempfile import numpy as np from urh.util import util util.set_shared_library_path() from urh.dev.native.lib import hackrf from urh.dev.native.HackRF import HackRF class TestHackRF(unittest.TestCase): def callback_fun(self, buffer): print(buffer) for i in range(0, len(buffer), 4): try: r = np.fromstring(buffer[i:i + 2], dtype=np.float16) / 32767.5 i = np.fromstring(buffer[i + 2:i + 4], dtype=np.float16) / 32767.5 except ValueError: continue if r and i: print(r, i) # out.append(complex(float(buffer[i:i+1])/32767.5, float(buffer[i+2:i+3])/32767.5)) return 0 def test_fromstring(self): buffer = b'\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xff\xfd\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe\xfd\xfe\xff\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe' r = np.empty(len(buffer) // 2, dtype=np.float32) i = np.empty(len(buffer) // 2, dtype=np.float32) c = np.empty(len(buffer) // 2, dtype=np.complex64) # dtype = unpacked = np.frombuffer(buffer, dtype=[('r', np.uint8), ('i', np.uint8)]) ru = unpacked['r'] / 128.0 iu = unpacked['i'] / 128.0 # for j in range(0, len(buffer)-1, 2): # r[j//2] = np.frombuffer(buffer[j:j + 1], dtype=np.int8) / 128.0 # i[j//2] = np.frombuffer(buffer[j + 1:j + 2], dtype=np.int8) / 128.0 # r2 = np.fromstring(buffer[], dtype=np.float16) / 32767.5 c.real = ru c.imag = iu print(c) # x,y = np.frombuffer(buffer, dtype=[('x', np.float16), ('y', np.float16)]) def test_fromstring2(self): buffer = b'\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xff\xfd\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe\xfd\xfe\xff\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfd\xfe' c = np.empty(len(buffer) // 2, dtype=np.complex64) # dtype = unpacked = np.frombuffer(buffer, dtype="