def _get_acf_peakheights(lags, acf, npeaks=20, searchinterval=1):
'''This calculates the relative peak heights for first npeaks in ACF.
Usually, the first peak or the second peak (if its peak height > first peak)
corresponds to the correct lag. When we know the correct lag, the period is
then:
bestperiod = time[lags == bestlag] - time[0]
'''
maxinds = argrelmax(acf, order=searchinterval)[0]
maxacfs = acf[maxinds]
maxlags = lags[maxinds]
mininds = argrelmin(acf, order=searchinterval)[0]
minacfs = acf[mininds]
minlags = lags[mininds]
relpeakheights = np.zeros(npeaks)
relpeaklags = np.zeros(npeaks,dtype=np.int64)
peakindices = np.zeros(npeaks,dtype=np.int64)
for peakind, mxi in enumerate(maxinds[:npeaks]):
# check if there are no mins to the left
# throw away this peak because it's probably spurious
# (FIXME: is this OK?)
if np.all(mxi < mininds):
continue
leftminind = mininds[mininds < mxi][-1] # the last index to the left
rightminind = mininds[mininds > mxi][0] # the first index to the right
relpeakheights[peakind] = (
acf[mxi] - (acf[leftminind] + acf[rightminind])/2.0
)
relpeaklags[peakind] = lags[mxi]
peakindices[peakind] = peakind
# figure out the bestperiod if possible
if relpeakheights[0] > relpeakheights[1]:
bestlag = relpeaklags[0]
bestpeakheight = relpeakheights[0]
bestpeakindex = peakindices[0]
else:
bestlag = relpeaklags[1]
bestpeakheight = relpeakheights[1]
bestpeakindex = peakindices[1]
return {'maxinds':maxinds,
'maxacfs':maxacfs,
'maxlags':maxlags,
'mininds':mininds,
'minacfs':minacfs,
'minlags':minlags,
'relpeakheights':relpeakheights,
'relpeaklags':relpeaklags,
'peakindices':peakindices,
'bestlag':bestlag,
'bestpeakheight':bestpeakheight,
'bestpeakindex':bestpeakindex}
评论列表
文章目录