print "usage: python pygaussfit.py bestprof_file prof_stdev" N = 128 DC = 600.0 noise_stdev = 8.0 params = [DC] params += [0.25, 0.1, 30.0] params += [0.3, 0.2, 15.0] params += [0.8, 0.05, 20.0] prof = normal(0.0, noise_stdev, N) + gen_gaussians(params, N) filenm = "test" else: filenm = sys.argv[1] prof = read_profile(filenm, normalize=0) if len(sys.argv)>=3: noise_stdev = float(sys.argv[2]) else: try: bprof = bestprof(sys.argv[1]) noise_stdev = bprof.prof_std except: noise_stdev = 1.0 fig = plt.figure() dataplot = fig.add_subplot(211) interactor = GaussianSelector(dataplot, prof, noise_stdev, filenm) plt.show()
gaussianwidth = float(a) except ValueError: gaussfitfile = a if o in ("-t", "--template"): templatefilenm = a if o in ("-o", "--output"): outfilenm = a if o in ("-s", "--sefd"): SEFD = float(a) print("Creating a summed profile of length %d bins using DM = %f" % (numbins, DM)) # Read the template profile or create an appropriate Gaussian if templatefilenm is not None: template = psr_utils.read_profile(templatefilenm) # Resample the template profile to have the correct number of bins (if required) if not len(template) == numbins: oldlen = len(template) template = sinc_interp.periodic_interp(template, numbins)[::oldlen] else: if gaussfitfile is not None: template = psr_utils.read_gaussfitfile(gaussfitfile, numbins) else: template = psr_utils.gaussian_profile(numbins, 0.0, gaussianwidth) # Normalize it template -= min(template) template /= max(template) # Rotate it so that it becomes a "true" template according to FFTFIT shift, eshift, snr, esnr, b, errb, ngood = measure_phase( template, template)
sumsubfreqs = (Num.arange(numsubbands)+0.5)*subpersumsub*fold_pfd.subdeltafreq + \ (fold_pfd.lofreq-0.5*fold_pfd.chan_wid) # Note: In the following, we cannot use fold_pfd.hifreqdelay since that # is based on the _barycentric_ high frequency (if the barycentric # conversion was available). For TOAs, we need a topocentric # delay, which is based on the topocentric frequency fold_pfd.hifreq sumsubdelays = (psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) - psr_utils.delay_from_DM(fold_pfd.bestdm, fold_pfd.hifreq))/SECPERDAY else: fold_pfd.subfreqs = Num.asarray([0.0]) sumsubfreqs = Num.asarray([0.0]) sumsubdelays = Num.asarray([0.0]) # Read the template profile if templatefilenm is not None: template = psr_utils.read_profile(templatefilenm, normalize=1) else: if (gaussfitfile): template = psr_utils.read_gaussfitfile(gaussfitfile, fold_pfd.proflen) else: template = psr_utils.gaussian_profile(fold_pfd.proflen, 0.0, gaussianwidth) template = template / max(template) #from Pgplot import * #plotxy(template) #closeplot() # Determine the Telescope used if (not fold.topo): obs = '@' # Solarsystem Barycenter else: try: obs = scopes[fold_pfd.telescope.split()[0]] except KeyError: print "Unknown telescope!!!"
def getErr(profile,templatefilenm): '''Based off of Pulsar Timing and Relativistic Gravity, J.H. Taylor, Phil. Trans. R. Soc. Lond. A 1992 341, 117-134 Appendix A Usage: give this a profile, a template, and a number of harmonics and it will calculate the offset and output an error''' #################################################################### ### Calculate sigscal ### #################################################################### k=int(len(profile)/2) template = psr_utils.read_profile(templatefilenm, normalize=0) template=template profile=profile a=1 offset=psr_utils.measure_phase_corr(profile-min(profile), template-min(template)) Pfft=FFT.rfft(profile-min(profile)) Tfft=FFT.rfft(template-min(template)) Pamp=[] Tamp=[] Pph=[] Tph=[] for i in range(0,k): Pamp.append(sqrt(real(Pfft[i]*conjugate(Pfft[i])))) Tamp.append(sqrt(real(Tfft[i]*conjugate(Tfft[i])))) Pph.append(arctan(1*imag(Pfft[i])/real(Pfft[i]))) Tph.append(arctan(1*imag(Tfft[i])/real(Tfft[i]))) b=0 total=0 denom=0 for i in range(1,k): b+=Pamp[i]*Tamp[i]*cos(Tph[i]-Pph[i]+i*offset*2*pi) total+=Tamp[i]*Tamp[i] denom+=i*i*Pamp[i]*Tamp[i]*cos(Tph[i]-Pph[i]+i*offset*2*pi) b=b/total sigscale=(abs(1/(2*b*denom))) ##################################################################### ### Done SigScale, start Poisson Variations ### ##################################################################### it=1000#do 'it' iterations variation=Num.zeros(len(profile)) Offsets=[] for i in range (0, it): for j in range (0,len(profile)): if profile[j]>=0: variation[j]=double(Num.random.poisson(profile[j],1)) if profile[j]<=0: variation[j]=0 Offsets.append(psr_utils.measure_phase_corr(variation-min(variation), template-min(template))) Offsets=np.array(Offsets) #This fixes the error being way too big is the shift was around 0 or 1 (The std # would then be averaging values near 0 and 1) k=0 for i in range (0,len(Offsets)): Offsets[i]= Offsets[i] - offset; if Offsets[i] < -0.5: Offsets[i] += 1.0 if Offsets[i] > 0.5: Offsets[i] -= 1.0 if abs(Offsets[i]) > 0.2: Offsets[i]=0 k=k+1 if k>=1: sys.stderr.write("Warning: May be Double peaked, attempting to correct:\nThere are "+str(k)+ " of "+ str(it) + " offsets > 0.2 phase away from the average offset .\n") sigma=std(Offsets) #print 'sigma = ',sigma return sigma
# later DM/timing correction. PBD 2011/11/03 sumsubfreqs_hi = sumsubfreqs + \ fold_pfd.subdeltafreq/2.0 - fold_pfd.chan_wid/2.0 subdelays2 = psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) - \ psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs_hi) else: fold_pfd.subfreqs = Num.asarray([0.0]) sumsubfreqs = Num.asarray([0.0]) sumsubdelays = Num.asarray([0.0]) subdelays2 = Num.asarray([0.0]) sumsubdelays_phs = Num.asarray([0.0]) # Read the template profile if templatefilenm is not None: template = psr_utils.read_profile(templatefilenm, normalize=1) else: if (gaussfitfile): template = psr_utils.read_gaussfitfile(gaussfitfile, fold_pfd.proflen) else: template = psr_utils.gaussian_profile(fold_pfd.proflen, 0.0, gaussianwidth) template = template / max(template) #from Pgplot import * #plotxy(template) #closeplot() # Determine the Telescope used if (not fold.topo): obs = '@' # Solarsystem Barycenter else:
in get_TOAs.py or sum_profiles.py with the '-g' parameter as a template.""" N = 128 DC = 600.0 noise_stdev = 8.0 params = [DC] params += [0.25, 0.1, 30.0] params += [0.3, 0.2, 15.0] params += [0.8, 0.05, 20.0] prof = normal(0.0, noise_stdev, N) + gen_gaussians(params, N) filenm = "test" else: filenm = sys.argv[1] prof = read_profile(filenm, normalize=0) if len(sys.argv)>=3: noise_stdev = float(sys.argv[2]) else: try: bprof = bestprof(sys.argv[1]) noise_stdev = bprof.prof_std except: noise_stdev = 1.0 fig = plt.figure() dataplot = fig.add_subplot(211) interactor = GaussianSelector(dataplot, prof, noise_stdev, filenm) plt.show()
try: gaussianwidth = float(a) except ValueError: gaussfitfile = a if o in ("-t", "--template"): templatefilenm = a if o in ("-o", "--output"): outfilenm = a if o in ("-s", "--sefd"): SEFD = float(a) print "Creating a summed profile of length %d bins using DM = %f"%(numbins, DM) # Read the template profile or create an appropriate Gaussian if templatefilenm is not None: template = psr_utils.read_profile(templatefilenm) # Resample the template profile to have the correct number of bins (if required) if not len(template)==numbins: oldlen = len(template) template = sinc_interp.periodic_interp(template, numbins)[::oldlen] else: if gaussfitfile is not None: template = psr_utils.read_gaussfitfile(gaussfitfile, numbins) else: template = psr_utils.gaussian_profile(numbins, 0.0, gaussianwidth) # Normalize it template -= min(template) template /= max(template) # Rotate it so that it becomes a "true" template according to FFTFIT shift,eshift,snr,esnr,b,errb,ngood = measure_phase(template, template) template = psr_utils.fft_rotate(template, shift)
ft1name = args[0] polyconame = args[1] poly = Polyco(polyconame, recalc_polycos=options.recalc_polycos) mjds, phases, met_converter, weights = process_ft1(ft1name, poly, options.emin, options.emax, write=options.addphase) if not options.unbinned: if not HAVE_PRESTO: raise ImportError, 'Presto tools not found; binned fitting not available!' # Read template profile if (options.template is not None): template = psr_utils.read_profile(options.template, normalize=1) else: if (options.gauss is not None): template = psr_utils.read_gaussfitfile(options.gauss, options.nbins) else: print >> sys.stderr, "WARNING: Using single gaussian template!" gaussianwidth = 0.1 # DEFAULT template = psr_utils.gaussian_profile(options.nbins, 0.0, gaussianwidth) template = template / template.max() else: if options.gauss is not None: template = LCTemplate(template=options.gauss) elif options.template is not None: template = LCTemplate(template=options.template)
def getErr(profile, templatefilenm): '''Based off of Pulsar Timing and Relativistic Gravity, J.H. Taylor, Phil. Trans. R. Soc. Lond. A 1992 341, 117-134 Appendix A Usage: give this a profile, a template, and a number of harmonics and it will calculate the offset and output an error''' #################################################################### ### Calculate sigscal ### #################################################################### k = int(len(profile) / 2) template = psr_utils.read_profile(templatefilenm, normalize=0) template = template profile = profile a = 1 offset = psr_utils.measure_phase_corr(profile - min(profile), template - min(template)) Pfft = FFT.rfft(profile - min(profile)) Tfft = FFT.rfft(template - min(template)) Pamp = [] Tamp = [] Pph = [] Tph = [] for i in range(0, k): Pamp.append(sqrt(real(Pfft[i] * conjugate(Pfft[i])))) Tamp.append(sqrt(real(Tfft[i] * conjugate(Tfft[i])))) Pph.append(arctan(1 * imag(Pfft[i]) / real(Pfft[i]))) Tph.append(arctan(1 * imag(Tfft[i]) / real(Tfft[i]))) b = 0 total = 0 denom = 0 for i in range(1, k): b += Pamp[i] * Tamp[i] * cos(Tph[i] - Pph[i] + i * offset * 2 * pi) total += Tamp[i] * Tamp[i] denom += i * i * Pamp[i] * Tamp[i] * cos(Tph[i] - Pph[i] + i * offset * 2 * pi) b = b / total sigscale = (abs(1 / (2 * b * denom))) ##################################################################### ### Done SigScale, start Poisson Variations ### ##################################################################### it = 1000 #do 'it' iterations variation = Num.zeros(len(profile)) Offsets = [] for i in range(0, it): for j in range(0, len(profile)): if profile[j] >= 0: variation[j] = double(Num.random.poisson(profile[j], 1)) if profile[j] <= 0: variation[j] = 0 Offsets.append( psr_utils.measure_phase_corr(variation - min(variation), template - min(template))) Offsets = np.array(Offsets) #This fixes the error being way too big is the shift was around 0 or 1 (The std # would then be averaging values near 0 and 1) k = 0 for i in range(0, len(Offsets)): Offsets[i] = Offsets[i] - offset if Offsets[i] < -0.5: Offsets[i] += 1.0 if Offsets[i] > 0.5: Offsets[i] -= 1.0 if abs(Offsets[i]) > 0.2: Offsets[i] = 0 k = k + 1 if k >= 1: sys.stderr.write( "Warning: May be Double peaked, attempting to correct:\nThere are " + str(k) + " of " + str(it) + " offsets > 0.2 phase away from the average offset .\n") sigma = std(Offsets) #print 'sigma = ',sigma return sigma