LoginSignup
3
5

PythonでわかるRCL回路の周波数応答

Last updated at Posted at 2024-03-31

直列RCL回路の周波数応答を調べるためにボード線図のプログラム描写を実装をしていく

image.png

ラプラス変換の補助方程式
image.png
伝達関数
image.png
Pythonコード

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# パラメータの設定
R = 1.0  # 抵抗(オーム)
L = 0.5  # コイル(ヘンリー)
C = 0.1  # コンデンサ(ファラド)

# 伝達関数の定義
num = [1]
den = [L*C, R*C, 1]
sys = signal.TransferFunction(num, den)

# ボード線図の描画
w, mag, phase = signal.bode(sys)
plt.figure()
plt.semilogx(w, mag)
plt.title('Bode Plot')
plt.xlabel('Frequency [rad/s]')
plt.ylabel('Magnitude [dB]')
plt.grid()

plt.figure()
plt.semilogx(w, phase)
plt.title('Bode Plot')
plt.xlabel('Frequency [rad/s]')
plt.ylabel('Phase [deg]')
plt.grid()

plt.show()

結果

image.png

image.png

RC積分回路の伝達関数=(1/(sC))/(R+(1/(sC)))
RC微分回路の伝達関数= (R))/(R+(1/(sC)))

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# RC circuit parameters
R = 1.0  # Resistance value
C = 1.0  # Capacitor capacitance (for both RC integral and differential circuits)

# Calculate cutoff frequencies
cutoff_freq_rc_integral = 1 / (2 * np.pi * R * C)
cutoff_freq_rc_differential = 1 / (2 * np.pi * R * C)

print("Cutoff frequency of RC Integral Circuit:", cutoff_freq_rc_integral)
print("Cutoff frequency of RC Differential Circuit:", cutoff_freq_rc_differential)

# Transfer function numerator and denominator coefficients for RC integral circuit
num_rc_integral = [1/(R*C)]
den_rc_integral = [1, 1/(R*C)]

# Transfer function numerator and denominator coefficients for RC differential circuit
num_rc_differential = [R, 0]
den_rc_differential = [R, 1/(R*C)]

# Create transfer functions
system_rc_integral = signal.TransferFunction(num_rc_integral, den_rc_integral)
system_rc_differential = signal.TransferFunction(num_rc_differential, den_rc_differential)

# Bode Plot calculation
w, mag_rc_integral, phase_rc_integral = signal.bode(system_rc_integral)
w, mag_rc_differential, phase_rc_differential = signal.bode(system_rc_differential)

# Plot
plt.figure()
plt.subplot(2, 1, 1)
plt.semilogx(w, mag_rc_integral, label='RC Integral Circuit')
plt.semilogx(w, mag_rc_differential, label='RC Differential Circuit')
plt.title('Bode Diagram')
plt.ylabel('Magnitude [dB]')
plt.legend()

plt.subplot(2, 1, 2)
plt.semilogx(w, phase_rc_integral, label='RC Integral Circuit')
plt.semilogx(w, phase_rc_differential, label='RC Differential Circuit')
plt.xlabel('Frequency [rad/s]')
plt.ylabel('Phase [deg]')
plt.legend()
plt.show()

結果
image.png

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# 伝達関数の定義
A = 2
numerator = [A]
denominator = [1, A]

sys = signal.TransferFunction(numerator, denominator)

# 周波数応答の計算
w, mag, phase = signal.bode(sys)

# プロット
plt.figure()
plt.subplot(2, 1, 1)
plt.semilogx(w, mag)  # デシベルスケールでプロット
plt.grid(True)
plt.title('Bode Plot - Magnitude')
plt.ylabel('Magnitude [dB]')

plt.subplot(2, 1, 2)
plt.semilogx(w, phase)  # デシベルスケールでプロット
plt.grid(True)
plt.title('Bode Plot - Phase')
plt.xlabel('Frequency [rad/s]')
plt.ylabel('Phase [degrees]')

plt.tight_layout()
plt.show()


print("Gain margin (dB):", 20 * np.log10(gm))
print("Phase margin (degrees):", pm)

結果
image.png

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5