import numpy as np
import matplotlib.pyplot as plt
import skrf as rf
import filtersynth2 as fs
#周波数の設定
fstart = 0.1
fstop = 20
points = 200
#モンテカルロシミュレーションでは中心周波数と比帯域幅を乱数で振るが、それの最大最小
f0min = 2
f0max = 18
dfrmax = 0.2
dfrmin = 0.1
#フィルタデータ1つ分作る関数
def filter_data(n, f0, df, fstart, fstop, points, type):
"""
Kerasで読めるような形でBPFの2ポートSパラメータとその合成に使うL,Cの値をそれぞれ
1つ分のdata, labelとして出力する。具体的にはdataは(1, points, 5), labelは(1, 6)。
dataが5なのは奇数次のLCフィルタはS11=S22、S21=S12かつ複素数データなので
freq, S11real, S11imag, S21real, S21imagだから。Sパラメータは絶対値1なので規格化はせず
周波数のみ最大周波数で割っている。
"""
label = np.zeros((1, 5))
if type == 0:
bpf_cir, bpf_network = fs.BPF_synthesis(n, f0, df, fstart, fstop, points, type = "Butterworth")
label[0, 0] = 1
elif type == 1:
bpf_cir, bpf_network = fs.BPF_synthesis(n, f0, df, fstart, fstop, points, type = "Chebyshev")
label[0, 1] = 1
elif type == 2:
bpf_cir, bpf_network = fs.BPF_synthesis(n, f0, df, fstart, fstop, points, type = "Chebyshev2")
label[0, 2] = 1
elif type == 3:
bpf_cir, bpf_network = fs.BPF_synthesis(n, f0, df, fstart, fstop, points, type = "Elliptic")
label[0, 3] = 1
elif type == 4:
bpf_cir, bpf_network = fs.BPF_synthesis(n, f0, df, fstart, fstop, points, type = "Bessel")
label[0, 4] = 1
freq = bpf_network.f
Spara = bpf_network.s
data = np.array([freq[:]/fstop*1E-9,Spara[:,0, 0].real, Spara[:,0, 0].imag,
Spara[:,1, 0].real, Spara[:,1, 0].imag]).T.reshape(1, Spara.shape[0],5)
return data, label
#モンテカルロシミュレーションでデータ作成して保存する。
N = 10000
rng = np.random.default_rng(1)
data = np.empty((0, points, 5))
label = np.empty((0, 5))
for i in range(N):
f0 = f0min + (f0max - f0min) * rng.random()
df = (dfrmin + (dfrmax - dfrmin) * rng.random()) * f0
type = rng.integers(0, 5)
x, y = filter_data(5, f0, df, fstart, fstop, points, type)
data = np.vstack((data, x))
label = np.vstack((label, y))
#保存
np.savez_compressed("filter_characteristics.npz", data=data, label=label)
最近のコメント