[Circuit x Python] How to expand and calculate transfer functions using Lcapy

Introduction

I wrote an article about how to symbolically analyze the transfer function of a circuit using Lcapy, a linear circuit analysis package in Python.

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

After finding the transfer function, we want to expand it and perform numerical analysis. These methods are explained in this article.

environment

Python: 3.7.4、SymPy: 1.6.2、Lcapy: 0.67.0

Circuit to analyze

Consider an inverting amplifier that feeds back an operational amplifier with resistors RG and RF. The characteristics of the operational amplifier shall be expressed by the following equation. $A(f) = \frac{A_{OL}}{1+s/p}$ image.png

This circuit also appeared in Previous article, and is drawn as follows on LTSPICE. image.png

The code to find the transfer function and the execution result are as follows.

python


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)}$

1. 1. Expand the transfer function

Instructions on how to expand the transfer function can be found in the official documentation Expressions => Methods (http://lcapy.elec.canterbury.ac.nz/expressions.html).

The example used in the above circuit is shown below. image.png

In this way, you can easily expand and transform expressions with very little code.

2. Numerical calculation

After symbolically analyzing the circuit to find the transfer function, let's assign a value to the transfer function and perform numerical analysis.

First, find the transfer function. (The code has been partially modified from the one listed above. See below for the reason)

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 step
G1 0 N002 0 INN AOL
""")

H = (cct["OUT"].V(s) / cct.V1.V(s)).simplify()
H

Execution result: $- \frac{A_{\mathrm{OL}} R_{F} p}{p \left(A_{\mathrm{OL}} R_{G} + R_{F} + R_{G}\right) + s \left(R_{F} + R_{G}\right)}$

The following code assigns a value to the obtained transfer function. Input values: AOL = 1000, p = 2π x 10kHz, RG = 1kΩ, RF = 2kΩ

H = H.subs("AOL", 1000).subs("p", 2*pi*1e4).subs("RG", 1000).subs("RF", 2000)
H

Execution result: $- \frac{40000000000.0 \pi}{3000 s + 20060000000.0 \pi}$

The characteristics of the transfer function (intensity, unit dB here) can be calculated and graphed with the following code.


import numpy as np
import matplotlib.pyplot as plt

freq = np.logspace(start=5, stop=9, num=(9-5)*21)
sm1 = H(f).dB.evaluate(freq)

fig, ax = plt.subplots()
ax.plot(freq,sm1)

ax.set_xscale("log")
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel("Signal Magnitude (dB)")

plt.show()

Execution result: image.png

The following code is numerically calculated to generate an array sm1 = H(f).dB.evaluate(freq)

The following processing is performed here. Substitute 2πjf for s → Calculate the decibel of this → Numerical calculation with evaluate (freq)

image.png

bug? (2020/10/31)

An error will occur if you perform a numerical calculation from the code described in "Circuit to analyze".

python


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 = H.subs("AOL", 1000).subs("p", 2*pi*1e4).subs("RG", 1000).subs("RF", 2000)

import numpy as np
import matplotlib.pyplot as plt

freq = np.logspace(start=5, stop=9, num=(9-5)*21)
sm1 = H(f).dB.evaluate(freq)

fig, ax = plt.subplots()
ax.plot(freq,sm1)

ax.set_xscale("log")
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel("Signal Magnitude (dB)")

plt.show()
""")

Execution result: ValueError: Cannot convert non-causal s-expression to f domain

I don't think it's strange in code, but I get an error. The same transfer function can be obtained with the code of "2. Numerical calculation", but it can be calculated for some reason. In my environment, the code described in the official document also causes an error, so I think it is a bug. If the situation changes, I will correct this part.

Recommended Posts

[Circuit x Python] How to expand and calculate transfer functions using Lcapy
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
[Circuit x Python] How to solve circuit equations symbolically using sympy
[Python] How to calculate MAE and RMSE
[Circuit x Python] How to enable the linear circuit analysis package Lcapy
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
How to install python using anaconda
How to calculate date with python
How to get followers and followers from python using the Mastodon API
How to use functions in separate files Perl and Python versions
How to package and distribute Python scripts
From Python to using MeCab (and CaboCha)
How to install and use pandas_datareader [Python]
[Control engineering] Graphing transfer functions using Python
How to erase Python 2.x on Mac.
python: How to use locals () and globals ()
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
How to use is and == in Python
[Blender x Python] How to use modifiers
How to generate permutations in Python and C ++
How to create explanatory variables and objective functions
Python datetime How to calculate dates and convert strings strftime, strptime [Definitive Edition]
[Python] How to read data from CIFAR-10 and CIFAR-100
How to convert SVG to PDF and PNG [Python]
[Algorithm x Python] How to use the list
[Blender x Python] How to make an animation
[Python] How to use hash function and tuple.
[Blender x Python] How to make vertex animation
How to plot autocorrelation and partial autocorrelation in python
[Python] [Django] How to use ChoiceField and how to add options
How to set up a Python environment using pyenv
[Blender x Python] How to create an original object
How to auto-submit Microsoft Forms using python (Mac version)
[Python] How to expand variables in a character string
How to make a Python package using VS Code
[Python] How to sort dict in list and instance in list
[Python] How to split and modularize files (simple, example)
[Python] How to create Correlation Matrix and Heat Map
How to exit when using Python in Terminal (Mac)
How to retrieve multiple arrays using slice in python.
[Introduction to Python] How to stop the loop using break?
Python # How to check type and type for super beginners
How to execute a command using subprocess in Python
[Introduction to Python] How to write repetitive statements using for statements
How to install Python
How to install python
How to parse Java source code with AST (Abstract Syntax Tree) using ANTLR and Python
Try to make it using GUI and PyQt in Python
[Introduction to Python] How to write conditional branches using if statements
How to swap elements in an array in Python, and how to reverse an array.
[python] Summary of how to retrieve lists and dictionary elements
[Python] How to play with class variables with decorator and metaclass
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to transpose a 2D array using only python [Note]
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Python] How to set variable names dynamically and speed comparison
How to learn TensorFlow for liberal arts and Python beginners
How to add new data (lines and plots) using matplotlib
Build a Python environment and transfer data to the server