Skip to content

Instantly share code, notes, and snippets.

@LukeGary462
Last active November 17, 2021 19:27
Show Gist options
  • Select an option

  • Save LukeGary462/fdd4fffbc1647e37829cefef861d738e to your computer and use it in GitHub Desktop.

Select an option

Save LukeGary462/fdd4fffbc1647e37829cefef861d738e to your computer and use it in GitHub Desktop.
Omega - Numworks
'''conversion functions
'''
from math import (sqrt, pi, floor, log10)
def powerise10(value):
""" Returns value as a * 10 ^ b with 0<= a <10
"""
if value == 0: return 0 , 0
_neg = value <0
if _neg : value = -value
_a = 1.0 * value / 10**(floor(log10(value)))
_b = int(floor(log10(value)))
if _neg : _a = -_a
return _a ,_b
def eng(value):
"""Return a string representing value in an engineer friendly notation"""
_a , _b = powerise10(float(value))
if -3<_b<3: return "%.4g" % value
_a = _a * 10**(_b%3)
_b = _b - _b%3
return "%.4g*10^%s" %(_a,_b)
BOLTZMAN = 1.38065e-23
def celsius_to_kelvin(temp_celsius):
'''celsius to kelvin'''
return 273.15 + temp_celsius
def resistor_vrms(temp_kelvin, resistance, bandwidth):
'''resistor rms thermal noise'''
return eng(sqrt((4 * BOLTZMAN)*temp_kelvin*resistance*bandwidth))
def vrms_to_vpp(vrms):
'''rms to pp'''
return eng(6.6 * vrms)
def lc_lowpass_cutoff():
'''3db cutoff of an LC filter'''
l = float(input('Inductance (H): '))
c = float(input('Capacitance (F): '))
a = eng((1/(2*pi*sqrt(l*c))))
print(eng(a), 'Hz')
def rc_lowpass_cutoff():
'''3db cutoff of an RC filter'''
r = float(input('Resistance (Ohms): '))
c = float(input('Capacitance (F): '))
a = eng((1/(2*pi*r*c)))
print(eng(a), 'Hz')
def capacitor_impedance():
'''Impedance of a Capacitor'''
freq = float(input('Frequency (Hz): '))
c = float(input('Capacitance (F): '))
a = eng((1/(2*pi*freq*c)))
print(eng(a), 'Ohms')
def inductor_impedance():
'''Impedance of a Inductor'''
freq = float(input('Frequency (Hz): '))
l = float(input('Inductance (H): '))
a = eng((2*pi*freq*l))
print(eng(a), 'Ohms')
def dc_voltage_divider():
'''Voltage Divider'''
Z1 = float(input('Impedance Top (Ohms): '))
Z2 = float(input('Impedance Bottom (Ohms): '))
Vin = float(input('Input Voltage (Volts): '))
a = eng(Vin*(Z2/(Z1 + Z2)))
print(eng(a), 'Volts')
def ac_voltage_divider():
'''ac voltage divider
'''
freq = float(input('Operating Freq (Hz): '))
input_cap = float(input('In-Cap (F, 0 if unused): '))
input_ind = float(input('In-Ind (H, 0 if unused): '))
input_res = float(input('In-res (Ohms): '))
output_cap = float(input('Out-Cap (F, 0 if unused): '))
output_ind = float(input('Out-Ind (H, 0 if unused): '))
output_res = float(input('Out-res (Ohms): '))
Vin = float(input('Input Voltage (Vpp): '))
Z1 = 0
if input_cap > 0:
Z1 = (1/(2*pi*freq*input_cap))
elif input_ind > 0:
Z1 = (2*pi*freq*input_ind)
Z1 = Z1 + input_res
Z2 = 0
if output_cap > 0:
Z2 = (1/(2*pi*freq*output_cap))
elif output_ind > 0:
Z2 = (2*pi*freq*output_ind)
Z2 = Z2 + output_res
a = eng(Vin*(Z2/(Z1 + Z2)))
print(eng(a), 'Vpp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment