我正在从事一个RF项目,工作流程如下:
sweep
start、stop、step每个frequency
Z0和频率相关的有效介电常数eef从params
H 218<代码>G 219)。目前,我已经在for-循环中使用列表/numpy-数组来实现它。这很有效,但当我想要链接多个ABCD时,这是很难看的,而且很费劲。问题是很难调试(在上述步骤之间失去临时结果)。
我认为一个解决办法可能是使用熊猫,但我很难实现以下需求:
f
individually
创建以频率为索引的数据格式(f列也可能工作)
展开列“动态”存储某些列的每个dtype=的结果,因为当前dtype=np.clongdouble是必需的(否则,在包含numpy-数组的后一个calculations)
one列( abcd-matrix)
)中,div是零的。)
我已经搜索过了,但是结果并没有阐明所需的概念和方法,比如
>>> import pandas as pd
>>> start = int(100E6)
>>> stop = int(1E9)
>>> step = int(1E6)
>>> df = pd.DataFrame(index=range(start,stop+step,step),columns=["z0","eef"])
>>> df.index
RangeIndex(start=100000000, stop=1001000000, step=1000000)
>>> 尝试访问df.index(1000)或df[1000]时引发错误。
发布于 2022-09-05 10:25:22
为了更好地证明这一点,我们可以创建一个函数来帮助计算给定的f_start、f_stop、f_step以及函数Z0和eef中的S-params。在这里,我将考虑其中一个已经具有函数Z0和eef。
让我们调用函数calc_s (注释使它变得不言自明)
import pandas as pd
def calc_s(f_start, f_stop, f_step, Z0, eef):
# Create a dataframe with the frequency column
df = pd.DataFrame({'f': pd.Series(range(f_start, f_stop, f_step))})
# Calculate Z0 and eef and add them to the dataframe
df['Z0'] = df['f'].apply(Z0)
df['eef'] = df['f'].apply(eef)
# Calculate the ABCD matrix for every frequency
df['A'] = 1
df['B'] = 2 * df['Z0'] * df['eef']**0.5
df['C'] = 1
df['D'] = -2 * df['Z0'] * df['eef']**0.5
# Calculate the scattering parameters S
df['S11'] = (df['A'] + df['B']) / (df['A'] - df['B'])
df['S12'] = 2 * df['C'] / (df['A'] - df['B'])
df['S21'] = 2 * df['C'] / (df['A'] - df['B'])
df['S22'] = (df['A'] - df['B']) / (df['A'] + df['B'])
# Calculate the magnitude of the scattering parameters
df['S11_mag'] = df['S11'].apply(lambda x: x.real**2 + x.imag**2)**0.5
df['S12_mag'] = df['S12'].apply(lambda x: x.real**2 + x.imag**2)**0.5
df['S21_mag'] = df['S21'].apply(lambda x: x.real**2 + x.imag**2)**0.5
df['S22_mag'] = df['S22'].apply(lambda x: x.real**2 + x.imag**2)**0.5
return df请注意,如果需要,一个cal总是调整计算,或者将其更改为您可能希望/需要的其他计算,不管是移除、添加、添加、.
现在,让我们用一些虚拟数据测试上面的函数
f_start = 1
f_stop = 100
f_step = 1
Z0 = lambda f: 50 # This is a dummy function that returns 50 for every f
eef = lambda f: 1 # This is a dummy function that returns 1 for every f
df = calc_s(f_start, f_stop, f_step, Z0, eef)
[Out]:
f Z0 eef A B ... S22 S11_mag S12_mag S21_mag S22_mag
0 1 50 1 1 100.0 ... -0.980198 1.020202 0.020202 0.020202 0.980198
1 2 50 1 1 100.0 ... -0.980198 1.020202 0.020202 0.020202 0.980198
2 3 50 1 1 100.0 ... -0.980198 1.020202 0.020202 0.020202 0.980198
3 4 50 1 1 100.0 ... -0.980198 1.020202 0.020202 0.020202 0.980198
4 5 50 1 1 100.0 ... -0.980198 1.020202 0.020202 0.020202 0.980198
[5 rows x 15 columns]https://stackoverflow.com/questions/73460494
复制相似问题