Python has a package called Lcapy that can analyze linear circuits. Lcapy has many functions, but this article describes how to find the transfer function of a circuit symbolically (mathematical).
In addition, the following article explains how to enable Lcapy. [Circuit x Python] How to enable the linear circuit analysis package Lcapy
Python: 3.7.4、SymPy: 1.6.2、Lcapy: 0.67.0
Lcapy is still a developing package, so the code in this article may not work with the same ver as above. Even in my environment, the code in the official documentation may not work ... Be aware that it may not work even if your code isn't bad!
This article uses the Circuit method to find the transfer function. As a first example, let's solve the transfer function of the RC circuit in the figure below.
Code for the transfer function:
Example 1-1)
from lcapy import *
cct = Circuit("""
Vi 1 0
R 1 2 RF
C 2 0 CF
""")
H = (cct.C.V(s) / cct.Vi.V(s)).simplify()
H
Execution result:
Commentary: I am entering a netlist inside Circuit ("" "" ""). The applied voltage of C is calculated by cct.C.V (s), and the transfer function is calculated by dividing this by the input voltage cct.Vi.V (s). The transfer function is simplified with symplify. In this way, we can see that the transfer function was obtained symbolically (mathematically).
It is also possible to omit the element values (RF and CF in Example 1-1). In this case, element value = element name (R and C).
Example 1-2)
from lcapy import *
cct = Circuit("""
Vi 1 0
R 1 2
C 2 0
""")
H = (cct.C.V(s) / cct.Vi.V(s)).simplify()
H
Execution result:
Of course, it is also possible to enter the element value as a numerical value instead of a symbol.
code:
Example 1-3)
from lcapy import *
cct = Circuit("""
Vi 1 0
R 1 2 1e3
C 2 0 1e-6
""")
H = (cct.C.V(s) / cct.Vi.V(s)).simplify()
H
Execution result:
It is also possible to find the transfer function symbolically and then substitute the value.
Example 1-4)
from lcapy import *
cct = Circuit("""
Vi 1 0
R 1 2 RF
C 2 0 CF
""")
H = (cct.C.V(s) / cct.Vi.V(s)).simplify()
H.subs('CF',1e-6).subs('RF',1e3)
Execution result:
If it is a simple circuit like Example 1), it will not take much time to input the netlist by yourself. However, when it comes to a slightly complicated circuit like the one shown below, self-input is troublesome.
You don't have to write the netlist yourself by creating a schematic with the circuit design tool, generating a netlist there, and passing it to the Lcapy code.
The above figure was drawn with LTSPICE, but in the case of LTSPICE, you can get the netlist from View => SPICE NETLIST.
Paste the netlist into the Lcapy code as follows:
Example 1-3) RC circuit
from lcapy import *
cct = Circuit("""
V1 IN 0 s 1
R1 OUT N001 RP
C1 OUT 0 CM
L1 IN N001 LP
R2 OUT 0 RM
C2 OUT N001 CP
""")
H = cct["OUT"].V(s).simplify()
H
The transfer function is obtained by a method different from Example 1). First, V1 is modified as follows in the copied and pasted netlist. V1 IN 0 s 1 It means a signal with amplitude = 1 in the s domain. The transfer function H is obtained by measuring the voltage of the node "OUT".
Execution result:
Non-inverting amplifiers like the one shown below can also be analyzed with Lcapy.
VCVS (voltage control voltage signal source) is used as the operational amplifier. If you draw with LTSPICE, it will look like the figure below.
As in Example 2), copy the netlist and modify V1 to obtain the transfer function.
Example 3-1)
from lcapy import *
cct = Circuit("""
R1 N001 N002 RG
R2 OUT N001 RF
V1 N002 0 s 1
C1 N001 OUT CF
E1 OUT 0 0 N001 AOL
C2 N001 0 CG
""")
H = cct["OUT"].V(s).simplify()
H
Execution result:
If you want to analyze with an operational amplifier that has one pole, you can create a circuit diagram like the one shown below.
The characteristics of the operational amplifier shall be expressed by the following equation.
Example 3-2)
from lcapy import *
cct = Circuit("""
E1 0 OUT N002 0 1
C1 N002 0 {1/p}
R1 N002 0 1
R2 OUT INN RF
R3 INN N001 RG
V1 N001 0 s 1
G1 0 N002 0 INN AOL
""")
H = cct["OUT"].V(s).simplify()
H
Execution result:
A fully differential amplifier can be calculated with Lcapy using the circuit shown below.
The Lcapy package, which allows you to easily find the transfer function without solving the circuit equations, is very convenient. Once you have a transfer function, you'll want to expand it (such as finding the poles) or do a numerical analysis, which I'll cover in a separate article.
[Circuit x Python] How to expand and calculate transfer function using Lcapy
Recommended Posts