def echoloc(indata, histlen, startoffset=5.0): thehist, peakheight, peakloc, peakwidth, centerofmass = makehistogram( indata, histlen, refine=True ) thestore = np.zeros((2, len(thehist[0])), dtype="float64") thestore[0, :] = (thehist[1][1:] + thehist[1][0:-1]) / 2.0 thestore[1, :] = thehist[0][-histlen:] timestep = thestore[0, 1] - thestore[0, 0] startpt = np.argmax(thestore[1, :]) + int(startoffset // timestep) print("primary peak:", peakheight, peakloc, peakwidth) print("startpt, startloc, timestep:", startpt, thestore[1, startpt], timestep) while (thestore[1, startpt] > thestore[1, startpt + 1]) and (startpt < len(thehist[0]) - 2): startpt += 1 echopeakindex = np.argmax(thestore[1, startpt:-2]) + startpt echopeakloc = thestore[0, echopeakindex + 1] echopeakheight = thestore[1, echopeakindex + 1] numbins = 1 while (echopeakindex + numbins < histlen - 1) and ( thestore[1, echopeakindex + numbins] > echopeakheight / 2.0 ): numbins += 1 echopeakwidth = (thestore[0, echopeakindex + numbins] - thestore[0, echopeakindex]) * 2.0 echopeakheight, echopeakloc, echopeakwidth = tide_fit.gaussfit( echopeakheight, echopeakloc, echopeakwidth, thestore[0, :], thestore[1, :] ) return echopeakloc - peakloc, (echopeakheight * echopeakwidth) / (peakheight * peakwidth)
def autocorrcheck(corrscale, thexcorr, delta=0.1, acampthresh=0.1, aclagthresh=10.0, displayplots=False, prewindow=True, detrendorder=1, debug=False): """ Parameters ---------- corrscale thexcorr delta acampthresh aclagthresh displayplots prewindow detrendorder debug Returns ------- """ lookahead = 2 peaks = tide_fit.peakdetect(thexcorr, x_axis=corrscale, delta=delta, lookahead=lookahead) maxpeaks = np.asarray(peaks[0], dtype='float64') minpeaks = np.asarray(peaks[1], dtype='float64') if len(peaks[0]) > 0: if debug: print(peaks) zeropkindex = np.argmin(abs(maxpeaks[:, 0])) for i in range(zeropkindex + 1, maxpeaks.shape[0]): if maxpeaks[i, 0] > aclagthresh: return None, None if maxpeaks[i, 1] > acampthresh: sidelobetime = maxpeaks[i, 0] sidelobeindex = tide_util.valtoindex(corrscale, sidelobetime) sidelobeamp = thexcorr[sidelobeindex] numbins = 1 while (sidelobeindex + numbins < np.shape(corrscale)[0] - 1) and ( thexcorr[sidelobeindex + numbins] > sidelobeamp / 2.0): numbins += 1 sidelobewidth = (corrscale[sidelobeindex + numbins] - corrscale[sidelobeindex]) * 2.0 fitstart = sidelobeindex - numbins fitend = sidelobeindex + numbins sidelobeamp, sidelobetime, sidelobewidth = tide_fit.gaussfit(sidelobeamp, sidelobetime, sidelobewidth, corrscale[fitstart:fitend + 1], thexcorr[fitstart:fitend + 1]) if displayplots: pl.plot(corrscale[fitstart:fitend + 1], thexcorr[fitstart:fitend + 1], 'k', corrscale[fitstart:fitend + 1], tide_fit.gauss_eval(corrscale[fitstart:fitend + 1], [sidelobeamp, sidelobetime, sidelobewidth]), 'r') pl.show() return sidelobetime, sidelobeamp return None, None
def gethistprops(indata, histlen, refine=False, therange=None, pickleft=False, peakthresh=0.33): """ Parameters ---------- indata histlen refine therange pickleftpeak Returns ------- """ thestore = np.zeros((2, histlen), dtype="float64") if therange is None: thehist = np.histogram(indata, histlen) else: thehist = np.histogram(indata, histlen, therange) thestore[0, :] = thehist[1][-histlen:] thestore[1, :] = thehist[0][-histlen:] # get starting values for the peak, ignoring first and last point of histogram if pickleft: overallmax = np.max(thestore[1, 1:-2]) peakindex = 1 i = 1 started = False finished = False while i < len(thestore[1, :] - 2) and not finished: if thestore[1, i] > peakthresh * overallmax: started = True if thestore[1, i] > thestore[1, peakindex]: peakindex = i if started and (thestore[1, i] < 0.75 * thestore[1, peakindex]): finished = True i += 1 else: peakindex = np.argmax(thestore[1, 1:-2]) peaklag = thestore[0, peakindex + 1] peakheight = thestore[1, peakindex + 1] numbins = 1 while (peakindex + numbins < histlen - 1) and ( thestore[1, peakindex + numbins] > peakheight / 2.0 ): numbins += 1 peakwidth = (thestore[0, peakindex + numbins] - thestore[0, peakindex]) * 2.0 if refine: peakheight, peaklag, peakwidth = tide_fit.gaussfit( peakheight, peaklag, peakwidth, thestore[0, :], thestore[1, :] ) return peaklag, peakheight, peakwidth
def gethistprops(indata, histlen, refine=False, therange=None, pickleft=False): """ Parameters ---------- indata histlen refine therange pickleftpeak Returns ------- """ thestore = np.zeros((2, histlen), dtype='float64') if therange is None: thehist = np.histogram(indata, histlen) else: thehist = np.histogram(indata, histlen, therange) thestore[0, :] = thehist[1][-histlen:] thestore[1, :] = thehist[0][-histlen:] # get starting values for the peak, ignoring first and last point of histogram if pickleft: overallmax = np.max(thestore[1, 1:-2]) peakindex = 1 i = 1 started = False finished = False while i < len(thestore[1, :] - 2) and not finished: if thestore[1, i] > 0.5 * overallmax: started = True i += 1 if thestore[1, i] > thestore[1, peakindex]: peakindex = i if started and (thestore[1, i] < 0.75 * thestore[1, peakindex]): finished = True else: peakindex = np.argmax(thestore[1, 1:-2]) peaklag = thestore[0, peakindex + 1] peakheight = thestore[1, peakindex + 1] numbins = 1 while (peakindex + numbins < histlen - 1) and (thestore[1, peakindex + numbins] > peakheight / 2.0): numbins += 1 peakwidth = (thestore[0, peakindex + numbins] - thestore[0, peakindex]) * 2.0 if refine: peakheight, peaklag, peakwidth = tide_fit.gaussfit(peakheight, peaklag, peakwidth, thestore[0, :], thestore[1, :]) return peaklag, peakheight, peakwidth
def makeandsavehistogram(indata, histlen, endtrim, outname, binsize=None, displaytitle='histogram', displayplots=False, refine=False, therange=None): """ Parameters ---------- indata histlen endtrim outname displaytitle displayplots refine therange Returns ------- """ thehist = makehistogram(indata, histlen, binsize=binsize, therange=therange) thestore = np.zeros((2, len(thehist[0])), dtype='float64') thestore[0, :] = (thehist[1][1:] + thehist[1][0:-1]) / 2.0 thestore[1, :] = thehist[0][-histlen:] # get starting values for the peak, ignoring first and last point of histogram peakindex = np.argmax(thestore[1, 1:-2]) peaklag = thestore[0, peakindex + 1] peakheight = thestore[1, peakindex + 1] numbins = 1 while (peakindex + numbins < histlen - 1) and (thestore[1, peakindex + numbins] > peakheight / 2.0): numbins += 1 peakwidth = (thestore[0, peakindex + numbins] - thestore[0, peakindex]) * 2.0 if refine: peakheight, peaklag, peakwidth = tide_fit.gaussfit(peakheight, peaklag, peakwidth, thestore[0, :], thestore[1, :]) centerofmass = np.sum(thestore[0, :] * thestore[1, :]) / np.sum(thestore[1, :]) tide_io.writenpvecs(np.array([centerofmass]), outname + '_centerofmass.txt') tide_io.writenpvecs(np.array([peaklag]), outname + '_peak.txt') tide_io.writenpvecs(thestore, outname + '.txt') if displayplots: fig = pl.figure() ax = fig.add_subplot(111) ax.set_title(displaytitle) pl.plot(thestore[0, :(-1 - endtrim)], thestore[1, :(-1 - endtrim)])
def makehistogram(indata, histlen, binsize=None, therange=None, refine=False): """ Parameters ---------- indata histlen binsize therange Returns ------- """ if therange is None: therange = [indata.min(), indata.max()] if histlen is None and binsize is None: thebins = 10 elif binsize is not None: thebins = np.linspace( therange[0], therange[1], (therange[1] - therange[0]) / binsize + 1, endpoint=True, ) else: thebins = histlen thehist = np.histogram(indata, thebins, therange) thestore = np.zeros((2, len(thehist[0])), dtype="float64") thestore[0, :] = (thehist[1][1:] + thehist[1][0:-1]) / 2.0 thestore[1, :] = thehist[0][-histlen:] # get starting values for the peak, ignoring first and last point of histogram peakindex = np.argmax(thestore[1, 1:-2]) peakloc = thestore[0, peakindex + 1] peakheight = thestore[1, peakindex + 1] numbins = 1 while (peakindex + numbins < histlen - 1) and ( thestore[1, peakindex + numbins] > peakheight / 2.0 ): numbins += 1 peakwidth = (thestore[0, peakindex + numbins] - thestore[0, peakindex]) * 2.0 if refine: peakheight, peakloc, peakwidth = tide_fit.gaussfit( peakheight, peakloc, peakwidth, thestore[0, :], thestore[1, :] ) centerofmass = np.sum(thestore[0, :] * thestore[1, :]) / np.sum(thestore[1, :]) return thehist, peakheight, peakloc, peakwidth, centerofmass
def autocorrcheck(corrscale, thexcorr, delta=0.1, acampthresh=0.1, aclagthresh=10.0, displayplots=False, prewindow=True, detrendorder=1, debug=False): """ Parameters ---------- corrscale thexcorr delta acampthresh aclagthresh displayplots prewindow detrendorder debug Returns ------- """ lookahead = 2 peaks = tide_fit.peakdetect(thexcorr, x_axis=corrscale, delta=delta, lookahead=lookahead) maxpeaks = np.asarray(peaks[0], dtype='float64') minpeaks = np.asarray(peaks[1], dtype='float64') if len(peaks[0]) > 0: if debug: print(peaks) zeropkindex = np.argmin(abs(maxpeaks[:, 0])) for i in range(zeropkindex + 1, maxpeaks.shape[0]): if maxpeaks[i, 0] > aclagthresh: return None, None if maxpeaks[i, 1] > acampthresh: sidelobetime = maxpeaks[i, 0] sidelobeindex = tide_util.valtoindex(corrscale, sidelobetime) sidelobeamp = thexcorr[sidelobeindex] numbins = 1 while (sidelobeindex + numbins < np.shape(corrscale)[0] - 1) and (thexcorr[sidelobeindex + numbins] > sidelobeamp / 2.0): numbins += 1 sidelobewidth = (corrscale[sidelobeindex + numbins] - corrscale[sidelobeindex]) * 2.0 fitstart = sidelobeindex - numbins fitend = sidelobeindex + numbins sidelobeamp, sidelobetime, sidelobewidth = tide_fit.gaussfit( sidelobeamp, sidelobetime, sidelobewidth, corrscale[fitstart:fitend + 1], thexcorr[fitstart:fitend + 1]) if displayplots: pl.plot( corrscale[fitstart:fitend + 1], thexcorr[fitstart:fitend + 1], 'k', corrscale[fitstart:fitend + 1], tide_fit.gauss_eval( corrscale[fitstart:fitend + 1], [sidelobeamp, sidelobetime, sidelobewidth]), 'r') pl.show() return sidelobetime, sidelobeamp return None, None
def check_autocorrelation( corrscale, thexcorr, delta=0.1, acampthresh=0.1, aclagthresh=10.0, displayplots=False, detrendorder=1, ): """Check for autocorrelation in an array. Parameters ---------- corrscale thexcorr delta acampthresh aclagthresh displayplots windowfunc detrendorder Returns ------- sidelobetime sidelobeamp """ lookahead = 2 peaks = tide_fit.peakdetect(thexcorr, x_axis=corrscale, delta=delta, lookahead=lookahead) maxpeaks = np.asarray(peaks[0], dtype="float64") if len(peaks[0]) > 0: LGR.debug(peaks) zeropkindex = np.argmin(abs(maxpeaks[:, 0])) for i in range(zeropkindex + 1, maxpeaks.shape[0]): if maxpeaks[i, 0] > aclagthresh: return None, None if maxpeaks[i, 1] > acampthresh: sidelobetime = maxpeaks[i, 0] sidelobeindex = tide_util.valtoindex(corrscale, sidelobetime) sidelobeamp = thexcorr[sidelobeindex] numbins = 1 while (sidelobeindex + numbins < np.shape(corrscale)[0] - 1) and ( thexcorr[sidelobeindex + numbins] > sidelobeamp / 2.0 ): numbins += 1 sidelobewidth = ( corrscale[sidelobeindex + numbins] - corrscale[sidelobeindex] ) * 2.0 fitstart = sidelobeindex - numbins fitend = sidelobeindex + numbins sidelobeamp, sidelobetime, sidelobewidth = tide_fit.gaussfit( sidelobeamp, sidelobetime, sidelobewidth, corrscale[fitstart : fitend + 1], thexcorr[fitstart : fitend + 1], ) if displayplots: plt.plot( corrscale[fitstart : fitend + 1], thexcorr[fitstart : fitend + 1], "k", corrscale[fitstart : fitend + 1], tide_fit.gauss_eval( corrscale[fitstart : fitend + 1], [sidelobeamp, sidelobetime, sidelobewidth], ), "r", ) plt.show() return sidelobetime, sidelobeamp return None, None