from sympy import *
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
import pandas as pd
import SymMNA
from IPython.display import display, Markdown, Math, Latex
init_printing()
25 2nd Order BRF
25.1 Introduction
This notebook walks through the Python code used to find the transfer function of the circuit shown in Figure 25.1.
25.2 Circuit description
The circuit above is a 2nd order band reject filter, problem 13-20 from Budak (1974). The schematic of the circuit is shown with each node annotated. The problem asks to obtain the transfer function, \(\frac {V_2} {V_1}\).
The net list for this circuit is:
O1 2 5 2
V1 1 0 1
C3 3 0 2
C1 4 1 1
C2 5 4 1
R1 2 4 0.5
R2 3 1 1
R3 5 3 1
The following Python modules are used in this notebook.
25.3 Load the net list
= '''
net_list O1 2 5 2
V1 1 0 1
C3 3 0 2
C1 4 1 1
C2 5 4 1
R1 2 4 0.5
R2 3 1 1
R3 5 3 1
'''
25.4 Call the symbolic modified nodal analysis function
= SymMNA.smna(net_list) report, network_df, i_unk_df, A, X, Z
25.4.1 Build the network equations
# Put matrices into SymPy
= Matrix(X)
X = Matrix(Z)
Z
= Eq(A*X,Z)
NE_sym
# display the equations
= ''
temp for i in range(shape(NE_sym.lhs)[0]):
+= '${:s} = {:s}$<br>'.format(latex(NE_sym.rhs[i]),latex(NE_sym.lhs[i]))
temp Markdown(temp)
\(0 = - C_{1} s v_{4} + I_{V1} + v_{1} \left(C_{1} s + \frac{1}{R_{2}}\right) - \frac{v_{3}}{R_{2}}\)
\(0 = I_{O1} + \frac{v_{2}}{R_{1}} - \frac{v_{4}}{R_{1}}\)
\(0 = v_{3} \left(C_{3} s + \frac{1}{R_{3}} + \frac{1}{R_{2}}\right) - \frac{v_{5}}{R_{3}} - \frac{v_{1}}{R_{2}}\)
\(0 = - C_{1} s v_{1} - C_{2} s v_{5} + v_{4} \left(C_{1} s + C_{2} s + \frac{1}{R_{1}}\right) - \frac{v_{2}}{R_{1}}\)
\(0 = - C_{2} s v_{4} + v_{5} \left(C_{2} s + \frac{1}{R_{3}}\right) - \frac{v_{3}}{R_{3}}\)
\(V_{1} = v_{1}\)
\(0 = v_{2} - v_{5}\)
Turn the free symbols into SymPy variables.
str(NE_sym.free_symbols).replace('{','').replace('}','')) var(
\(\displaystyle \left( C_{3}, \ v_{4}, \ R_{1}, \ V_{1}, \ v_{3}, \ s, \ R_{2}, \ v_{5}, \ C_{2}, \ I_{O1}, \ v_{2}, \ C_{1}, \ I_{V1}, \ v_{1}, \ R_{3}\right)\)
25.5 Symbolic solution
= solve(NE_sym,X) U_sym
Display the symbolic solution
= ''
temp for i in U_sym.keys():
+= '${:s} = {:s}$<br>'.format(latex(i),latex(U_sym[i]))
temp
Markdown(temp)
\(v_{1} = V_{1}\)
\(v_{2} = \frac{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} V_{1} s^{3} + C_{1} C_{2} R_{1} R_{2} V_{1} s^{2} + C_{1} C_{2} R_{1} R_{3} V_{1} s^{2} + C_{1} R_{1} V_{1} s + C_{2} R_{1} V_{1} s + V_{1}}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + C_{1} C_{2} R_{1} R_{2} s^{2} + C_{1} C_{2} R_{1} R_{3} s^{2} + C_{1} C_{3} R_{1} R_{2} s^{2} + C_{1} R_{1} s + C_{2} C_{3} R_{1} R_{2} s^{2} + C_{2} R_{1} s + C_{3} R_{2} s + 1}\)
\(v_{3} = \frac{C_{1} C_{2} R_{1} R_{2} V_{1} s^{2} + C_{1} C_{2} R_{1} R_{3} V_{1} s^{2} + C_{1} R_{1} V_{1} s + C_{2} R_{1} V_{1} s + V_{1}}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + C_{1} C_{2} R_{1} R_{2} s^{2} + C_{1} C_{2} R_{1} R_{3} s^{2} + C_{1} C_{3} R_{1} R_{2} s^{2} + C_{1} R_{1} s + C_{2} C_{3} R_{1} R_{2} s^{2} + C_{2} R_{1} s + C_{3} R_{2} s + 1}\)
\(v_{4} = \frac{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} V_{1} s^{3} + C_{1} C_{2} R_{1} R_{2} V_{1} s^{2} + C_{1} C_{2} R_{1} R_{3} V_{1} s^{2} + C_{1} C_{3} R_{1} R_{2} V_{1} s^{2} + C_{1} R_{1} V_{1} s + C_{2} R_{1} V_{1} s + V_{1}}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + C_{1} C_{2} R_{1} R_{2} s^{2} + C_{1} C_{2} R_{1} R_{3} s^{2} + C_{1} C_{3} R_{1} R_{2} s^{2} + C_{1} R_{1} s + C_{2} C_{3} R_{1} R_{2} s^{2} + C_{2} R_{1} s + C_{3} R_{2} s + 1}\)
\(v_{5} = \frac{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} V_{1} s^{3} + C_{1} C_{2} R_{1} R_{2} V_{1} s^{2} + C_{1} C_{2} R_{1} R_{3} V_{1} s^{2} + C_{1} R_{1} V_{1} s + C_{2} R_{1} V_{1} s + V_{1}}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + C_{1} C_{2} R_{1} R_{2} s^{2} + C_{1} C_{2} R_{1} R_{3} s^{2} + C_{1} C_{3} R_{1} R_{2} s^{2} + C_{1} R_{1} s + C_{2} C_{3} R_{1} R_{2} s^{2} + C_{2} R_{1} s + C_{3} R_{2} s + 1}\)
\(I_{V1} = \frac{- C_{1} C_{2} C_{3} R_{1} R_{2} V_{1} s^{3} - C_{1} C_{2} C_{3} R_{1} R_{3} V_{1} s^{3} - C_{1} C_{3} R_{1} V_{1} s^{2} - C_{1} C_{3} R_{2} V_{1} s^{2} - C_{2} C_{3} R_{1} V_{1} s^{2} - C_{3} V_{1} s}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + C_{1} C_{2} R_{1} R_{2} s^{2} + C_{1} C_{2} R_{1} R_{3} s^{2} + C_{1} C_{3} R_{1} R_{2} s^{2} + C_{1} R_{1} s + C_{2} C_{3} R_{1} R_{2} s^{2} + C_{2} R_{1} s + C_{3} R_{2} s + 1}\)
\(I_{O1} = \frac{C_{1} C_{3} R_{2} V_{1} s^{2}}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + C_{1} C_{2} R_{1} R_{2} s^{2} + C_{1} C_{2} R_{1} R_{3} s^{2} + C_{1} C_{3} R_{1} R_{2} s^{2} + C_{1} R_{1} s + C_{2} C_{3} R_{1} R_{2} s^{2} + C_{2} R_{1} s + C_{3} R_{2} s + 1}\)
Symbolic form of the transfer function.
= (U_sym[v2]/U_sym[v1]).simplify().collect(s)
H_sym H_sym
\(\displaystyle \frac{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + s^{2} \left(C_{1} C_{2} R_{1} R_{2} + C_{1} C_{2} R_{1} R_{3}\right) + s \left(C_{1} R_{1} + C_{2} R_{1}\right) + 1}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3} s^{3} + s^{2} \left(C_{1} C_{2} R_{1} R_{2} + C_{1} C_{2} R_{1} R_{3} + C_{1} C_{3} R_{1} R_{2} + C_{2} C_{3} R_{1} R_{2}\right) + s \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) + 1}\)
The numerator and denominator of the transfer function can be obtained as follows:
= fraction(H_sym) num, den
The roots of the numerator can be obtained symbolically. Since the degree of the polynominal is a third order, SymPy can solve for the roots and obtain a solution very quickly. The roots expressed in symbolic form are not very useful, but are easily obtained, something that would be almost impossible to do by hand with pencil and paper.
= solve(num,s)
z 0] z[
\(\displaystyle - \frac{\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}}{3 \sqrt[3]{\frac{\sqrt{- 4 \left(\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}\right)^{3} + \left(\frac{2 \left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{2}}}{2} + \frac{\left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{2 C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}} - \frac{\sqrt[3]{\frac{\sqrt{- 4 \left(\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}\right)^{3} + \left(\frac{2 \left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{2}}}{2} + \frac{\left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{2 C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}}{3} - \frac{R_{2} + R_{3}}{3 C_{3} R_{2} R_{3}}\)
1] z[
\(\displaystyle - \frac{\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}}{3 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}\right)^{3} + \left(\frac{2 \left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{2}}}{2} + \frac{\left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{2 C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}} - \frac{\left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}\right)^{3} + \left(\frac{2 \left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{2}}}{2} + \frac{\left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{2 C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}}{3} - \frac{R_{2} + R_{3}}{3 C_{3} R_{2} R_{3}}\)
2] z[
\(\displaystyle - \frac{\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}}{3 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}\right)^{3} + \left(\frac{2 \left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{2}}}{2} + \frac{\left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{2 C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}} - \frac{\left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(\frac{\left(R_{2} + R_{3}\right)^{2}}{C_{3}^{2} R_{2}^{2} R_{3}^{2}} - \frac{3 \left(C_{1} + C_{2}\right)}{C_{1} C_{2} C_{3} R_{2} R_{3}}\right)^{3} + \left(\frac{2 \left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{2}}}{2} + \frac{\left(R_{2} + R_{3}\right)^{3}}{C_{3}^{3} R_{2}^{3} R_{3}^{3}} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} + C_{2}\right) \left(R_{2} + R_{3}\right)}{2 C_{1} C_{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}}{3} - \frac{R_{2} + R_{3}}{3 C_{3} R_{2} R_{3}}\)
The roots of the denominator can be obtained symbolically.
= solve(den,s)
p 0] p[
\(\displaystyle - \frac{- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}{3 \sqrt[3]{\frac{\sqrt{- 4 \left(- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{3} + \left(\frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{2 \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}\right)^{2}}}{2} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{2 C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}}} - \frac{\sqrt[3]{\frac{\sqrt{- 4 \left(- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{3} + \left(\frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{2 \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}\right)^{2}}}{2} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{2 C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}}}{3} - \frac{C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}}{3 C_{1} C_{2} C_{3} R_{2} R_{3}}\)
1] p[
\(\displaystyle - \frac{- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}{3 \left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{3} + \left(\frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{2 \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}\right)^{2}}}{2} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{2 C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}}} - \frac{\left(- \frac{1}{2} - \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{3} + \left(\frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{2 \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}\right)^{2}}}{2} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{2 C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}}}{3} - \frac{C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}}{3 C_{1} C_{2} C_{3} R_{2} R_{3}}\)
2] p[
\(\displaystyle - \frac{- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}}{3 \left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{3} + \left(\frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{2 \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}\right)^{2}}}{2} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{2 C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}}} - \frac{\left(- \frac{1}{2} + \frac{\sqrt{3} i}{2}\right) \sqrt[3]{\frac{\sqrt{- 4 \left(- \frac{3 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right)}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{2}}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{2}^{2} R_{3}^{2}}\right)^{3} + \left(\frac{27}{C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{2 \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}\right)^{2}}}{2} + \frac{27}{2 C_{1} C_{2} C_{3} R_{1} R_{2} R_{3}} - \frac{9 \left(C_{1} R_{1} + C_{2} R_{1} + C_{3} R_{2}\right) \left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)}{2 C_{1}^{2} C_{2}^{2} C_{3}^{2} R_{1} R_{2}^{2} R_{3}^{2}} + \frac{\left(C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}\right)^{3}}{C_{1}^{3} C_{2}^{3} C_{3}^{3} R_{2}^{3} R_{3}^{3}}}}{3} - \frac{C_{1} C_{2} R_{2} + C_{1} C_{2} R_{3} + C_{1} C_{3} R_{2} + C_{2} C_{3} R_{2}}{3 C_{1} C_{2} C_{3} R_{2} R_{3}}\)
25.6 Numerical solution
Construct a dictionary of element values.
= SymMNA.get_part_values(network_df)
element_values
# display the component values
for k,v in element_values.items():
print('{:s} = {:s}'.format(str(k), str(v)))
V1 = 1.0
O1 = nan
C3 = 2.0
C1 = 1.0
C2 = 1.0
R1 = 0.5
R2 = 1.0
R3 = 1.0
Put the component values into the equations.
= NE_sym.subs(element_values) NE
Display the equations with component values.
= ''
temp for i in range(shape(NE.lhs)[0]):
+= '${:s} = {:s}$<br>'.format(latex(NE.rhs[i]),latex(NE.lhs[i]))
temp
Markdown(temp)
\(0 = I_{V1} - 1.0 s v_{4} + v_{1} \cdot \left(1.0 s + 1.0\right) - 1.0 v_{3}\)
\(0 = I_{O1} + 2.0 v_{2} - 2.0 v_{4}\)
\(0 = - 1.0 v_{1} + v_{3} \cdot \left(2.0 s + 2.0\right) - 1.0 v_{5}\)
\(0 = - 1.0 s v_{1} - 1.0 s v_{5} - 2.0 v_{2} + v_{4} \cdot \left(2.0 s + 2.0\right)\)
\(0 = - 1.0 s v_{4} - 1.0 v_{3} + v_{5} \cdot \left(1.0 s + 1.0\right)\)
\(1.0 = v_{1}\)
\(0 = v_{2} - v_{5}\)
Solve for voltages and currents and display the results.
= solve(NE,X)
U
= ''
temp for i in U.keys():
+= '${:s} = {:s}$<br>'.format(latex(i),latex(U[i]))
temp
Markdown(temp)
\(v_{1} = 1.0\)
\(v_{2} = \frac{s^{2} + 1.0}{s^{2} + 2.0 s + 1.0}\)
\(v_{3} = \frac{s^{2} + s + 1.0}{s^{3} + 3.0 s^{2} + 3.0 s + 1.0}\)
\(v_{4} = \frac{s^{3} + 2.0 s^{2} + s + 1.0}{s^{3} + 3.0 s^{2} + 3.0 s + 1.0}\)
\(v_{5} = \frac{s^{2} + 1.0}{s^{2} + 2.0 s + 1.0}\)
\(I_{V1} = - \frac{2.0 s}{s + 1.0}\)
\(I_{O1} = \frac{2.0 s^{2}}{s^{3} + 3.0 s^{2} + 3.0 s + 1.0}\)
25.6.1 Plot the frequency response of the transfer function
= U[v2]/U[v1]
H H
\(\displaystyle \frac{1.0 \left(s^{2} + 1.0\right)}{s^{2} + 2.0 s + 1.0}\)
= fraction(H) #returns numerator and denominator
num, denom
# convert symbolic to numpy polynomial
= np.array(Poly(num, s).all_coeffs(), dtype=float)
a = np.array(Poly(denom, s).all_coeffs(), dtype=float)
b = (a, b) system
#x = np.linspace(0.01*2*np.pi, 1*2*np.pi, 2000, endpoint=True)
= np.logspace(-2, 0, 2000, endpoint=False)*2*np.pi
x = signal.bode(system, w=x) # returns: rad/s, mag in dB, phase in deg w, mag, phase
Load the csv file of node 10 voltage over the sweep range and plot along with the results obtained from SymPy.
Plot the results.
= plt.subplots()
fig, ax1 'magnitude, dB')
ax1.set_ylabel('frequency, Hz')
ax1.set_xlabel(
/(2*np.pi), mag,'-b') # Bode magnitude plot
plt.semilogx(w
='y')
ax1.tick_params(axis#ax1.set_ylim((-30,20))
plt.grid()
# instantiate a second y-axes that shares the same x-axis
= ax1.twinx()
ax2 = 'tab:blue'
color
/(2*np.pi), phase,':',color='tab:red') # Bode phase plot
plt.semilogx(w
'phase, deg',color=color)
ax2.set_ylabel(='y', labelcolor=color)
ax2.tick_params(axis#ax2.set_ylim((-5,25))
'Magnitude and phase response')
plt.title( plt.show()
print('null: {:.2f} dB at {:.3f} Hz'.format(mag.min(),w[np.argmin(mag)]/(2*np.pi)))
null: -67.66 dB at 0.159 Hz
25.6.2 Poles and zeros of the transfer function
The poles and zeros of the transfer function can easly be obtained with the following code:
= fraction(H) #returns numerator and denominator
H_num, H_denom
# convert symbolic to numpy polynomial
= np.array(Poly(H_num, s).all_coeffs(), dtype=float)
a = np.array(Poly(H_denom, s).all_coeffs(), dtype=float)
b = signal.TransferFunction(a,b) sys
= np.roots(sys.num)
sys_zeros = np.roots(sys.den) sys_poles
25.6.2.1 Low pass filter pole zero plot
The poles and zeros of the preamp transfer function are plotted.
'ob', markerfacecolor='none')
plt.plot(np.real(sys_zeros), np.imag(sys_zeros), 'xr')
plt.plot(np.real(sys_poles), np.imag(sys_poles), 'Zeros', 'Poles'], loc=0)
plt.legend(['Pole / Zero Plot')
plt.title('real part, \u03B1')
plt.xlabel('imaginary part, j\u03C9')
plt.ylabel(
plt.grid() plt.show()
Poles and zeros of the transfer function plotted on the complex plane. The units are in radian frequency.
Printing these values in Hz.
print('number of zeros: {:d}'.format(len(sys_zeros)))
for i in sys_zeros:
print('{:,.2f} Hz'.format(i/(2*np.pi)))
number of zeros: 2
0.00+0.16j Hz
0.00-0.16j Hz
print('number of poles: {:d}'.format(len(sys_poles)))
for i in sys_poles:
print('{:,.2f} Hz'.format(i/(2*np.pi)))
number of poles: 2
-0.16+0.00j Hz
-0.16-0.00j Hz
25.6.3 Impulse and step response
Use the SciPy functions impulse2 and step2 to plot the impulse and step response of the system.
1,2,figsize=(15, 5))
plt.subplots(
# using subplot function and creating
# plot one
1, 2, 1)
plt.subplot(
# impulse response
= signal.impulse2(sys,N=500)
t, y /1e-3, y)
plt.plot(t'Impulse response')
plt.title('volts')
plt.ylabel('time, msec')
plt.xlabel(
plt.grid()
# using subplot function and creating plot two
1, 2, 2)
plt.subplot(
= signal.step2(sys,N=500)
t, y /1e-3, y)
plt.plot(t'Step response')
plt.title('volts')
plt.ylabel('time, msec')
plt.xlabel(
plt.grid()
# show plot
plt.show()