gpt4 book ai didi

python - 为 x 的两个值插值一个峰值 - Python

转载 作者:太空狗 更新时间:2023-10-30 00:36:04 24 4
gpt4 key购买 nike

我想找到 x 的两个值与 y 的某个值相交在 x-y 上共振曲线图。但是,由于我的数据点很少,因此我需要进行插值以找到这些 x 的值。 .

我正在看的曲线可以在下面看到: enter image description here

如何找到 x 的两个值等于 y值(以红色显示)?

我试过了np.interpolate通过将数据分成两个数组:首先是 gradient(y)>0另一个是 gradient(y)<0 ,但是这产生了不正确的值。然而,这种方法远非优雅,我寻求一个简单的解决方案。提前为任何帮助欢呼。

附加信息:到目前为止使用的代码:

from numpy import *
import pylab as pl
import numpy as np
import scipy as scipy
from scipy import optimize

#Get data
fn = '4K_peak_hiresGhz.txt'
F_values, S_values, P_values = loadtxt(fn, unpack=True, usecols=[1, 2, 3])

#Select Frequency range of peak
lower = 4.995
upper = 5.002
F_values_2 = F_values[(F_values>lower) & (F_values<upper)]
S_values_2 = S_values[(F_values>lower) & (F_values<upper)]
P_values_2 = P_values[(F_values>lower) & (F_values<upper)]

#Calculate Q-value of selected peak
S_Peak = max(S_values_2)
print('S21 peak (dB):')
print(S_Peak)
print

F_Peak = F_values_2[S_values_2.argmax()]
print('Freq at S21 peak (GHz)')
print(F_Peak)
print

width_S = S_Peak - 3
print('S21 peak - 3dB (dB)')
print(width_S)
print

f, axarr = pl.subplots(2, sharex=False)
axarr[0].set_xlabel('Frequency (GHz)')
axarr[0].plot(F_values_2,S_values_2)
axarr[0].plot(F_values_2,S_values_2,'.')
axarr[0].set_ylabel('S21 (dB)')
axarr[0].axhline(y=width_S, linewidth='0.7', ls='dashed',color='red')
axarr[0].axhline(y=S_Peak, linewidth='1', ls='dashed', color='black')
axarr[0].axvline(x=F_Peak, linewidth='1', ls='dashed', color='black')
axarr[1].scatter(F_values_2, P_values_2)
axarr[1].set_ylabel('Phase (deg)')
axarr[1].set_xlabel('Frequency (GHz)')
pl.show()

此外,在这个程序中分析的数据可以在这个 dpaste 中找到:http://dpaste.com/13AMJ92/

最佳答案

准备曲线数据:

from numpy import *
import pylab as pl
import numpy as np
import scipy as scipy
from scipy import optimize

#Get data
fn = '13AMJ92.txt'
F_values, S_values, P_values = loadtxt(fn, unpack=True, usecols=[1, 2, 3])

#Select Frequency range of peak
lower = 4.995
upper = 5.002
F_values_2 = F_values[(F_values>lower) & (F_values<upper)]
S_values_2 = S_values[(F_values>lower) & (F_values<upper)]

S_Peak = max(S_values_2)
F_Peak = F_values_2[S_values_2.argmax()]
width_S = S_Peak - 3

在峰值处拆分,并使用 interp():

idx = S_values_2.argmax()
x1 = np.interp(width_S, S_values_2[:idx+1], F_values_2[:idx+1])
x2 = np.interp(width_S, S_values_2[-1:idx-1:-1], F_values_2[-1:idx-1:-1])
print x1, x2

输出:

4.99902583799 4.99954573393

您还可以使用 Shapely:

from shapely import geometry
curve = geometry.asLineString(np.c_[F_values_2, S_values_2])
hline = geometry.LineString([(F_values_2[0], width_S), (F_values_2[-1], width_S)])
print np.asarray(curve.intersection(hline))

输出:

[[  4.99902584 -21.958     ]
[ 4.99954573 -21.958 ]]

如果可以使用三次样条,则可以使用InterpolatedUnivariateSpline:

from scipy import interpolate
us = interpolate.InterpolatedUnivariateSpline(F_values_2, S_values_2 - width_S)
x1, x2 = us.roots()
print x1, x2
pl.plot(F_values_2, S_values_2 - width_S)
x = np.linspace(F_values_2[0], F_values_2[-1], 100)
pl.plot(x, us(x))
pl.axhline(0)
pl.plot([x1, x2], [0, 0], "o")

输出:

4.99891956723 4.99960633369

剧情:

enter image description here

关于python - 为 x 的两个值插值一个峰值 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com