Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparka committed Jun 22, 2018
1 parent 7e39471 commit 87b32e6
Show file tree
Hide file tree
Showing 6 changed files with 1,124 additions and 11 deletions.
1 change: 0 additions & 1 deletion .idea/pyha.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions pyha/common/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from contextlib import suppress

import collections
import numpy as np


def to_twoscomplement(bits, value):
# https: // stackoverflow.com / questions / 21871829 / twos - complement - of - numbers - in -python
if value < 0:
Expand Down Expand Up @@ -116,3 +119,55 @@ def show_plot():
if plt.gca().get_legend_handles_labels() != ([], []):
plt.legend()
plt.show()


class SignalTapParser:
def __init__(self, file: str):
import csv

self.labels = []
self.data = []
with open(file) as csvfile:
reader = csv.reader(csvfile)
for x in reader:
if len(x) and x[0] == 'Data:':
self.labels = next(reader)
self.data = [x for x in reader]

self.trans_data = np.array(self.data).T

def __getitem__(self, item: str):
i = self.labels.index(item)
return self.trans_data[i]

def to_int(self, data, bits):
r = []
for x in data:
with suppress(ValueError):
new = int(x, 16)
if new >= 2 ** (bits - 1): # conv to signed
new -= 2 ** (bits)
r.append(new)
return r

# NB! these wont work if you export sfixed signal( has negative bounds in name )
# need to invert msb or something
def to_float(self, data, bits):
""" assume 1 sign others fractional"""
ints = self.to_int(data, bits)
return np.array(ints) / 2 ** (bits - 1)

def to_bladerf(self, data):
""" assume 5 bit is for integer(1 bit is sign) and others for fractional part
This is used in bladeRF """
ints = self.to_int(data, 16)
return np.array(ints) / 2 ** (11)


def to_signed_int(number, bit_length):
# http://stackoverflow.com/questions/1375897/how-to-get-the-signed-integer-value-of-a-long-in-python
mask = (2 ** bit_length) - 1
if number & (1 << (bit_length - 1)):
return number | ~mask
else:
return number & mask
16 changes: 10 additions & 6 deletions pyha/simulation/simulation_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,29 @@ def convert_arg(default_type, arg, i):
return ret

args = list(args)

def is_list(x):
return isinstance(x, (list, np.ndarray))

with SimPath('inputs'):
for i, arg in enumerate(args):
arg = get_iterable(arg)
if any(isinstance(x, float) for x in arg):

if to_types is not None and not is_list(arg[0]):
args[i] = convert_arg(None, arg, i)
elif any(isinstance(x, float) for x in arg):
args[i] = convert_arg(default_sfix, arg, i)

elif any(isinstance(x, (complex, np.complexfloating)) for x in arg):
args[i] = convert_arg(default_complex, arg, i)

elif to_types is not None:
args[i] = convert_arg(None, arg, i)

elif isinstance(arg[0], Hardware):
for x in arg:
x._pyha_floats_to_fixed()

elif isinstance(arg[0], (list, np.ndarray)):
elif is_list(arg[0]):
# input is 2D array -> turn into packets (1D list of Stream objects)
args[i] = convert_input_types(arg, silence=True) # dont apply input callback here..
args[i] = convert_input_types(arg, silence=True, to_types=to_types) # dont apply input callback here..

if input_callback:
for i in range(len(args)):
Expand Down

0 comments on commit 87b32e6

Please sign in to comment.