3from scipy.optimize
import minimize
8 def __init__(self,wavelength=656E-9,bandwidth=100E-9,grating_angle=28,grating_freq=47):
19 '''this is all the gaussian fitting stuff'''
25 new_grid = grid.shifted([-mu_x, -mu_y]).rotated(orientation)
26 x = new_grid.x / sigma_x
27 y = new_grid.y / sigma_y
29 return hp.Field(np.exp(-0.5 * r2), grid)
32 def satellite_spot(self, amplitude, mu_x, mu_y, sigma_x, sigma_y, orientation, background):
34 return hp.Field(amplitude * self.
make_gaussian(mu_x, mu_y, sigma_x, sigma_y, orientation)(grid) + background, grid)
39 j = np.sum( (self.
data - fit)**2)
41 aspect_ratio = np.abs(theta[3] / theta[4])
47 j+= 1E3 * aspect_ratio **2
50 j+= 1E3 * 1/aspect_ratio**2
54 def fit(self,theta_est):
55 fitting = minimize(self.
cost,theta_est,options={
'maxiter':self.
maxiter})
59 M00 = np.sum(self.
data)
60 M10 = np.sum(self.
data * self.
data.grid.x)
61 M01 = np.sum(self.
data * self.
data.grid.y)
63 centroid = [M10/M00,M01/M00]
67 M00 = np.sum(self.
data)
68 M10 = np.sum(self.
data * self.
data.grid.x)
69 M01 = np.sum(self.
data * self.
data.grid.y)
71 M20 = np.sum(self.
data * self.
data.grid.x**2)
72 M02 = np.sum(self.
data * self.
data.grid.y**2)
73 M11 = np.sum(self.
data * self.
data.grid.y * self.
data.grid.x)
77 mu20 = M20 / M00 - mu10**2
78 mu02 = M02 / M00 - mu01**2
79 mu11 = M11 / M00 - mu10 * mu01
80 angle = (1/2 * np.arctan2(2 * mu11, mu20 - mu02))
86 '''speckles are indexed from the top right going counter clockwise'''
90 sizes = np.array([[8,20],[20,8],[8,20],[20,8]])
93 rect = hp.make_rotated_aperture(hp.make_rectangular_aperture(size=sizes[speckle_number], center=corners[speckle_number]), np.deg2rad(-grating_angle))(image.grid)
94 speckle_img = rect * image
103 speckle_angles = np.zeros(4)
104 sig_x = [0.8,3.5,0.8,3.5]
105 sig_y = [3.5,0.8,3.5,0.8]
118 orientation = np.pi/2 - orientation
120 orientation = -orientation
123 amplitude = self.
data.max()
129 theta_est = np.array([amplitude, mu_x, mu_y, sigma_x, sigma_y, orientation, background])
130 fit = self.
fit(theta_est)
131 speckle_angles[i] = np.degrees(fit.x[5])
134 speckle_angles = np.array(speckle_angles).T
136 return speckle_angles
139 pair02 = speckle_angles[0] - speckle_angles[2]
140 pair13 = speckle_angles[1] - speckle_angles[3]
141 return np.array([pair02,pair13])
144 predicted_disp = self.
control_mtx * np.matrix(speckle_angles).T
145 predicted_disp = np.array(predicted_disp)
146 return -predicted_disp
155 '''this is stuff specifically for working with the real calibration datacubes'''
157 indx = data.grid.closest_to(center)
158 y_ind, x_ind = np.unravel_index(indx, data.shaped.shape)
159 cutout = data.shaped[(y_ind-height//2):(y_ind + height//2), (x_ind-width//2):(x_ind+width//2)]
160 sub_grid = hp.make_pupil_grid([width, height], [width * data.grid.delta[0], height * data.grid.delta[1]])
161 return hp.Field(cutout.ravel(), sub_grid)
165 img = image/image.max()
167 img_subtracted = img >0.1
168 center_of_intensity = np.array([sum(img_subtracted*img_subtracted.grid.x)/sum(img_subtracted),sum(img_subtracted*img_subtracted.grid.y)/sum(img_subtracted)])
169 mask_ap = hp.make_circular_aperture(mask_diam,center_of_intensity)
170 mask = mask_ap(img_subtracted.grid)
172 masked_img = mask * img
175 img = self.
window_field(img,[center_of_intensity[0],center_of_intensity[1]],extent,extent)
180 img = hp.Field([x
if x>0
else 0
for x
in img],img.grid)
186 for i
in range(len(data_cube)):
187 img = self.
crop_image(data_cube[i],extent,mask_diam)
188 cropped_cube.append(img)
194 ff = hp.FourierFilter(img.grid, hp.make_circular_aperture(2 * np.pi * low_freq))
195 filtered_img= np.real(ff.forward(img + 0j))
196 img = img - filtered_img
198 ff2 = hp.FourierFilter(img.grid, hp.make_circular_aperture(2 * np.pi * high_freq))
199 filtered_img = np.real(ff2.forward(img + 0j))
206 filtered_subtracted = img
208 return filtered_subtracted
speckle_pairs(self, speckle_angles)
satellite_spot(self, amplitude, mu_x, mu_y, sigma_x, sigma_y, orientation, background)
crop_image(self, image, extent=400, mask_diam=60)
current_speckle
boundary condition for the aspect ratio
crop_cube(self, data_cube, extent=400, mask_diam=60)
set_measurement(self, data)
calculate_command(self, speckle_angles)
window_field(self, data, center, width, height)
find_speckle(self, image, speckle_number)
make_gaussian(self, mu_x, mu_y, sigma_x, sigma_y, orientation)
set_control_mtx(self, matrix)
find_speckle_angles2(self)
filter_image(self, img, low_freq=0.01, high_freq=1)
__init__(self, wavelength=656E-9, bandwidth=100E-9, grating_angle=28, grating_freq=47)