我正在尝试编写一个脚本,获取潜在变星的光度数据,并尝试将理论模板拟合到该脚本中,以确定有关它的几个特征,如周期和振幅。
数据带有日期/时间(以mjd表示,本质上是自公元前4713年1月1日以来的天数)和亮度。
模板是一个由相位和y值组成的数组,当y从0到1再回到0时,相位从t=0到t=.998 (以.002为增量)。
我的模型函数将拉伸和移动模板,使其具有与数据的y值相同的峰间振幅和范围。我移动给定的t值,然后除以周期,得到时间的相位。我使用%1删除除小数值以外的所有内容,因为我不关心数据来自哪个周期,只关心它在周期中的位置。(ex 347.65 -> 0.65)
class tmpfitter:
def __init__ (self, templets)
self.n=0
self.tmps=templets
def model(self, t, period, t0, amplitude, yoffset):
# modify the template using peak-to-peak amplitude, yoffset
# shift times so phases line up, fold input times t by period
xtemp = self.tmps[self.n,:,0]
ytemp = self.tmps[self.n,:,1]*amplitude + yoffset
ph = (t - t0) / period % 1 #Folds data into single period
# interpolate the modified template at the phase we want
return interp1d(xtemp,ytemp)(ph)
def tmpfit(templets,data,pinit):
datfit = []
npars = []
fitter = tmpfitter(templets)
# Iterate through all templates, finding a best fit for each, find best fit of best fits at end.
for i in range(len(templets)):
fitter.n = i
pars, cov = curve_fit(fitter.model, data[:,0], data[:,1], sigma=data[:,2], p0=pinit, maxfev=10000)
datfit.append(median(abs(fitter.model(data[:,0],pars[0],pars[1],pars[2],pars[3])-data[:,1])))
npars.append(pars)
n = np.argmin(datfit)
return n, npars[n]这会正确地找到t0、振幅和yoffset的值,但不能找到周期。如果我在初始猜测(p0)中给出了正确的句号,那么就可以成功地将模板与数据匹配起来。但是,如果周期为off,则不会从最初的猜测更改周期。
我怀疑这是因为我折叠数据和砍掉整个数字部分的方式,但对curve_fit函数的了解不够,无法找到修复方法。我也尝试过使用scipy的least_squares函数(我知道curve_fit是它的包装器),但也不能让它工作。
什么是更好的方式来做这件事呢?
发布于 2021-02-20 03:48:44
This function会在一段时间内“折叠”你的时间序列。然后,您可以将生成的折叠时间曲线拟合为正弦曲线(或任何模型),并查看哪个周期提供最佳结果。
如果你不想随机地尝试不同的周期,你可以对数据进行fourier transform,并寻找给出最高功率的周期。不过,我认为这个链接函数需要均匀分布的数据...
当涉及造父变星时,Here很好地描述了时间序列是如何处理的。
https://stackoverflow.com/questions/65241880
复制相似问题