www.tech-doc.cn Python

Python


import amepyplot
import scipy
import scipy.signal
from PySide2 import QtWidgets
import amesim

app = QtWidgets.QApplication.instance()
if app is None:
    app = QtWidgets.QApplication([])

plot = amepyplot.PlotWidget()

# 获取线性化时间点(如需)
times=amesim.amela('scrdemo2')

# 运行仿真
amesim.amerunsingle('scrdemo2')

# 选择要使用的jacfile索引(使用1表示显示第二个线性化时间点)
jacfileIndex = 0

[A, B, C, D, x, u, y, t, S] = amesim.ameloadj('scrdemo2', jacfileIndex)

wrange = 2*scipy.pi*scipy.logspace(-1,2,400)

# uindex是控制变量索引(注意索引从0开始)
uindex = 0
# yindex是观测变量索引(注意索引从0开始)
yindex = 0

# 使用scipy计算伯德图
sys_a = scipy.matrix(A)
sys_b = scipy.zeros((len(B[0]), 1))
for k in range(len(B[0])):
   sys_b[k, 0] = B[0][k][uindex]
sys_c = scipy.matrix(C[yindex])
sys_d = D[0][yindex][uindex]

system = scipy.signal.lti(sys_a, sys_b, sys_c, sys_d)
wout, y = scipy.signal.freqresp(system, [w for w in wrange if w])  # 剔除w=0的值
mag = abs(y)
phase = scipy.unwrap(scipy.arctan2(y.imag, y.real)) * 180.0 / scipy.pi

freq_range = wout / (2 * scipy.pi)
mag_dB = 20 * scipy.log10(mag)

# 在绘图窗口创建2个子图
plot.setRowCount(2)

# 创建伯德图(幅频特性)
x_item = amepyplot.Item(freq_range, '频率', 'Hz')
y_item = amepyplot.Item(mag_dB, '增益', 'dB')
ampl_curve = amepyplot.Curve2D(x_item, y_item, title='频率响应幅值')
ampl_graph = plot.getGraph(0, 0)
ampl_graph.addCurve(ampl_curve)
ampl_graph.xAxis().setLogScale(True)

# 创建伯德图(相频特性)
y_item = amepyplot.Item(phase, '相位', 'deg')
phase_curve = amepyplot.Curve2D(x_item, y_item, title='频率响应相位')
phase_graph = plot.getGraph(1, 0)
phase_graph.addCurve(phase_curve)
phase_graph.xAxis().setLogScale(True)

plot.setWindowTitle('t={}秒时的伯德图'.format(times[jacfileIndex]))
plot.resize(500, 400)
plot.show()

app.exec_()
  

图5-4. t=0s时的伯德图

图5-5. t=0.04s时的伯德图