N9WIB Projects

Ham Calc

PROJECT DESCRIPTION

Ham Calc is a python program that allows users to perform various amateur radio and basic electronic calculations.

PROJECT

Ham Calc is a python program designed to be run through the command line. The program was written for a linux operating system but with a little tweaking may be run on other operating systems as well. The user may choose to perform a variety of different calculations relevant to ham radio or basic electronics. The software is not fool proof so you need to enter in exactly what it asks for and pay attention to units. Ham calc may perform the following calculations....

Select the code below, copy and paste into your favorite editor and save as hamcalc.py. On the command line make the file executable by typing : sudo chmod 774 hamcalc.py. You may run the file by typing ./hamcalc.py on the command line within the directory which contains your program. I will periodically update the script to include new functions. Send me an email if you would like to see another calculation added.

#!/usr/bin/python3

import os
import math

# CAPACITOR IMPEDANCE FUNCTION

def capacitor_impedance():

    print("Equation for Capacitive Impedance = 1/(2*Pi*F*C) \n")

    frequency = float(input("Enter F Frequency in Hz: "))
    capacitance = float(input("Enter C Capacitance in farads: "))

    impedance = float(1/((2*3.14)*frequency*capacitance))

    print(f"Capacitive Impedance: {impedance} Ohms") 

    return impedance


# INDUCTOR IMPEDANCE FUNCTION

def inductor_impedance():

    print("Equation for Inductive Impedance = (2*Pi*F*L)")

    frequency = float(input("Enter F Frequency in Hz: "))
    inductance = float(input("Enter L Inductance in Henrys: "))

    impedance = float((2*3.14)*frequency*inductance)

    print(f"Inductive Impedance: {impedance} Ohms")

    return impedance

# RMS 

def rms():

    print("Equation for RMS = Peak Voltage / SQRT(2) or Peak Voltage * 0.707")

    voltage = float(input("Enter Peak Voltage: "))
    rms = float(voltage * 0.707)

    print(f"RMS: {rms} Volts") 

    return rms

# AC VOLTAGE AT TIME 

def ac_volt_time():

	print("Equation for AC voltage at time t: V(t) = Vpeak * SIN(360 * Freq Hz * t + phase shift)")

	volt_peak = float(input("Enter Peak AC Voltage: "))
	freq = float(input("Enter Frequency Hz: "))
	t = int(input("Enter Time Seconds: "))
	phase = int(input("Enter Phase Shift Degrees +/-: "))

	volt = float(volt_peak*math.sin(360*freq*t+phase))

	print(f"Voltage at Time {t} seconds: {volt} Volts")

	return volt;

# DEGREES TO RADIANS 

def deg_rad():

        print("Equation for Converting Degrees to Radians: Rad = Degrees * (2*Pi/360)")

        degrees = int(input("Enter Degrees: "))
        
        radians = float(degrees * ((2*3.14)/360))

        print(f"Radians: {radians}")

        return radians;

# RADIANS TO DEGREES 

def rad_deg():

        print("Equation for Converting Radians to Degrees: Degrees = Radians * (360/2*Pi)\n") 

        radians = float(input("Enter Radians: "))

        degrees = float(radians * (360/(2*3.14)))

        print(f"Degrees: {degrees} degrees")

        return degrees

# PERIOD TO FREQUENCY  

def period_freq():

        print("Equation for Converting Period to Frequency: Frequency = 1/Time (seconds)\n")

        time = float(input("Enter Time in Seconds: "))

        frequency = float(1/time)

        print(f"Frequency: {frequency} Hz") 

        return frequency


# FREQUENCY TO PERIOD 

def freq_period():

        print("Equation for Converting Frequency to Period (T): Period (T) = 1/frequency (Hz)\n")

        frequency = float(input("Enter Frequency in Hz: "))

        period = float(1/frequency)

        print(f"Period (T): {period:.3f}  seconds")

        return period

# PHASE SHIFT 

def phase_shift():

    print("Equation for Determining Phase Shift from Period:") 
    print("Phase Shift (deg) = (phase shift seconds/period)*360 \n")

    phase_shift_sec = float(input("Enter Phase Shift in Seconds: "))

    period = float(input("Enter Period in Seconds: "))

    phase_shift = float((phase_shift_sec/period)*360)

    print(f"Phase Shift:  {phase_shift} degrees") 

    return phase_shift

# FREQUENCY TO WAVELENGTH 

def freq_wave():

        print("Equation for Converting Frequency to Wavelength: Wavelength (m) = (299,792,458 m/s)/Freq (Hz) \n")

        frequency = float(input("Enter Frequency in Hz: "))

        wavelength = float((299792458)/frequency)

        print(f"Wavelength: {wavelength:.3f} meters")

        return wavelength

# WAVELENGTH TO FREQUENCY 

def wave_freq():

        print("Equation for Converting Wavelength to Frequency: Frequency (Hz) = (299,792,458 m/s) / Wavelength (meters)\n")

        wavelength = float(input("Enter Wavelength in meters: "))

        frequency = float((299792458)/wavelength)

        print(f"Fequency: {frequency:.3f}  Hz") 

        return frequency

# ANTENNA LENGTH CALCULATOR 

def antenna_length():

    print("Equation for Calculating Length of Antenna based on Desired Frequency")
    print("Half Wave Dipole Calc in Feet: Length (ft) = 468/Desired Freq MHz")
    print("General Formula Full Wavelength in Meters  = 300,000,000 / Frequency in Hz")
    print("300,000,000 is the rough equivlanet to the speed of light in meters / second")
    print("\n")

    frequency = float(input("Enter Desired Frequency in MHz: ")) 
    print("\n")

    frequency_full_ft = float((468*2)/frequency)
    frequency_full_meters = float(frequency_full_ft/3.28)

    frequency_half_ft = float(468/frequency)
    frequency_half_meters = float(frequency_half_ft/3.28)

    frequency_quarter_ft = float((468/frequency)/2)
    frequency_quarter_meters = float(frequency_quarter_ft/3.28)

    print(f"Antenna Length Based on Resonant Frequency: {frequency:.3f} MHz")
    print(f"Full Wave Antenna Length: {frequency_full_ft:.2f} ft {frequency_full_meters:.2f} meters")
    print(f"Half Wave Antenna Length: {frequency_half_ft:.2f} ft {frequency_half_meters:.2f} meters")
    print(f"Quarter Wave Antenna Length: {frequency_quarter_ft:.2f} ft {frequency_quarter_meters:.2f} meters")

    return frequency_full_ft

# HARMONICS 

def harmonics():

    print("Harmonics are multiples of the fundamental frequency")
    print("\n")

    frequency = float(input("Enter Fundamenal Frequency in MHz: "))
    print("\n")

    F2 = float(frequency*2)
    F3 = float(frequency*3)
    F4 = float(frequency*4)
    F5 = float(frequency*5)

    print(f"Fundamental Fequency and First Harmonic: {frequency} MHz")
    print(f"2nd Harmonic F2: {F2:.3f} MHz")
    print(f"3rd Harmonic F3: {F3:.3f} MHz") 
    print(f"4th Harmonic F4: {F4:.3f} MHz")

    return frequency

# OHMS LAW 

def ohms_law():

    print("Ohm's Law: Voltage = Current * Resistance \n")

    print("What would you like to calculate? ")
    print("1. Voltage")
    print("2. Current")
    print("3. Resistance")
    print("\n")

    choice = int(input("Enter your numeric choice: "))

    os.system("clear")

    if choice == 1:

        amps = float(input("Enter Current in Amps: "))
        ohms = float(input("Enter Resistance in Ohms: "))

        volts = (amps * ohms)

        print(f"Voltage: {volts:.2f} volts") 

    elif choice == 2:
            
        volts = float(input("Enter Voltage in Volts: "))
        ohms = float(input("Enter Resistance in Ohms: "))

        amps = float(volts/ohms)

        print(f"Current: {amps:.3f} amps")

    elif choice == 3:

        amps = float(input("Enter Current in Amps: "))
        volts = float(input("Enter Voltage in Volts: "))

        ohms = float(volts/amps)

        print(f"Resistance: {ohms:.3f} ohms")

    else: 

         print(f"Make the right choice")


# RESISTANCE SERIES 

def resistance_series():

    nresistor = [] 
    resistance_total = 0

    print("Resistance in Series = Rt = R1 + R2 + R3 + R(n)")

    resistor_count = int(input("How many resistors? "))

    for i in range(resistor_count):

        value = int(input(f"Enter resistance in ohms for Resistor #{i+1} : "))
        nresistor.append(value)
    
    for i in range(resistor_count): 

       resistance_total = resistance_total + nresistor[i]

    print(f"Total Resistance: {resistance_total} Ohms") 

    return resistance_total


# RESISTANCE PARALLEL 

def resistance_parallel():

    resistance_total = 0

    print("Resistance in Parallel = Rt = 1 / (1/R1 + 1/R2 + 1/R3 + 1/R(n))")

    resistor_count = int(input("How many resistors? "))

    nresistor = []
    nresistor_inv = []

    for i in range(resistor_count):

        value = int(input(f"Enter resistance in ohms for Resistor # {i+1}: "))
        nresistor.append(value)
        nresistor_inv.append(1/value)

        #print(f"RES: {nresistor[i]} INV {nresistor_inv[i]} ")

    for i in range(resistor_count):

        resistance_total = resistance_total + nresistor_inv[i]

    resistance_total = 1/resistance_total

    print(f"Total Resistance: {resistance_total:.3f} Ohms") 

    return resistance_total

# RESONANT FREQUENCY

def resonant_freq():

    print("Formula to calculate the resonant frequency using Capacitive impedance and Inductive Impedance")
    print("Zc = Zl Capacitive Impedance = Inductive Impedance at resonance")
    print("(Zc) 1/2PiFC = (Zl) 2PiFL")
    print("F resonant frequency = 1/(2*Pi*SQRT(LC))")
    print("At resonance, current and power are maximum")
    print("\n")

    capacitance = float(input("Enter capacitance (C) in Farads: "))
    inductance = float(input("Enter inductance (H) in Henrys: "))

    frequency = float(1/(2*3.14*((capacitance*inductance)**0.5)))

    print("\n")
    print(f"Resonant Frequency: {frequency:.2f} Hz ")

    return frequency 

# INDUCTANCE FOR RESONANT CIRCUIT

def inductance_resonant():

    print("Formula to calculate inductance from resonant frequency and capacitance")
    print("L = (1/(4*Pi^2*F^2*C))")
    print("\n")

    frequency = float(input("Enter resonant frequency (F) in Hz: "))
    capacitance = float(input("Enter capacitance (C) in Farads: "))

    inductance = float(1/(4*(3.14**2)*(frequency**2)*capacitance))

    print("\n")
    print(f"Inductance: {inductance} Henrys")

    return inductance 

# CAPACITANCE FOR RESONANT CIRCUIT

def capacitance_resonant():

    print("Formula to calculate capacitance from resonant frequency and inductance")
    print("C = (1/(4*Pi^2*F^2*L))")
    print("\n")

    frequency = float(input("Enter resonant frequency (F) in Hz: "))
    inductance = float(input("Enter inductance (L) in Henrys: "))

    capacitance = float(1/(4*(3.14**2)*(frequency**2)*inductance))

    print("\n")
    print(f"Capacitance: {capacitance} Farads")

    return capacitance

# QUALITY FACTOR  

def q():

    print("Calculate Qulaity Factor (Q) and Bandwidth for Series Circuit at Resonance")
    print("The higher the Quality Factor (Q) the more narrow the bandwidth")
    print("Q = (Zc or Zl / Resistance) at resonance") 
    print("Zc Capacitive Impedance, Zl Inductive Impedance")
    print("Band width = Frequency at Resonance Hz / Q")
    print("\n")

    impedance = float(input("Enter the Impedance of the reactive component in Ohms: "))
    resistance = float(input("Enter Resistance in Ohms: "))

    q = float(impedance/resistance)

    print("\n")
    print(f"Quality Factor Q: {q:.1f}")

    return q 

# BANDWIDTH 

def band_width():

    print("Calculate Bandwidth for Series Circuit at Resonance Using Q")
    print("Band width = Frequency at Resonance Hz / Q")
    print("\n")

    freq = float(input("Enter the Resonant Frequency in Hz: "))
    q = float(input("Enter the value of Q: "))

    bw = float(freq/q)

    print("\n")
    print(f"Band Width: {bw:.2f} Hertz")

    return bw

# RC TIME CONSTANT TAU 

def rc():

    print("Calculate RC Time Constant Tau and voltage at time t")
    print("Time Constant Tau = Resistance Ohms * Capacitance in Farads ")
    print("Time to full charge or discharge is 5 Time Constants (5*Tau)")
    print("Capacitor Charge Voltage at time (t) = V * (1-e^(-t/Tau))")
    print("Capacitor Discharge Voltage at time (t) = V supply * e^(-t/Tau)")
    print("\n")

    resistance = float(input("Enter Resistance Ohms: "))
    capacitance = float(input("Enter Capacitance Farads: "))

    tau = float(resistance*capacitance)
    full_rc = float(resistance*capacitance*5)
    full_rc_min = full_rc / 60

    print("\n")
    print(f"Time Constant is {tau:.1f}")
    print(f"Time to Full Charge or Discharge is {full_rc:0.1f} seconds or {full_rc_min:0.1f} minutes")
    print("\n")

    print("1. Calculate Voltage for Capacitor Charge")
    print("2. Calculate Voltage for Capacitor Discharge")
    print("\n")
    rc_choice = input("Enter Numeric Choice: ")

    rc_time = float(input("Enter time in seconds: "))
    voltage = float(input("Enter supply voltage: "))
    print("\n")

    if rc_choice == "1":

        voltage_calc = float(voltage*(1-(2.7182**((-rc_time)/tau))))

        print(f"Voltage at time {rc_time} seconds is {voltage_calc:.6f} volts") 

    if rc_choice == "2":

        voltage_calc = float(voltage*(2.7182**((-rc_time)/tau)))

        print(f"Voltage at time {rc_time} seconds is {voltage_calc:.6f} volts") 


# DB 

def db():

    print("Calculate Deci-Bell value for Power or Voltage")
    print("Gain dB Power = 10*LOG(P out / P in)")
    print("Gain dB Voltage = 20*LOG(V out / V in)")
    print("\n")
    print("1. Calculate Gain for Power")
    print("2. Calculate Gain for Voltage")
    print("3. Calculate Factor Change for Power dB")
    print("4. Calculate Factor Change for Voltage dB")
    print("\n")
    db_choice = input("Enter choice 1,2,3 or 4: ")
    print("\n")

    if db_choice == "1":

        print("Use the same units for input and output power")
        pin = float(input("Enter Input Power: "))
        pout = float(input("Enter Output Power: "))

        db = float(10*math.log10(pout/pin))
        print("\n")

        print(f"Gain is {db:.1f} dB")

    if db_choice == "2":

        print("Use the same units for input and output power")
        vin = float(input("Enter Input Voltage: "))
        vout = float(input("Enter Output Voltage: "))

        db = float(20*math.log10(vout/vin))
        print("\n")

        print(f"Gain is {db:.1f} dB")

    if db_choice == "3":

        db = float(input("Enter the Power decibell value: "))

        factor = float(10**(db/10))
        print("\n")

        print(f"Power Change {factor:.1f} times")

    if db_choice == "4":

        db = float(input("Enter the Voltage decibell value: "))

        factor = float(10**(db/20))
        print("\n")

        print(f"Voltage Change {factor:.1f} times")


# MAIN 

def main():

    os.system("clear")

    while True:

        # select calculation

        print("## WELCOME TO THE ELECTRICAL ENGINEERING CALCULATOR ## \n")

        print("1. Capacitor Impedance Calculator")
        print("2. Inductor Impedance Calculator")
        print("3. RMS Calculator")
        print("4. AC Voltage at Time (t)")
        print("5. Convert Degrees to Radians")
        print("6. Convert Radians to Degrees")
        print("7. Calculate Frequency from Period")
        print("8. Calculate Period from Frequency")
        print("9. Calculate Phase Shift in Degrees from Period")
        print("10. Calculate Wavelength from Frequency")
        print("11. Calculate Frequency from Wavelength")
        print("12. Calculate Antenna Length for Resonant Frequency")
        print("13. Calculate Frequency Harmonics")
        print("14. Ohms Law Calculate Voltage, Current, Resistance")
        print("15. Resistance in Series")
        print("16. Resistance in Parallel")
        print("17. Resonant Frequency")
        print("18. Inductance from Resonant Freq and Capacitance")
        print("19. Capacitance from Resonant Freq and Inductance")
        print("20. Quality Factor Q")
        print("21. Bandwidth from Resonant Freq and Q")
        print("22. Time Constant RC Tau")
        print("23. Gain in dB")

        print("\n")
        choice = int(input("Enter the Number of Your Selection: "))

        os.system("clear")

        if choice == 1:
            capacitor_impedance()

        elif choice == 2: 
    	    inductor_impedance()

        elif choice == 3: 
                rms()

        elif choice == 4: 
                ac_volt_time()

        elif choice == 5:
                deg_rad()

        elif choice == 6:
                rad_deg()

        elif choice == 7:
                period_freq()

        elif choice == 8:
                freq_period()

        elif choice == 9: 
                phase_shift()

        elif choice == 10:
                freq_wave()

        elif choice == 11: 
                wave_freq()

        elif choice == 12: 
                antenna_length()

        elif choice == 13: 
                harmonics()

        elif choice == 14:
                ohms_law()

        elif choice == 15: 
                resistance_series()

        elif choice == 16: 
                resistance_parallel()

        elif choice == 17:
                resonant_freq()

        elif choice == 18:
                inductance_resonant()

        elif choice == 19:
                capacitance_resonant()

        elif choice == 20:
                q()

        elif choice == 21:
                band_width()

        elif choice == 22:
                rc()

        elif choice == 23:
                db()

        print("\n")
        print("1. Calculate another value ") 
        print("2. Quit ")
        print("\n")
        choice_2 = int(input("Make Numeric Selection: "))

        if (choice_2 == 1):

            os.system("clear")
            print("Ok we will start over")

        elif (choice_2 == 2):
            break

main()

Ham Calc Python Code