SmallPythonProjects/ElectronicCalculator.py
2021-07-01 14:17:59 +02:00

123 lines
3.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 07:10:02 2021
@author: angoosh
"""
import math
import numpy as np
from matplotlib import pyplot as plt
def Rseries(R):
Rsum = 0
for i in range(0,len(R)):
Rsum += R[i]
return Rsum
def Rparalel(R):
Rsum = 0
for i in range(0,len(R)):
Rsum += (1/R[i])
return 1/Rsum
def VoltageDiv(U,R1,R2,Rload = float('inf')):
Rl = [R2,Rload]
R2 = Rparalel(Rl)
Uout = U*(R2/(R1+R2))
Imax = (U*(R1/(R1+R2)))/R1
return Uout,Imax
def RC(R,C):
tau = R*C
f = 1/(2*3.14159*tau)
return(tau,f)
def RCfreqresponse(R,C):
ann3dbf, ann3dbV = 0,0
tau,f0 = RC(R,C)
f = -4#pow(10,-4)
Vout = []
Fout = []
db3 = []
impedance = []
for i in range(0,1200):
impedance.append(1/(2*math.pi*C)+R)
db3.append(1/pow(2,0.5))
Vout.append(abs(1/pow(1+pow((2*3.14159*pow(10,f))*C*R,2),0.5)))
Fout.append(pow(10,f))
if pow(10,f) < f0:
pass
elif (pow(10,f) >= f0) and ann3dbf != 0:
ann3dbf = pow(10,f)
ann3dbV = abs(1/pow(1+pow((2*3.14159*pow(10,f))/(1/f0),2),0.5))
#Vout.append(1/(1+2*3.14159*f*R*C))
f += 0.01
plt.plot(Fout,Vout)
plt.plot(Fout,db3,color="red")
plt.xscale('log')
plt.xlabel("Frequency")
plt.ylabel("Voltage")
plt.title('RC frequency response')
plt.grid(True)
plt.text(x=pow(10,5), y=1, s="Tau = "+str(round(tau,5)))
plt.text(x=pow(10,5), y=0.95, s="f0 = "+str(round(f0,2))+"Hz")
print(f0)
plt.show()
while True:
print("What do you want to calculate?")
print(" 1: Rseries format: R1,R2,R3,...")
print(" 2: Rparalel format: R1,R2,R3,...")
print(" 3: Voltage divider format: U,R1,R2,Rload")
print(" 4: RC filter format: R,C")
print(" 5: RC frequency response format: R,C")
inp = input(": ")
if inp == "1":
x = input("Resistor values: ")
x = x.split(",")
xint = []
for i in range(0,len(x)):
xint.append(float(x[i]))
print(str(Rseries(xint))+"Ω")
elif inp == "2":
x = input("Resistor values: ")
x = x.split(",")
xint = []
for i in range(0,len(x)):
xint.append(float(x[i]))
print(str(Rparalel(xint))+"Ω")
elif inp == "3":
x = input("Voltage divider params: ")
x = x.split(",")
xint = []
for i in range(0,len(x)):
xint.append(float(x[i]))
if len(xint) == 3:
vdiv = VoltageDiv(xint[0],xint[1],xint[2])
else:
vdiv = VoltageDiv(xint[0],xint[1],xint[2],xint[3])
print(str(vdiv[0])+"V, "+str(vdiv[1])+"A")
elif inp == "4":
x = input("RC values: ")
x = x.split(",")
xint = []
for i in range(0,len(x)):
xint.append(float(x[i]))
rc = RC(xint[0],xint[1])
print(str(rc[1])+"Hz")
elif inp == "5":
x = input("RC values: ")
x = x.split(",")
xint = []
for i in range(0,len(x)):
xint.append(float(x[i]))
RCfreqresponse(xint[0],xint[1])
elif inp == "exit":
break
else:
pass
print("")