fixed newlite shit

This commit is contained in:
Luke 2023-07-16 11:17:38 -07:00
parent a39eb6d870
commit 5bbde54bc1
3 changed files with 185 additions and 185 deletions

View file

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

View file

@ -1,10 +1,10 @@
# Design Target # Design Target
## Curses Stack based calculator ## Curses Stack based calculator
* platform layer event loop * platform layer event loop
* use ncurses * use ncurses
* React to single keypress events @ a fixed interval. * React to single keypress events @ a fixed interval.
* operations are based on single letter keys * operations are based on single letter keys
* Write input to a prompt * Write input to a prompt
* display the stack every cycle * display the stack every cycle
* display shortcuts at the bottom * display shortcuts at the bottom

View file

@ -1,19 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import numpy as _np import numpy as _np
C_YEARS_TO_BLN_HOURS = (365.24 * 24) / 1_000_000_000 C_YEARS_TO_BLN_HOURS = (365.24 * 24) / 1_000_000_000
def to_fits(reliability, time_yrs=5): def to_fits(reliability, time_yrs=5):
time_hrs = time_yrs*C_YEARS_TO_BLN_HOURS time_hrs = time_yrs*C_YEARS_TO_BLN_HOURS
return _np.log(reliability)/-time_hrs return _np.log(reliability)/-time_hrs
def to_rel(FITs, time_yrs=5): def to_rel(FITs, time_yrs=5):
time_hrs = time_yrs*C_YEARS_TO_BLN_HOURS time_hrs = time_yrs*C_YEARS_TO_BLN_HOURS
return _np.exp(-time_hrs * FITs) return _np.exp(-time_hrs * FITs)
def to_urel(FITs, time_yrs=5): def to_urel(FITs, time_yrs=5):
time_hrs = time_yrs*C_YEARS_TO_BLN_HOURS time_hrs = time_yrs*C_YEARS_TO_BLN_HOURS
return 1-_np.exp(-time_hrs * FITs) return 1-_np.exp(-time_hrs * FITs)
from_fits=to_rel from_fits=to_rel
from_rel=to_fits from_rel=to_fits