Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevan committed Oct 26, 2024
1 parent 48fca4f commit d8caee5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ycx_complex_numbers/H.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self, c=None):


class NetH(Net):
"""H - (hybrid) 2-port-node parameters."""
def __init__(self, h11=None, h12=None, h21=None, h22=None):
super().__init__(c11=H(h11), c12=H(h12), c21=H(h21), c22=H(h22))

Expand Down Expand Up @@ -40,6 +41,7 @@ def is_symmetrical(self):
return self.determinant == 1

def to_a(self):
"""Convert to ABCD parameters"""
return cn.Neta(
a11=-self.determinant / self.h21,
a12=-self.h11 / self.h21,
Expand All @@ -48,6 +50,7 @@ def to_a(self):
)

def to_b(self):
"""Convert to ABCD' parameters"""
return cn.Netb(
b11=1 / self.h12,
b12=self.h11 / self.h12,
Expand All @@ -59,6 +62,7 @@ def to_ABCD(self):
return self.to_a()

def to_Y(self):
"""Convert to Y parameters"""
return cn.NetY(
y11=1 / self.h11,
y12=-self.h12 / self.h11,
Expand All @@ -67,6 +71,7 @@ def to_Y(self):
)

def to_Z(self):
"""Convert to Z parameters"""
return cn.NetZ(
z11=self.determinant / self.h22,
z12=self.h12 / self.h22,
Expand All @@ -75,6 +80,7 @@ def to_Z(self):
)

def to_S(self, Z0=50 + 0j):
"""Convert to S parameters"""
hi = self.h11 / Z0
hr = self.h12
hf = self.h21
Expand Down
5 changes: 5 additions & 0 deletions ycx_complex_numbers/S.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, c=None):


class NetS(Net):
"""S - Scatter 2-port-node parameters."""
def __init__(self, s11=None, s12=None, s21=None, s22=None):
super().__init__(c11=S(s11), c12=S(s12), c21=S(s21), c22=S(s22))

Expand All @@ -33,6 +34,7 @@ def s22(self):
return self._c22

def to_Y(self, Z0=50 + 0j):
"""Convert to Y parameters"""
return NetY(
y11=((1 + self.s22) * (1 - self.s11) + self.s12 * self.s21)
/ ((1 + self.s11) * (1 + self.s22) + self.s12 * self.s21)
Expand All @@ -49,6 +51,7 @@ def to_Y(self, Z0=50 + 0j):
)

def to_Z(self, Z0=50+0j):
"""Convert to Z parameters"""
d = (1 - self.s11) * (1 - self.s22) - self.s12 * self.s21
return cn.NetZ(
z11=((1 + self.s11) * (1 - self.s22) + self.s12 * self.s21) / d * Z0,
Expand All @@ -58,6 +61,7 @@ def to_Z(self, Z0=50+0j):
)

def to_a(self):
"""Convert to ABCD parameters"""
d = 2 * self.s21
return cn.Neta(
a11=((1 + self.s11) * (1 - self.s22) + self.s12 * self.s21) / d,
Expand All @@ -67,6 +71,7 @@ def to_a(self):
)

def to_H(self, Z0=50 + 0j):
"""Convert to H parameters"""
d = (1 - self.s11) * (1 + self.s22) + self.s12 * self.s21
return cn.NetH(
h11=((1 + self.s11) * (1 + self.s22) - self.s12 * self.s21) / d * Z0,
Expand Down
6 changes: 6 additions & 0 deletions ycx_complex_numbers/Y.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def B(self):
return self._c.imag

class NetY(Net):
"""Y - Admittance 2-port-node parameters."""
def __init__(self, y11=None, y12=None, y21=None, y22=None):
super().__init__(c11=Y(y11), c12=Y(y12), c21=Y(y21), c22=Y(y22))

Expand Down Expand Up @@ -47,6 +48,7 @@ def is_symmetrical(self):
return self.y11 == self.y22

def to_a(self):
"""Convert to ABCD parameters"""
return cn.Neta(
a11=-self.y22 / self.y21,
a12=-1 / self.y21,
Expand All @@ -55,6 +57,7 @@ def to_a(self):
)

def to_b(self):
"""Convert to ABCD' parameters"""
return cn.Netb(
b11=-self.y11 / self.y12,
b12=-1 / self.y12,
Expand All @@ -66,6 +69,7 @@ def to_ABCD(self):
return self.to_a()

def to_H(self):
"""Convert to H parameters"""
return cn.NetH(
h11=1 / self.y11,
h12=-self.y12 / self.y11,
Expand All @@ -74,6 +78,7 @@ def to_H(self):
)

def to_Z(self):
"""Convert to Z parameters"""
return cn.NetZ(
z11=self.y22 / self.determinant,
z12=-(self.y12 / self.determinant),
Expand All @@ -82,6 +87,7 @@ def to_Z(self):
)

def to_S(self, Z0=50 + 0j):
"""Convert to S parameters"""
yi = self.y11 * Z0
yr = self.y12 * Z0
yf = self.y21 * Z0
Expand Down
6 changes: 6 additions & 0 deletions ycx_complex_numbers/Z.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def X(self):
return self._c.imag

class NetZ(Net):
"""Z - Impedance 2-port-node parameters."""
def __init__(self, z11=None, z12=None, z21=None, z22=None):
super().__init__(c11=Z(z11), c12=Z(z12), c21=Z(z21), c22=Z(z22))

Expand Down Expand Up @@ -47,6 +48,7 @@ def is_symmetrical(self):
return self.z11 == self.z22

def to_a(self):
"""Convert to ABCD parameters"""
return cn.Neta(
a11=self.z11 / self.z21,
a12=self.determinant / self.z21,
Expand All @@ -55,6 +57,7 @@ def to_a(self):
)

def to_b(self):
"""Convert to ABCD' parameters"""
return cn.Netb(
b11=self.z22 / self.z12,
b12=self.determinant / self.z12,
Expand All @@ -66,6 +69,7 @@ def to_ABCD(self):
return self.to_a()

def to_H(self):
"""Convert to H parameters"""
return cn.NetH(
h11=self.determinant / self.z22,
h12=self.z12 / self.z22,
Expand All @@ -74,6 +78,7 @@ def to_H(self):
)

def to_Y(self):
"""Convert to Y parameters"""
return cn.NetY(
y11=self.z22 / self.determinant,
y12=-self.z12 / self.determinant,
Expand All @@ -82,6 +87,7 @@ def to_Y(self):
)

def to_S(self, Z0=50 + 0j):
"""Convert to S parameters"""
zi = self.z11 / Z0
zr = self.z12 / Z0
zf = self.z21 / Z0
Expand Down
8 changes: 7 additions & 1 deletion ycx_complex_numbers/a.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class a(Complex):
"""ABCD - A ABCD (fwd Chain/Cascade/Transmission) parameters."""
"""ABCD - A ABCD (fwd Chain/Cascade/Transmission) parameter."""

_symbol = "a"

Expand All @@ -12,6 +12,7 @@ def __init__(self, c=None):


class Neta(Net):
"""ABCD - An ABCD (fwd Chain/Cascade/Transmission) 2-port-node parameters."""
def __init__(self, a11=None, a12=None, a21=None, a22=None):
super().__init__(c11=a(a11), c12=a(a12), c21=a(a21), c22=a(a22))

Expand Down Expand Up @@ -56,6 +57,7 @@ def is_symmetrical(self):
return self.A == self.D

def to_b(self):
"""Convert to ABCD' parameters"""
return cn.Netb(
b11=self.D / self.determinant,
b12=self.B / self.determinant,
Expand All @@ -64,6 +66,7 @@ def to_b(self):
)

def to_H(self):
"""Convert to H parameters"""
return cn.NetH(
h11=self.B / self.D,
h12=self.determinant / self.D,
Expand All @@ -72,6 +75,7 @@ def to_H(self):
)

def to_Y(self):
"""Convert to Y parameters"""
return cn.NetY(
y11=self.D / self.B,
y12=-self.determinant / self.B,
Expand All @@ -80,6 +84,7 @@ def to_Y(self):
)

def to_Z(self):
"""Convert to Z parameters"""
return cn.NetZ(
z11=self.A / self.C,
z12=self.determinant / self.C,
Expand All @@ -88,6 +93,7 @@ def to_Z(self):
)

def to_S(self):
"""Convert to S parameters"""
d = self.A + self.B + self.C + self.D
return cn.NetS(
s11=(self.A + self.B - self.C - self.D) / d,
Expand Down
7 changes: 6 additions & 1 deletion ycx_complex_numbers/b.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class b(Complex):
"""ABCD' - A ABCD' (rev Chain/Cascade/Transmission) parameters."""
"""ABCD' - A ABCD' (rev Chain/Cascade/Transmission) parameter."""

_symbol = "b"

Expand All @@ -12,6 +12,7 @@ def __init__(self, c=None):


class Netb(Net):
"""ABCD' - An ABCD' (rev Chain/Cascade/Transmission) 2-port-node parameters."""
def __init__(self, b11=None, b12=None, b21=None, b22=None):
super().__init__(c11=b(b11), c12=b(b12), c21=b(b21), c22=b(b22))

Expand Down Expand Up @@ -56,6 +57,7 @@ def is_symmetrical(self):
return self.A == self.D

def to_a(self):
"""Convert to ABCD parameters"""
return cn.Neta(
a11=self.D / self.determinant,
a12=self.B / self.determinant,
Expand All @@ -64,6 +66,7 @@ def to_a(self):
)

def to_H(self):
"""Convert to H parameters"""
return cn.NetH(
h11=self.B / self.A,
h12=1 / self.A,
Expand All @@ -72,6 +75,7 @@ def to_H(self):
)

def to_Y(self):
"""Convert to Y parameters"""
return cn.NetY(
y11=self.A / self.B,
y12=-1 / self.B,
Expand All @@ -80,6 +84,7 @@ def to_Y(self):
)

def to_Z(self):
"""Convert to Z parameters"""
return cn.NetZ(
z11=self.D / self.C,
z12=1 / self.C,
Expand Down
9 changes: 8 additions & 1 deletion ycx_complex_numbers/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, c=None):
self._c = c + 0j

def from_polar(self, mag, angle):
"""Create a Complex instance from a polar coordinate using magnitude and phase angle"""
x = mag * np.cos(np.deg2rad(angle))
y = mag * np.sin(np.deg2rad(angle))
self._c = complex(x, y)
Expand Down Expand Up @@ -62,14 +63,19 @@ def __abs__(self):
return abs(self._c)

def as_complex(self):
"""Return Complex as a pure python complex number"""
return self._c

def as_polar(self):
"""Return as polar magnitude and phase angle"""
mag = abs(self._c)
angle = math.degrees(math.atan2(self._c.imag, self._c.real))
return {"mag": mag, "angle": angle}

def as_conjugate(self):
"""Return the complex conjugate
see also conjugate() property"""
return self.__class__((self._c.real - 1j * self._c.imag))

@property
Expand Down Expand Up @@ -223,7 +229,7 @@ def c22(self):

@property
def m(self):
"""as numpy array matrix"""
"""as a numpy square 2x2 array matrix"""
return np.array([[self.c11, self.c12], [self.c21, self.c22]])

def __add__(self, other):
Expand Down Expand Up @@ -295,4 +301,5 @@ def __rmul__(self, other):

@property
def determinant(self):
"""Return the determinant of the complex square matrix"""
return self._c11 * self._c22 - self._c12 * self._c21

0 comments on commit d8caee5

Please sign in to comment.