Skip to content

Electrical

fpc.electrical

electrical.py - electrical engineering calculations.

Covers the fundamentals of circuits: Ohm's law, electrical power, series and parallel resistance, the reactance of capacitors and inductors, and the time constant of an RC circuit.

Electrical

Electrical engineering helpers.

Source code in fpc/electrical.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
class Electrical:
    """Electrical engineering helpers."""

    def ohms_law_voltage(self, *, current, resistance):
        """Voltage from Ohm's law: V = I * R.

        :param current: current I [A]
        :param resistance: resistance R [ohm]
        :returns: voltage [V]
        """
        return current * resistance

    def power(self, *, voltage, current):
        """Electrical power: P = V * I.

        :param voltage: voltage V [V]
        :param current: current I [A]
        :returns: power [W]
        """
        return voltage * current

    def series_resistance(self, *, resistances):
        """Total resistance of resistors in series (they simply add).

        :param resistances: iterable of resistances [ohm]
        :returns: total series resistance [ohm]
        """
        return sum(resistances)

    def parallel_resistance(self, *, resistances):
        """Total resistance of resistors in parallel.

        1/R_total = sum(1/R_i). The result is always smaller than the smallest
        individual resistor.

        :param resistances: iterable of resistances [ohm]
        :returns: total parallel resistance [ohm]
        """
        # Sum the reciprocals (conductances), then invert.
        reciprocal_sum = sum(1.0 / r for r in resistances)
        return 1.0 / reciprocal_sum

    def capacitive_reactance(self, *, frequency, capacitance):
        """Reactance of a capacitor: Xc = 1 / (2*pi*f*C).

        Reactance is the AC equivalent of resistance; it falls as frequency rises.

        :param frequency: signal frequency f [Hz]
        :param capacitance: capacitance C [F]
        :returns: capacitive reactance [ohm]
        """
        return 1.0 / (2 * math.pi * frequency * capacitance)

    def inductive_reactance(self, *, frequency, inductance):
        """Reactance of an inductor: Xl = 2*pi*f*L.

        :param frequency: signal frequency f [Hz]
        :param inductance: inductance L [H]
        :returns: inductive reactance [ohm]
        """
        return 2 * math.pi * frequency * inductance

    def rc_time_constant(self, *, resistance, capacitance):
        """Time constant of an RC circuit: tau = R * C.

        After one tau the capacitor reaches ~63% of its final voltage; after
        ~5 tau it is essentially fully charged.

        :param resistance: resistance R [ohm]
        :param capacitance: capacitance C [F]
        :returns: time constant [s]
        """
        return resistance * capacitance

ohms_law_voltage(*, current, resistance)

Voltage from Ohm's law: V = I * R.

Parameters:

Name Type Description Default
current

current I [A]

required
resistance

resistance R [ohm]

required

Returns:

Type Description

voltage [V]

Source code in fpc/electrical.py
17
18
19
20
21
22
23
24
def ohms_law_voltage(self, *, current, resistance):
    """Voltage from Ohm's law: V = I * R.

    :param current: current I [A]
    :param resistance: resistance R [ohm]
    :returns: voltage [V]
    """
    return current * resistance

power(*, voltage, current)

Electrical power: P = V * I.

Parameters:

Name Type Description Default
voltage

voltage V [V]

required
current

current I [A]

required

Returns:

Type Description

power [W]

Source code in fpc/electrical.py
26
27
28
29
30
31
32
33
def power(self, *, voltage, current):
    """Electrical power: P = V * I.

    :param voltage: voltage V [V]
    :param current: current I [A]
    :returns: power [W]
    """
    return voltage * current

series_resistance(*, resistances)

Total resistance of resistors in series (they simply add).

Parameters:

Name Type Description Default
resistances

iterable of resistances [ohm]

required

Returns:

Type Description

total series resistance [ohm]

Source code in fpc/electrical.py
35
36
37
38
39
40
41
def series_resistance(self, *, resistances):
    """Total resistance of resistors in series (they simply add).

    :param resistances: iterable of resistances [ohm]
    :returns: total series resistance [ohm]
    """
    return sum(resistances)

parallel_resistance(*, resistances)

Total resistance of resistors in parallel.

1/R_total = sum(1/R_i). The result is always smaller than the smallest individual resistor.

Parameters:

Name Type Description Default
resistances

iterable of resistances [ohm]

required

Returns:

Type Description

total parallel resistance [ohm]

Source code in fpc/electrical.py
43
44
45
46
47
48
49
50
51
52
53
54
def parallel_resistance(self, *, resistances):
    """Total resistance of resistors in parallel.

    1/R_total = sum(1/R_i). The result is always smaller than the smallest
    individual resistor.

    :param resistances: iterable of resistances [ohm]
    :returns: total parallel resistance [ohm]
    """
    # Sum the reciprocals (conductances), then invert.
    reciprocal_sum = sum(1.0 / r for r in resistances)
    return 1.0 / reciprocal_sum

capacitive_reactance(*, frequency, capacitance)

Reactance of a capacitor: Xc = 1 / (2pif*C).

Reactance is the AC equivalent of resistance; it falls as frequency rises.

Parameters:

Name Type Description Default
frequency

signal frequency f [Hz]

required
capacitance

capacitance C [F]

required

Returns:

Type Description

capacitive reactance [ohm]

Source code in fpc/electrical.py
56
57
58
59
60
61
62
63
64
65
def capacitive_reactance(self, *, frequency, capacitance):
    """Reactance of a capacitor: Xc = 1 / (2*pi*f*C).

    Reactance is the AC equivalent of resistance; it falls as frequency rises.

    :param frequency: signal frequency f [Hz]
    :param capacitance: capacitance C [F]
    :returns: capacitive reactance [ohm]
    """
    return 1.0 / (2 * math.pi * frequency * capacitance)

inductive_reactance(*, frequency, inductance)

Reactance of an inductor: Xl = 2pif*L.

Parameters:

Name Type Description Default
frequency

signal frequency f [Hz]

required
inductance

inductance L [H]

required

Returns:

Type Description

inductive reactance [ohm]

Source code in fpc/electrical.py
67
68
69
70
71
72
73
74
def inductive_reactance(self, *, frequency, inductance):
    """Reactance of an inductor: Xl = 2*pi*f*L.

    :param frequency: signal frequency f [Hz]
    :param inductance: inductance L [H]
    :returns: inductive reactance [ohm]
    """
    return 2 * math.pi * frequency * inductance

rc_time_constant(*, resistance, capacitance)

Time constant of an RC circuit: tau = R * C.

After one tau the capacitor reaches ~63% of its final voltage; after ~5 tau it is essentially fully charged.

Parameters:

Name Type Description Default
resistance

resistance R [ohm]

required
capacitance

capacitance C [F]

required

Returns:

Type Description

time constant [s]

Source code in fpc/electrical.py
76
77
78
79
80
81
82
83
84
85
86
def rc_time_constant(self, *, resistance, capacitance):
    """Time constant of an RC circuit: tau = R * C.

    After one tau the capacitor reaches ~63% of its final voltage; after
    ~5 tau it is essentially fully charged.

    :param resistance: resistance R [ohm]
    :param capacitance: capacitance C [F]
    :returns: time constant [s]
    """
    return resistance * capacitance