-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterf.py
169 lines (151 loc) · 7.03 KB
/
interf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import numpy as np
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy import integrate
def make_w_axis(w_min=0,w_step=4,w_max=4096,taxis=0):
#returns a frequency axis (cm-1) given either w min, step, max or a time axis in seconds
if taxis==0:
waxis = np.arange(w_min,w_max,w_step,dtype=float)
else:
dt = taxis[1] - taxis[0]
waxis = np.linspace(-1.0/dt, 1.0/dt, len(taxis))
return waxis
def gaussian(w, mu, sigma):
#returns a gaussian centered at mu with width sigma
#unnormalized
return np.exp(-np.divide(np.power(w-mu,2.0),2.0*sigma*sigma))
def main(args):
##Define BBIR spectrum
c = 29979245800.0 #speed of light in cm/s
_to_ps = 1.0e12 # convert from s to ps
w = make_w_axis( 0.0, 1.0, 8096.0 )#define frequency axis
mu1, mu2 = 1320.0, 1680.0 #peak centers for the spectrum
sig1, sig2 = 130.0, 130.0 #peak widths
sig_w = 0.85*gaussian(w,mu1,sig1) + 1.0*gaussian(w,mu2,sig2) #define the spectrum
##Initlize Plot
fig = plt.figure(dpi=600, figsize=[12, 6], num=1) #initialize figure A4 size
lw = 0.5 #default linewidth
lfs = 'xx-small'#legend font size
wlabel = 'wavenumber (cm$^{-1}$)' #w axis label
Elabel = 'electric field (AU)' #electric field label
tlabel = 'time (s)'
gs = mpl.gridspec.GridSpec(3,2, figure=fig) #make two rows and three columns
#Make first subplot
ax = fig.add_subplot(gs[0,0], title='$|E(w)|^2$')
ax.plot(w, sig_w, 'b-', lw=lw)
ax.set(xlabel=wlabel, ylabel=Elabel)
ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
ax.autoscale(enable=True, axis='x', tight=True)
ax.set_axisbelow(True)
plt.xlim(left=800, right=2200)
plt.ylim(bottom=0, top=1.1)
##Define BBIR E field from FFT of spectrum
t = np.arange(len(w)) #define time axis
t = np.divide(t - np.mean(t), c*np.amax(w)) #set the units
t0 = np.argmin(np.abs(t))
dt = t[1]-t[0]
if args.debug:
print('Made time axis with ranging from {:.2f} to {:.2f} ps with center at index {} and delta_t of {:.2f} ps'.format(np.min(t)*_to_ps,np.max(t)*_to_ps,t[t0],dt*_to_ps))
sig_t = np.fft.ifft(np.sqrt(sig_w)) #complex (transform-limited) electric field in the time domain (note this sqrt was not in the MATLAB version)
sig_t = np.fft.fftshift(sig_t)
#Make second subplot
ax = fig.add_subplot(gs[0,1], title='E(t)')
ax.plot(t, sig_t.real, 'b-', lw=lw, label='real')
ax.plot(t, sig_t.imag, 'r', lw=lw, label='imag')
ax.plot(t, np.abs(sig_t), 'k', lw=lw, label='abs')
ax.set(xlabel=tlabel, ylabel=Elabel)
ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
ax.autoscale(enable=True, axis='x', tight=True)
ax.set_axisbelow(True)
plt.tight_layout()
plt.xlim(left=t[t0]-0.2e-12, right=t[t0]+0.2e-12)
#Make chirped pulses
c1=1.0e10#chirp for arm1
c2=1.5e10#chirp for arm2
sig_1=np.fft.fftshift(np.fft.ifft(np.multiply(np.sqrt(sig_w),np.exp(-1j*(c1*np.multiply(w,t)+c1*np.multiply(w,t**2))))))
sig_1 = np.real(sig_1)
sig_2_0 =np.fft.fftshift(np.fft.ifft(np.multiply(np.sqrt(sig_w),np.exp(-1j*(c2*np.multiply(w,t)+c2*np.multiply(w,t**2))))))
sig_2_0 = np.real(sig_2_0)
##Make third subplot
#ax = fig.add_subplot(gs[1,0], title='E(t)')
#ax.plot(t, sig_1.real, 'b-', lw=lw, label='Re[E1]')
#ax.plot(t, sig_2_0.real, 'r-', lw=lw, label='Re[E2]')
#ax.set(xlabel=tlabel, ylabel=Elabel)
#ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
#ax.autoscale(enable=True, axis='x', tight=True)
#ax.set_axisbelow(True)
#plt.tight_layout()
#plt.xlim(left=t[t0-100], right=t[t0+100])
LIVE=False
n=0
scan_range = np.arange(-150,150,1)
interf_t = np.zeros((len(scan_range)))
for t_n in scan_range:
sig_2 = np.roll(sig_2_0,t_n)#np.roll is used to scan the time axis
interf_t[n] = integrate.simps(np.power(np.abs(sig_1+sig_2),2.0),t) #the interferogram is the frequency-integrated, magnitude squared of the sum of the electric fields
n = n + 1
if LIVE or t_n==scan_range[-1]:
#Update third subplot
#ax.clear()
ax = fig.add_subplot(gs[1,0], title='E1+E2')
ax.plot(t, np.real(sig_1+sig_2),'m-',lw=lw, label='Re[E1+E2]')
ax.plot(t, np.real(sig_1),'r-',lw=lw, label='Re[E1]')
ax.plot(t, np.real(sig_2),'b-',lw=lw, label='Re[E2]')
ax.set(xlabel=tlabel, ylabel=Elabel)
ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
ax.autoscale(enable=True, axis='x', tight=True)
ax.set_axisbelow(True)
plt.tight_layout()
plt.xlim(left=t[t0]-1.0e-12, right=t[t0]+1.0e-12)
#Make forth subplot
ax = fig.add_subplot(gs[1,1], title='E1+E2')
ax.plot(t, np.real(sig_1+sig_2),'m-',lw=lw, label='Re[E1+E2]')
ax.plot(t, np.real(sig_1),'r-',lw=lw, label='Re[E1]')
ax.plot(t, np.real(sig_2),'b-',lw=lw, label='Re[E2]')
ax.plot(t, sig_2.real, 'r-', lw=lw, label='Re[E2]')
ax.set(xlabel=tlabel, ylabel=Elabel)
ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
ax.autoscale(enable=True, axis='x', tight=True)
ax.set_axisbelow(True)
plt.tight_layout()
plt.xlim(left=t[t0]-5.0e-12, right=t[t0]+5.0e-12)
#Make fifth subplot
ax = fig.add_subplot(gs[2,0], title='Interferogram')
ax.plot(t[0:n]-np.mean(t[0:n]), interf_t[0:n], lw=lw, label='$\int{dt {|E1+E2|}^2}$')
ax.set(xlabel=tlabel, ylabel='Interferogram Signal')
ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
ax.autoscale(enable=True, axis='x', tight=True)
ax.set_axisbelow(True)
plt.tight_layout()
if LIVE: plt.show()
#Make sixth subplot
ax = fig.add_subplot(gs[2,1], title='FFT of Interferogram')
interferogram_spectrum = np.abs(np.fft.fftshift(np.fft.fft(interf_t)))
interferogram_spectrum = np.sqrt(interferogram_spectrum)
w_interf = np.fft.fftfreq(n=interf_t.size, d=dt)*2.0/(np.pi*c)
w_interf = np.fft.fftshift(w_interf)
ax.plot(w_interf, interferogram_spectrum, lw=lw)
ax.set(xlabel=wlabel, ylabel='FFT Power')
ax.legend(fontsize=lfs, loc='best', bbox_to_anchor=[1, 0, 0.5, 1])
ax.autoscale(enable=True, axis='x', tight=True)
ax.set_axisbelow(True)
plt.tight_layout()
plt.xlim(left=-3200, right=3200)
plt.ylim(bottom=0, top=1.1*np.amax(interferogram_spectrum[w_interf>1000]))
fs = 8 #default fontsize
plt.rc('font', size=fs)
plt.rc('lines', linewidth=1)
#plt.show()
plt.savefig('test.png', bbox_inches='tight')
return
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--debug', action='store_true', help='turns on debugging output')
args = parser.parse_args()
try:
main(args)
except Exception as e:
print(str(e),0)
raise e