# import matplotlib as mpl # mpl.use("pgf") # pgf_with_pdflatex = { # "pgf.texsystem": "pdflatex", # "pgf.preamble": [ # r"\usepackage{amsmath}", # r"\usepackage{amssymb}", # r"\usepackage{timet}", # ] # } # #r"\usepackage[utf8x]{inputenc}", # #r"\usepackage[T1]{fontenc}", # #r"\usepackage{cmbright}", # mpl.rcParams.update(pgf_with_pdflatex) # # from matplotlib import rc # rc('font',**{'family':'serif','serif':['times']}) # ## for Palatino and other serif fonts use: # #rc('font',**{'family':'serif','serif':['Palatino']}) # #rc('text', usetex=True) #import matplotlib #from matplotlib import rc #matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{timet,amsmath}"] #rc('font',**{'family':'serif','serif':['timet']}) #rc('text', usetex=True) from GJIPlot import * import numpy as np import matplotlib.pyplot as plt from smooth import smooth #from invertColours import * from decay import * from scipy import signal from notch import * import rotate # why is it called loss? BLACK = False labs = 16 if BLACK: labcol = 'white' else: labcol = 'black' def Kw(a, phi2, s, rT2, wL): return a*((np.sin(phi2)*s + ((1/rT2)*np.sin(phi2)) + wL*np.cos(phi2) ) / (wL**2+(s+1/rT2)**2 )) def quadDetect(T, vL, wL, dt, xn, DT): # decimate # blind decimation # 1 instead of T irsamp = (T) * int( (1./vL) / dt) # real iisamp = int( ((1./vL)/ dt) * ( .5*np.pi / (2.*np.pi) ) ) # imaginary dsamp = int( DT / dt) # real iisamp += dsamp ############################################################ # simple quadrature-detection via sampling xr = xn[dsamp::irsamp] xi = xn[iisamp::irsamp] phase = np.angle( xr + 1j*xi ) abse = np.abs( xr + 1j*xi ) # times ta = np.arange(0, TT, dt) te = np.arange(DT, TT, TT/len(abse)) ############################################################# # hilbert transform ht = signal.hilbert(xn) #, 100)) he = np.abs(ht) #, 100)) he = ht*np.exp( -1j*wL*t ) # ht*e^{-jwLt} # phase envelope #hp = ((np.angle(ht[dsamp::irsamp]))) #[Q, I, tt] = rotate.quadrature(T, vL, wL, dt, xn, DT, t) [E0,df,phi,T2] = quadratureDetect(np.imag(he), np.real(he), t) D = rotate.RotateAmplitude(np.real(he), np.imag(he), phi, df, t) he = D.imag #print ("df", df, "phi", phi) """ plt.plot(xn) plt.plot(np.real(he)) plt.plot(np.imag(he)) plt.plot(D.imag) plt.plot(D.real) print ("phi", phi) plt.plot( np.unwrap(np.arctan(np.unwrap(np.angle( he ) + np.pi/2 ))) ) # plt.plot(np.imag(ht)) # plt.plot(xn) # plot FFT XN = np.fft.rfft(xn) plt.figure() plt.plot(XN.real, label='orig') #XNN = XN * np.exp(1j*phi-1.) """ """ phi = np.arange(-np.pi, np.pi, .001) best = 1e12 bestPhi = 0 for p in phi: XNN = XN * np.exp(1j*p) if ( (np.linalg.norm( XNN.real[XNN.real < 0] )) < best): best = np.linalg.norm( XNN.real[XNN.real < 0]) bestPhi = p #print (len( XNN.real[XNN.real < 0] )) #best = min(best, -( np.sum(XNN.real[XNN.real < 0]) )) #print (XNN[XNN.real < 0]) #plt.plot(XNN.real[XNN.real<0]) print ("best", best, bestPhi) XNN = XN * np.exp(1j*bestPhi) """ """ plt.plot(XNN.real, label='phased') plt.plot(XNN.imag, label='imag') plt.legend() plt.figure() plt.plot(np.angle(XN)) plt.plot(np.angle(XNN)) plt.show() exit() """ ############################################################# # Resample ht, no gate integrate """ htd = signal.decimate(he, 100, ftype='fir') td = signal.decimate(t, 100, ftype='fir') #[htd, td] = signal.resample(he, 21, t) #toss first, and use every third htd = htd[1::5] td = td[1::5] """ ############################################################# # Resample ht and gate integrate to 20 gates per decate GPD = 20 # Gates per decade GI = 15 # initial 15 times are just used # first just resample to 20 Gates per decade #nrs = (np.log(t[-1]) - np.log10(t[0])) tdd = np.logspace( np.log10(16*dt), np.log10(t[-1]), 80-GI, base=10, endpoint=True) tdl = tdd[0:-1] # these are left edges tdr = tdd[1::] # these are left edges td = (tdl+tdr) / 2. # Just use the first 20 datapoints td = np.concatenate( ([t[0:GI] , td]) ) htd = np.zeros( len(td) ) #htd[0:15] = he[0:15] htd[0:GI] = he[0:GI] ii = 15 isum = np.zeros( len(td) ) Vars = np.ones( len(td) ) * .15**2 isum [0:15] = 1 #Vars [0:15] = .15**2 # variance #var = .15**2 for itd in range(GI, len(he)): #print (itd) if ( t[itd] > tdr[ii-GI] ): ii += 1 isum[ii] += 1 htd[ii] += he[ itd ] Vars[ii] += .15**2 htd /= isum #Vars = ( Vars) / isum Stds = np.sqrt( Vars) / isum Vars = Stds**2 #plt.figure() #plt.gca().errorbar( td, htd, fmt='o', yerr=Vars ) #plt.gca().set_xscale('log', basex=10) # htd = he[itd] # (int)(dt*itd) ] #td = [] # OK, so tdd are the desired time gates, but we might not be able to satisfy all of them #htdt = signal.decimate(he, 10, ftype='fir') ##tdt = signal.decimate(t, 10, ftype='fir') #tdt = t[::10] #[htd, td] = signal.resample(he, 21, t) ##toss first, and use every third #htd = [] # htdt[1::5] #td = tdt[1::15] #for ix in np.arange(1,len(htdt),15): # if ix>15 and ix