I want to find two values of x
that intersect a certain value of y
on a x-y
plot of a resonance curve. However, as I have few data points I will need to interpolate to find these values of x
.
The curve I am looking at can be seen below:
How do I find the two values of x
that equal a y
value (shown in red)?
I have tried np.interpolate
by splitting the data into two arrays: first with gradient(y)>0
and another with gradient(y)<0
, however this yielded incorrect values. However, this approach is far from elegant and I seek a simple solution. Cheers in advance for any help.
Additional information: Code used thus far:
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()
Also, the data analysed in this program is found in this dpaste: http://dpaste.com/13AMJ92/
Prepare curve data:
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
split at peak, and use 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
the output:
4.99902583799 4.99954573393
You can also use 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))
the output:
[[ 4.99902584 -21.958 ]
[ 4.99954573 -21.958 ]]
If it's ok to use cubic splines, then you can use 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")
the output:
4.99891956723 4.99960633369
the plot:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With