import numpy as np
import matplotlib.pyplot as plt
import csv
import scienceplots
#Warning : As of version 2.0.0, you need to add import scienceplots before setting the style (plt.style.use('science')).
plt.style.use(['science', 'notebook'])
ユーザ関数
複素電圧などから絶対値,デジベル(dB),偏角(deg)の計算は,
def dbdeg_unwrap(z): # 複素係数(複素電圧や複素電界等)から絶対値,デジベル(dB),偏角(deg)の計算
q = 180.0/np.pi
amp = np.abs(z)
db = 20.0*np.log10(amp)
rad = np.angle(z)
# rad = np.unwrap(rad, discont=np.pi)
rad = np.unwrap(rad)
deg = rad*q
return amp,db,deg
基本行列による直列素子,並列素子の縦続接続は,
def ladder_f(izy, zzyy): # izy = 0(sereis) or 1(shunt) first element
n, n_s = zzyy.shape
a = np.ones(n_s)
d = np.ones(n_s)
b = np.zeros(n_s)
c = np.zeros(n_s)
for i in range(n):
zy = zzyy[i][:]
if np.mod(i,2) == izy: # sereis element
ai = 1.0*a
bi = a*zy+b
ci = 1.0*c
di = c*zy+d
else: # shunt element
ai = a+b*zy
bi = 1.0*b
ci = c+d*zy
di = 1.0*d
a = ai
b = bi
c = ci
d = di
return a, b, c, d
def element_values(gk, ip):
n2 = len(gk)
n = n2-2
gk_f = np.ones(n2)
gk_f2 = np.ones(n2)
gk_f[n+1] = 1.0*gk[n+1]
gk_f2[n+1] = 1.0*gk[n+1]
if ip == 1:
gk_f[1:n+1] = 1.0*gk[1:n+1] # Normalized element values for lowpass response
elif ip == 2:
gk_f[1:n+1] = 1.0/gk[1:n+1] # Normalized element values for highpass response
elif ip == 3:
gk_f[1:n+1] = gk[1:n+1]/rbw # Normalized element values for bandpass response
gk_f2[1:n+1] = 1.0/gk_f[1:n+1]
elif ip == 4:
gk_f[1:n+1] = 1.0/rbw/gk[1:n+1] # Normalized element values for bandstop response
gk_f2[1:n+1] = 1.0/gk_f[1:n+1]
print(f'{n:2d}',end=' ')
[print(f"{gk_f[j]:6.4f}",end=' ') for j in range(n+2)]
print('')
if ip >= 3:
print(f'{n:2d}',end=' ')
[print(f"{gk_f2[j]:6.4f}",end=' ') for j in range(n+2)]
print('')
return gk_f, gk_f2
計算値のフォーマット付出力のために,
# 可変長引数*args(複数の引数をタプルとして受け取る)
# 可変長引数**kwargs(複数の引数を辞書として受け取る)
def table_ss(x, *args, **kwargs):
nx = len(x)
ny = len(args)
for k in kwargs.values():
print(f'{k:>14s}',end='')
print('')
for i in range(nx):
print(f'{x[i]:14.3f}',end='')
for j in range(ny):
print(f'{args[j][i]:14.3f}',end='')
print('')
return
周波数特性の計算
特性関数として,0.5dBの等リプル特性を選択し,csvファイルから規格化素子値を読み込むと,
ic = 2 # 特性関数の選択
if ic == 1:
htype = 'maximally_flat'
hhtype = 'maximally flat'
csv_filename = 'element_values_maximally_flat.csv'
elif ic == 2:
htype = 'equal_ripple_0_5dB'
hhtype = 'equal ripple (0.5dB)'
csv_filename = 'element_values_equal_ripple_0_5dB.csv'
# csv_filename = 'element_values_equal_ripple_20dB.csv'
with open(csv_filename) as file: # ファイルのオープン
reader = csv.reader(file, quoting=csv.QUOTE_NONNUMERIC) # csvファイルからの読み込み
sst = [row for row in reader]
梯子型回路の段数を設定して,
n_ = 7 # csvファイルのデータの中から段数の指定して配列に代入
for i in range(len(sst)):
if len(sst[i])==n_+2:
gk = np.array(sst[i])
n = n_
n, gk