[Circuit x Python] How to find the transfer function of a circuit using Lcapy

Introduction

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

environment

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!

Example 1) RC circuit

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. image.png

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:

\frac{1}{C_{F} R_{F} s + 1}

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:

\frac{1}{C R s + 1}

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: $\frac{1000}{s + 1000}$

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: $\frac{1}{\frac{s}{1000} + 1}$

Example 2) More complex passive circuit

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. image.png

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. image.png

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: $\frac{R_{M} \left(C_{P} R_{P} s + 1\right)}{C_{M} C_{P} L_{P} R_{M} R_{P} s^{3} + L_{P} s^{2} \left(C_{M} R_{M} + C_{P} R_{P}\right) + R_{M} + R_{P} + s \left(C_{M} R_{M} R_{P} + C_{P} R_{M} R_{P} + L_{P}\right)}$

Example 3) Operational amplifier with finite gain

Limited gain, single output, no frequency dependence

Non-inverting amplifiers like the one shown below can also be analyzed with Lcapy. image.png

VCVS (voltage control voltage signal source) is used as the operational amplifier. If you draw with LTSPICE, it will look like the figure below. image.png

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: $- \frac{A_{\mathrm{OL}} R_{F}}{A_{\mathrm{OL}} R_{G} + R_{F} R_{G} s \left(A_{\mathrm{OL}} C_{F} + C_{F} + C_{G}\right) + R_{F} + R_{G}}$

Limited gain, single output, single pole

If you want to analyze with an operational amplifier that has one pole, you can create a circuit diagram like the one shown below. image.png

The characteristics of the operational amplifier shall be expressed by the following equation. $A(f) = \frac{A_{OL}}{1+s/p}$ The operational amplifier is made by combining VCCS (voltage control current source), resistance, capacitance, and VCVS. The poles are determined by capacity and are $ C = {1} / p $. The capacity value can be entered in a formula, and the formula is entered inside {}. Therefore, the capacity value is set to {1 / p} on LTSPICE.

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: $- \frac{A_{\mathrm{OL}} R_{F} p}{A_{\mathrm{OL}} R_{G} p + R_{F} p + R_{G} p + s \left(R_{F} + R_{G}\right)}$

Limited gain, fully differential, no frequency dependence

A fully differential amplifier can be calculated with Lcapy using the circuit shown below. image.png

At the end

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

[Circuit x Python] How to find the transfer function of a circuit using Lcapy
[Circuit x Python] How to expand and calculate transfer functions using Lcapy
[Circuit x Python] How to enable the linear circuit analysis package Lcapy
[Python] A simple function to find the center coordinates of a circle
[Circuit x Python] How to solve circuit equations symbolically using sympy
How to find the scaling factor of a biorthogonal wavelet
How to unit test a function containing the current time using freezegun in python
How to determine the existence of a selenium element in Python
[Introduction to Python] How to split a character string with the split function
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
[Python3] Define a decorator to measure the execution time of a function
How to divide and process a data frame using the groupby function
[Python] Explains how to use the range function with a concrete example
How to find out the number of CPUs without using the sar command
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to create a wrapper that preserves the signature of the function to wrap
[Introduction to Python] How to write a character string with the format function
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
How to calculate the volatility of a brand
How to find the area of the Voronoi diagram
Get the caller of a function in Python
[Algorithm x Python] How to use the list
Find the geometric mean of n! Using Python
[2015/11/19] How to register a service locally using the python SDK on naoqi os
How to pass the execution result of a shell command in a list in Python
[python] How to sort by the Nth Mth element of a multidimensional array
[Python] Smasher tried to make the video loading process a function using a generator
How to find the coefficient of the trendline that passes through the vertices in Python
I made a script to record the active window using win32gui of Python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to write a GUI using the maya command
How to get the number of digits in Python
How to set up a Python environment using pyenv
[Introduction to Python] How to iterate with the range function?
Cut a part of the string using a Python slice
How to write a list / dictionary type of Python3
How to make a Python package using VS Code
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
[Introduction to Python] How to stop the loop using break?
How to execute a command using subprocess in Python
[Python] How to call a c function from python (ctypes)
Python Note: The mystery of assigning a variable to a variable
I tried to find out how to streamline the work flow with Excel x Python ②
I tried to find out how to streamline the work flow with Excel x Python ④
How to identify the element with the smallest number of characters in a Python list?
I tried to find out how to streamline the work flow with Excel x Python ⑤
How to call a function
I tried to find out how to streamline the work flow with Excel x Python ①
How to check in Python if one of the elements of a list is in another list
I tried to find out how to streamline the work flow with Excel x Python ③
How to find the first element that matches your criteria in a Python list
Add a function to tell the weather of today to slack bot (made by python)
I tried to find the entropy of the image with python
[Ubuntu] How to delete the entire contents of a directory
How to find the optimal number of clusters in k-means
Find out the apparent width of a string in python