#!/usr/bin/env python3 # requires pyreadline3, numpy, scipy import numpy as np import struct from numpy import pi, exp from numpy import cos, sin, tan, sqrt import os import sys from lib import reliability sys.ps1 = 'CALC > ' sys.ps2 = '..... ' from scipy.special import erf, erfc, erfcinv, erfinv from scipy import constants as scicon if sys.platform == 'win32': def _calcopen(): os.system('explorer.exe {}'.format(sys.path[0])) if False: def _calcedit(editor): os.system('explorer.exe .') else: def _calcopen(): os.system('xdg-open .') if False: def _calcedit(editor): os.system('explorer.exe .') def iswrsig(gamma): """Convert reflection coefficent to VSWR""" return (1+gamma)/(1-gamma) def iswr(retlos): """Convert return loss in dB to VSWR""" if retlos > 0: retlos = -retlos print(' assuming {:.2f} dB input.'.format(retlos)) gamma = idbsig(retlos) return (1+gamma)/(1-gamma) def swrsig(vswr): """Convert VSWR to a reflection coefficent""" return (vswr-1)/(vswr+1) def swr(vswr): """Convert a VSWR to a return loss in dB""" return 20*np.log10((vswr-1)/(vswr+1)) def db(x): return 10*np.log10(x) db.doc_str=""" """ dB = db def db20(x): return 20*np.log10(x) def idb(x): return np.power(10,x/10) def idbsig(x): return np.power(10,x/20) def cosd(x): return np.cos(x*np.pi/180) def sind(x): return np.sin(x*np.pi/180) def tand(x): return np.tan(x*np.pi/180) def ebno2perr(ebno_db): """Compute BPSK P(Error) from Eb/N0 in dB""" print('BPSK from %.1f dB'.format(ebno_db)) ebno_lin = idb(ebno_db) return erfc(sqrt(ebno_lin))/2 def perr2ebno(perr): """Compute BPSK Eb/N0 in dB required to hit a specific P(Error).""" print('BPSK (dB) from %.1e P_err'.format(perr)) return db(erfcinv(2*perr)**2) dBk=db(scicon.Boltzmann) dbk = dBk class rpr: """Wrappers to convert an aribtrary assigned data value to a formated binary representation""" __doc_s__=__doc__ def __init__(self, v): self.v = v @classmethod def _print(cls, b_list, cbytes=0): b = iter(b_list) b_str = '0x' b_merge = [] if b.__length_hint__() % 2 != 0: b_merge.append('{:02x}'.format(b.__next__())) while b.__length_hint__() > 0: b_merge.append(''.join([ '{:02x}'.format(b.__next__()) for _ in range(2) ])) return b_str + '_'.join(b_merge) def __repr__(self): return "{}\t\t{}".format(type(self.v), self.v) @property def f64(self): return self._print(struct.pack('!d', self.v)) @property def f32(self): return self._print(struct.pack('!f', self.v)) @property def i16(self): return self._print(struct.pack('!h', self.v)) @property def u16(self): return self._print(struct.pack('!H', self.v)) @property def i32(self): return self._print(struct.pack('!i', self.v)) @property def u32(self): return self._print(struct.pack('!I', self.v)) @property def i64(self): return self._print(struct.pack('!l', self.v)) @property def u64(self): return self._print(struct.pack('!L', self.v)) if False: import code local_part=locals().copy() rm_locals = [ 'code', ] for key in local_part.keys(): if key[0] == '_': rm_locals.append(key) for key in rm_locals: del local_part[key] print(local_part.keys()) c=code.InteractiveConsole(locals=local_part) #c.runsource('locals()') c.interact() def __lpr__fixreadline__(): import os import atexit import readline readline_history_file = os.path.join(os.path.expanduser('~'), '.pycalc_history') readline.read_history_file(readline_history_file) atexit.register(readline.write_history_file, readline_history_file) if False: __lpr__fixreadline__() __tmp_global_keys__=list(locals().keys()) for name in __tmp_global_keys__: handle=locals()[name] if hasattr(handle, '__doc_s__'): print("{0:10s} {1:s}".format(handle.__name__, handle.__doc_s__)) del __tmp_global_keys__