def makeTemplate(rawData, numOffsCorrIters=1, decayTime=50, nSigmaTrig=4.0, isVerbose=False, isPlot=False, sigPass=1): """ Make a matched filter template using a raw phase timestream INPUTS: rawData - noisy phase timestream with photon pulses numOffsCorrIters - number of pulse offset corrections to perform decayTime - approximate decay time of pulses (units: ticks) nSigmaTrig - threshold to detect pulse in terms of standard deviation of data isVerbose - print information about the template fitting process isPlot - plot information about Chi2 cut sigPass - std of data left after Chi2 cut OUTPUTS: finalTemplate - template of pulse shape time - use as x-axis when plotting template noiseSpectDict - dictionary containing noise spectrum and corresponding frequencies templateList - list of template itterations by correcting offsets peakIndices - list of peak indicies from rawData used for template """ # hipass filter data to remove any baseline data = hpFilter(rawData) # trigger on pulses in data peakDict = sigmaTrigger(data, nSigmaTrig=nSigmaTrig, decayTime=decayTime, isVerbose=isVerbose) # remove pulses with additional triggers in the pulse window peakIndices = cutPulsePileup(peakDict["peakMaxIndices"], decayTime=decayTime, isVerbose=isVerbose) # remove pulses with a large chi squared value peakIndices = cutChiSquared( data, peakIndices, sigPass=sigPass, decayTime=decayTime, isVerbose=isVerbose, isPlot=isPlot ) # Create rough template roughTemplate, time = averagePulses(data, peakIndices, decayTime=decayTime) # create noise spectrum from pre-pulse data for filter noiseSpectDict = mNS.makeWienerNoiseSpectrum(data, peakIndices, template=roughTemplate, isVerbose=isVerbose) # Correct for errors in peak offsets due to noise templateList = [roughTemplate] for i in range(numOffsCorrIters): peakIndices = correctPeakOffs(data, peakIndices, noiseSpectDict, roughTemplate, "wiener") # calculate a new corrected template roughTemplate, time = averagePulses(data, peakIndices, isoffset=True, decayTime=decayTime) templateList.append(roughTemplate) finalTemplate = roughTemplate return finalTemplate, time, noiseSpectDict, templateList, peakIndices
plt.show() #####Test energy resolution with real data. Assumes constant photon energy##### if False: isPlot=False isVerbose=False #extract raw data rawData = eRD.parseQDRPhaseSnap(os.path.join(os.getcwd(),'20140915/redLaser'),pixelNum=1,steps=30) rawTemplateData = rawData[0:1000000] rawTestData = rawData[1000000:2000000] #make template finalTemplate, time , noiseSpectrumDict, _ , tempPeakIndices = mT.makeTemplate(rawTemplateData,nSigmaTrig=4.,numOffsCorrIters=2,isVerbose=isVerbose,isPlot=isPlot) #fit to arbitrary pulse shape fittedTemplate, startFit, riseFit, fallFit = mT.makeFittedTemplate(finalTemplate,time,riseGuess=3.e-6,fallGuess=55.e-6) noiseSpectrumDictCovMat = mNS.makeWienerNoiseSpectrum(rawTemplateData, tempPeakIndices, 8000, 1000) #make matched filter matchedFilter=mF.makeMatchedFilter(finalTemplate, noiseSpectrumDictCovMat['noiseSpectrum'], nTaps=50, tempOffs=75) superMatchedFilter=mF.makeSuperMatchedFilter(finalTemplate, noiseSpectrumDictCovMat['noiseSpectrum'], fallFit, nTaps=50, tempOffs=75) #plot templates plt.plot(time,finalTemplate) plt.plot(time,fittedTemplate) plt.show() #filter data #data=mT.hpFilter(rawTestData) data = rawTestData #convolve with filter filteredData=np.convolve(data,matchedFilter,mode='same') superFilteredData=np.convolve(data,superMatchedFilter,mode='same')