-
Notifications
You must be signed in to change notification settings - Fork 5
/
calibratePZT_methods.py
349 lines (240 loc) · 8.28 KB
/
calibratePZT_methods.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# -*- coding: utf-8 -*-
"""
Created on Fri May 31 20:51:32 2019
@author: Evangelos Tzardis
"""
import matplotlib.pyplot as plt
import numpy as np
try:
import tkFileDialog
except:
import tkinter.filedialog as tkFileDialog
try:
import Tkinter
except:
import tkinter as Tkinter
import imageio as io
import glob
from skimage.feature import peak_local_max
from matplotlib.lines import Line2D
import os
def read_IsIrIb():
global Is
global Ir
global Ib
folder = ResultDir + '\\IsIrIb'
Is = io.imread(folder + '\\Is' + '.tiff')
Ir = io.imread(folder + '\\Ir' + '.tiff')
Ib = io.imread(folder + '\\Ib' + '.tiff')
def normalize_image(image):
eps = np.finfo(np.float64).eps
nom = 2.0*np.sqrt(Is)*np.sqrt(Ir)
nom[np.where(nom==0)] = eps
image = (image-Is-Ir-Ib)/nom
return image
def calc_phase(image):
f = np.fft.fft2(image)
f = np.fft.fftshift(f)
f_abs = np.abs(f)
# Find the coordinates of local maxima
coeff_max = peak_local_max(f_abs, min_distance=3, threshold_rel=0.1, num_peaks=3)
# 1st maximum corresponds to the lowest dominant frquency of the image :
# coeff_max[1]
# 2nd maximum corresponds to the fringe modulation frequency :
# coeff_max[0] or coeff_max[2]
# We want to calculate the phase difference of the fourier coefficients
# indicated by the 2nd maxima of each fourier norm image.
cf = f[coeff_max[0,0],coeff_max[0,1]]
p = np.angle(cf)
return p
def im2odd_dim(image):
rows, cols = np.shape(image)
if rows % 2 == 0: # crop the last row if rows even
image = image[:-1,:]
if cols % 2 == 0: # crop the last column if columns even
image = image[:,:-1]
return image
def reject_outliers(data, m = 2.):
d = np.abs(data - np.median(data))
mdev = np.median(d)
s = d/mdev if mdev else 0.
return data[s<m]
# sort file names by numerical order indicated by name
def sort_filelist(filelist):
numlist = []
for f in filelist:
f = f[::-1]
idx = f.find('/')
f = f[:idx]
chars = []
for i in range(len(f)):
chars.append(f[i])
dig_str = [c for c in chars if c.isdigit()]
dig_str = dig_str[::-1]
numstr= ''
for dig_char in dig_str:
numstr += dig_char
num = int(numstr)
numlist.append(num)
sorted_idx = (np.argsort(numlist))
sorted_filelist = [filelist[i] for i in sorted_idx]
return sorted_filelist
def onpick1(event, fig):
global sel
global mapStart
global mapEnd
if isinstance(event.artist, Line2D):
thisline = event.artist
xdata = thisline.get_xdata()
ind = event.ind
Xsel = np.take(xdata, ind)[0]
if sel == 0:
mapStart = int(np.round(Xsel))
sel += 1
elif sel == 1:
mapEnd = int(np.round(Xsel))
plt.close(fig)
def evaluate_mapping(incremental):
global sel
global mapStart
global mapEnd
sel = 0
fig, ax = plt.subplots()
ax.set_title('click on points', picker=True)
line, = ax.plot(incremental, 'o', picker=5)
fig.canvas.mpl_connect('pick_event', lambda event: onpick1(event, fig))
return incremental[mapStart:mapEnd+1]
#if 'allfiles' in globals():
# allfiles.clear()
def run(steps):
root = Tkinter.Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
ResultDir = tkFileDialog.askdirectory(parent=root,title='Pick the folder that contains the interferometer images')
if ResultDir == '':
return
FileList1 = glob.glob(ResultDir+'\\'+'*.bmp')
FileList2 = glob.glob(ResultDir+'\\'+'*.tif')
FileList3 = glob.glob(ResultDir+'\\'+'*.tiff')
allfiles = [FileList1, FileList2, FileList3]
if len(allfiles) == 0:
raise Exception("No interferometer files were found")
if FileList1:
file_list = FileList1
# ftype = '.bmp'
elif FileList2:
file_list = FileList2
# ftype = '.tif'
elif FileList3:
file_list = FileList3
# ftype = '.tiff'
file_list = sort_filelist(file_list)
# read images of intensities of sample, reference and background
#read_IsIrIb()
listlen = len(file_list)
phases = np.zeros(listlen)
image = (io.imread(file_list[0])).astype(float)
#image = normalize_image(image)
rows, cols = np.shape(image)
image = im2odd_dim(image)
phases[0] = calc_phase(image)
for j in range(1,listlen):
image = ((io.imread(file_list[j])).astype(float))
# image = normalize_image(image)
image = im2odd_dim(image)
phases[j] = calc_phase(image)
print('%d/%d' %(j+1,listlen))
uwphases = np.unwrap(phases)
dp = np.diff(uwphases)
# Phase difference converted to displacement of fringes.
# In michelson interferometers, displacement of the sample is equal to
# half the optical path difference
lamda = 670.0 # in nm
displacements = dp/(2*np.pi)*lamda/2 # in nm
displacements = np.concatenate([[0],displacements]) # zero as the first position
incremental = np.cumsum(displacements)
grad = np.gradient(incremental)
grad = reject_outliers(grad, m = 2.)
dp_mean = np.mean(grad)
incremental = - incremental if dp_mean < 0 else incremental
#mapping = evaluate_mapping(incremental)
mapping = incremental
"""
SAVE MAPPING
"""
pardirs = 2
for i in range(pardirs):
ResultDir = os.path.abspath(os.path.join(ResultDir, os.pardir))
samenamelist1 = glob.glob(ResultDir+'\\'+'Mapping_Steps_Displacement*.txt')
len_snlist = len(samenamelist1)
np.savetxt('Mapping_Steps_Displacement_'+steps+'_{}.txt'.format(str(len_snlist+1)), mapping)
#--------
# FOR MEASURING VIBRATIONS -- ONLY WHEN FLAT MIRRORS ON BOTH ARMS AND PZT NOT MOVING
# np.savetxt('Displacements.txt', displacements)
# np.savetxt('Incremental.txt', incremental)
#--------
"""
PLOT MAPPING
"""
x = np.arange(1,listlen+1)
plt.title('PZT incremental displacement')
plt.xlabel('# of step')
plt.ylabel('Displacement in $nm$')
plt.plot(x,incremental,'-o')
## display results
#fig, axes = plt.subplots(1, 3, figsize=(8, 3), sharex=True, sharey=True)
#ax = axes.ravel()
#ax[0].imshow(f_abs, cmap=plt.cm.gray)
#ax[0].axis('off')
#ax[0].set_title('Original')
#
#ax[1].imshow(image_max, cmap=plt.cm.gray)
#ax[1].axis('off')
#ax[1].set_title('Maximum filter')
#
#ax[2].imshow(f_abs, cmap=plt.cm.gray)
#ax[2].autoscale(False)
#ax[2].plot(coordinates[:, 1], coordinates[:, 0], 'r.')
#ax[2].axis('off')
#ax[2].set_title('Peak local max')
#
#fig.tight_layout()
#
#plt.show()
#df = f2/f
#exp = np.log(df)
#shift = np.abs(exp)
#gx, gy = np.gradient(shift)
#rot_angle = np.arctan(gy/gx)
#norm = np.sqrt(gx**2 + gy**2)
########################
#sum_d = 0
#for i in range(rows):
# peaks, _ = find_peaks(image_g[i])
# d = np.diff(peaks)
# d = np.average(d)
# sum_d = sum_d + d
#
#d = sum_d/rows
#
#rot = np.deg2rad(52.67)
#theta = (rot - np.pi/2) if (rot >= np.pi/2) else (np.pi/2 - rot)
#x = d*np.cos(theta)
#pixel = lamda/x # 'x' pixels --> 'lamda' <=> 1 pixel --> 'lamda'/'x'
#x = np.arange(cols)
#dx = np.linspace(-x[-1], x[-1], 2*cols-1)
#xcorr = correlate(image[rows//2], image2[rows//2])
#pixel_shift = dx[xcorr.argmax()]
################################
#dt = 1e-2;
#t = np.arange(0,20,dt);
#y1 = np.sin(t); h1 = hilbert(y1);
#y2 = np.sin(t+1); h2 = hilbert(y2);
#p1 = np.angle(h1); p2 = np.angle(h2);
#p = np.unwrap(p2)-np.unwrap(p1);
#
#fig = plt.figure()
#ax = fig.add_subplot(211)
#ax.plot(t,p1,'r',t,p2,'b');
#ax2 = fig.add_subplot(212)
#ax2.plot(t,p,'k');