Skip to content

Resonant Frequency & Electrical Impedance #6983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cfcec2c
Resonant Frequency
SKVKPandey Oct 8, 2022
75d4311
Resonant Frequency of LC Circuit
SKVKPandey Oct 11, 2022
83fbe1e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2022
63e739d
Update electronics/resonant_frequency.py
SKVKPandey Oct 15, 2022
bfae159
Update electronics/resonant_frequency.py
SKVKPandey Oct 15, 2022
0ae3830
Update electronics/resonant_frequency.py
SKVKPandey Oct 15, 2022
ed30f58
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
ab0d6eb
Updated resonant_frequency.py
SKVKPandey Oct 15, 2022
c571d56
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
da21dcf
Update electronics/resonant_frequency.py
SKVKPandey Oct 19, 2022
d4a1a5b
Fixed doctest issues in resonant_frequency.py
SKVKPandey Oct 19, 2022
aec3c38
Algorithm for Electrical Impedance
SKVKPandey Oct 20, 2022
e45e47c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 20, 2022
0d72928
Updated Algorithm for Electrical Impedance
SKVKPandey Oct 20, 2022
524cf41
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 20, 2022
bba6342
Update resonant_frequency.py
SKVKPandey Oct 22, 2022
bf1538c
Update electrical_impedance.py
SKVKPandey Oct 22, 2022
5122597
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 22, 2022
a737543
Update resonant_frequency.py
SKVKPandey Oct 22, 2022
75bf82a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 22, 2022
3429519
Update electronics/electrical_impedance.py
SKVKPandey Oct 22, 2022
b3af034
Update electronics/electrical_impedance.py
SKVKPandey Oct 22, 2022
7aeebc7
Update electronics/resonant_frequency.py
SKVKPandey Oct 22, 2022
bb292dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions electronics/electrical_impedance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Electrical impedance is the measure of the opposition that a
circuit presents to a current when a voltage is applied.
Impedance extends the concept of resistance to alternating current (AC) circuits.
Source: https://en.wikipedia.org/wiki/Electrical_impedance
"""

from __future__ import annotations

from math import pow, sqrt


def electrical_impedance(
resistance: float, reactance: float, impedance: float
) -> dict[str, float]:
"""
Apply Electrical Impedance formula, on any two given electrical values,
which can be resistance, reactance, and impedance, and then in a Python dict
return name/value pair of the zero value.

>>> electrical_impedance(3,4,0)
{'impedance': 5.0}
>>> electrical_impedance(0,4,5)
{'resistance': 3.0}
>>> electrical_impedance(3,0,5)
{'reactance': 4.0}
>>> electrical_impedance(3,4,5)
Traceback (most recent call last):
...
ValueError: One and only one argument must be 0
"""
if (resistance, reactance, impedance).count(0) != 1:
raise ValueError("One and only one argument must be 0")
if resistance == 0:
return {"resistance": sqrt(pow(impedance, 2) - pow(reactance, 2))}
elif reactance == 0:
return {"reactance": sqrt(pow(impedance, 2) - pow(resistance, 2))}
elif impedance == 0:
return {"impedance": sqrt(pow(resistance, 2) + pow(reactance, 2))}
else:
raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
import doctest

doctest.testmod()
50 changes: 50 additions & 0 deletions electronics/resonant_frequency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# https://en.wikipedia.org/wiki/LC_circuit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before, perhaps add a brief explanation of LC circuits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the description for LC Circuit.


"""An LC circuit, also called a resonant circuit, tank circuit, or tuned circuit,
is an electric circuit consisting of an inductor, represented by the letter L,
and a capacitor, represented by the letter C, connected together.
The circuit can act as an electrical resonator, an electrical analogue of a
tuning fork, storing energy oscillating at the circuit's resonant frequency.
Source: https://en.wikipedia.org/wiki/LC_circuit
"""

from __future__ import annotations

from math import pi, sqrt


def resonant_frequency(inductance: float, capacitance: float) -> tuple:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tuple of what?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tuple contains the values of inductance and capacitance that are required for calculating the resonant frequency of the LC Circuit.

"""
This function can calculate the resonant frequency of LC circuit,
for the given value of inductance and capacitnace.

Examples are given below:
>>> resonant_frequency(inductance=10, capacitance=5)
('Resonant frequency', 0.022507907903927652)
>>> resonant_frequency(inductance=0, capacitance=5)
Traceback (most recent call last):
...
ValueError: Inductance cannot be 0 or negative
>>> resonant_frequency(inductance=10, capacitance=0)
Traceback (most recent call last):
...
ValueError: Capacitance cannot be 0 or negative
"""

if inductance <= 0:
raise ValueError("Inductance cannot be 0 or negative")

elif capacitance <= 0:
raise ValueError("Capacitance cannot be 0 or negative")

else:
return (
"Resonant frequency",
float(1 / (2 * pi * (sqrt(inductance * capacitance)))),
)


if __name__ == "__main__":
import doctest

doctest.testmod()