def main(): # Variables to be set. wavelength = 0.5 * pow(10, -6) pixeltom = 6 * pow(10, -6) distance = 0.2 propagation_type = 'IR Fresnel' k = wavenumber(wavelength) sample_field = np.zeros((500, 500), dtype=np.complex64) sample_field[240:260, 240:260] = 1000 random_phase = np.pi * np.random.random(sample_field.shape) sample_field = sample_field * np.cos( random_phase) + 1j * sample_field * np.sin(random_phase) sample_field = torch.from_numpy(sample_field) hologram = propagate_beam_torch(sample_field, k, distance, pixeltom, wavelength, propagation_type) reconstruction = propagate_beam_torch(hologram, k, -distance, pixeltom, wavelength, propagation_type) # from odak.visualize.plotly import detectorshow # detector = detectorshow() # detector.add_field(sample_field) # detector.show() # detector.add_field(hologram) # detector.show() # detector.add_field(reconstruction) # detector.show() assert True == True
def main(): # Variables to be set. wavelength = 0.5 * pow(10, -6) pixeltom = 6 * pow(10, -6) distance = 0.1 propagation_type = 'Fraunhofer' k = wavenumber(wavelength) sample_field = np.zeros((150, 150), dtype=np.complex64) sample_field[40:60, 40:60] = 10 sample_field = add_random_phase(sample_field) hologram = propagate_beam(sample_field, k, distance, pixeltom, wavelength, propagation_type) if propagation_type == 'Fraunhofer': distance = np.abs(distance) reconstruction = propagate_beam(hologram, k, distance, pixeltom, wavelength, 'Fraunhofer Inverse') from odak.visualize.plotly import detectorshow detector = detectorshow() detector.add_field(sample_field) detector.show() detector.add_field(hologram) detector.show() detector.add_field(reconstruction / np.amax(np.abs(reconstruction))) detector.show() assert True == True
def test(): no0 = [100, 100] no1 = [100, 100] size0 = [10., 10.] size1 = [10., 10.] wavelength = 0.005 surface_location_0 = [0., 0., 0.] surface_location_1 = [0., 0., 10.] wave_number = wave.wavenumber(wavelength) samples_surface_0 = tools.grid_sample(no=no0, size=size0, center=surface_location_0) samples_surface_1 = tools.grid_sample(no=no1, size=size1, center=surface_location_1) field_0 = np.zeros((no0[0], no0[1])) field_0[50, 50] = 1. field_0[0, 20] = 2. field_0 = field_0.reshape((no0[0] * no0[1])) # Propagates to a specific plane. field_1 = wave.propagate_field(samples_surface_0, samples_surface_1, field_0, wave_number, direction=1) # Reconstruction: propagates back from that specific plane to start plane. field_2 = wave.propagate_field(samples_surface_1, samples_surface_0, field_1, wave_number, direction=-1) assert True == True
def compare(): wavelength = 0.5 * pow(10, -6) pixeltom = 6 * pow(10, -6) distance = 0.2 propagation_type = 'IR Fresnel' k = wavenumber(wavelength) sample_field = np.zeros((500, 500), dtype=np.complex64) sample_field[240:260, 240:260] = 1000 random_phase = np.pi * np.random.random(sample_field.shape) sample_field = sample_field * np.cos( random_phase) + 1j * sample_field * np.sin(random_phase) sample_field_torch = torch.from_numpy(sample_field) ## Propagate and reconstruct using torch. hologram_torch = propagate_beam_torch(sample_field_torch, k, distance, pixeltom, wavelength, propagation_type) reconstruction_torch = propagate_beam_torch(hologram_torch, k, -distance, pixeltom, wavelength, propagation_type) ## Propagate and reconstruct using np. hologram = propagate_beam(sample_field, k, distance, pixeltom, wavelength, propagation_type) reconstruction = propagate_beam(hologram, k, -distance, pixeltom, wavelength, propagation_type) np.testing.assert_array_almost_equal(hologram_torch.numpy(), hologram, 3)
def main(): # Variables to be set. wavelength = 0.5*pow(10,-6) pixeltom = 6*pow(10,-6) distance = 0.2 propagation_type = 'Bandlimited Angular Spectrum' k = wavenumber(wavelength) sample_field = np.zeros((500,500),dtype=np.complex64) sample_field[ 240:260, 240:260 ] = 1000 random_phase = np.pi*np.random.random(sample_field.shape) sample_field = sample_field*np.cos(random_phase)+1j*sample_field*np.sin(random_phase) criterion = torch.nn.MSELoss() if np.__name__ == 'cupy': sample_field = np.asnumpy(sample_field) sample_field = torch.from_numpy(sample_field) hologram = propagate_beam_torch( sample_field, k, distance, pixeltom, wavelength, propagation_type ) hologram.requires_grad = True reconstruction = propagate_beam_torch( hologram, k, -distance, pixeltom, wavelength, propagation_type ) loss = criterion(torch.abs(sample_field), torch.abs(reconstruction)) loss.backward() print(hologram.grad) print('backward successfully') #from odak.visualize.plotly import detectorshow #detector = detectorshow() #detector.add_field(sample_field) #detector.show() #detector.add_field(hologram) #detector.show() #detector.add_field(reconstruction) #detector.show() assert True==True
def main(): # Variables to be set. wavelength = 0.5*pow(10,-6) pixeltom = 6*pow(10,-6) distance = 10.0 propagation_type = 'Fraunhofer' k = wavenumber(wavelength) sample_field = np.zeros((150,150),dtype=np.complex64) sample_field = np.zeros((150,150),dtype=np.complex64) sample_field[ 65:85, 65:85 ] = 1 sample_field = add_random_phase(sample_field) hologram = propagate_beam( sample_field, k, distance, pixeltom, wavelength, propagation_type ) if propagation_type == 'Fraunhofer': # Uncomment if you want to match the physical size of hologram and input field. #from odak.wave import fraunhofer_equal_size_adjust #hologram = fraunhofer_equal_size_adjust(hologram,distance,pixeltom,wavelength) propagation_type = 'Fraunhofer Inverse' distance = np.abs(distance) reconstruction = propagate_beam( hologram, k, -distance, pixeltom, wavelength, propagation_type ) #from odak.visualize.plotly import detectorshow #detector = detectorshow() #detector.add_field(sample_field) #detector.show() #detector = detectorshow() #detector.add_field(hologram) #detector.show() #detector = detectorshow() #detector.add_field(reconstruction) #detector.show() assert True==True
def __init__(self, phase=None, shape=[10., 10.], center=[0., 0., 0.], angles=[0., 0., 0.], diffusion_angle=5., diffusion_no=[3, 3], name='diffuser'): """ Class to represent a simple thin diffuser with a random phase. Parameters ---------- phase : ndarray Initial phase to be loaded. If non provided, it will start with a random phase. shape : list Shape of the detector. center : list Center of the detector. angles : list Rotation angles of the detector in degrees. diffusion angles : list Full angle of diffusion along two axes. diffusion_no : list Number of rays to be generated along two axes at each diffusion. """ self.settings = { 'name': name, 'center': center, 'angles': angles, 'rotation mode': 'XYZ', 'shape': shape, 'diffusion angle': diffusion_angle, 'number of diffusion rays': diffusion_no } self.plane = define_plane(self.settings['center'], angles=self.settings['angles']) self.k = wavenumber(0.05) self.diffusion_points = circular_uniform_sample( no=self.settings["number of diffusion rays"], radius=np.tan(np.radians(self.settings["diffusion angle"] / 2.)), center=[0., 0., 1.]) if type(phase) == type(None): self.surface_points = grid_sample(no=[100, 100], size=self.settings["shape"], center=self.settings["center"], angles=self.settings["angles"])
def gerchberg_saxton(field, n_iterations, distance, dx, wavelength, slm_range=6.28, propagation_type='IR Fresnel'): """ Definition to compute a hologram using an iterative method called Gerchberg-Saxton phase retrieval algorithm. For more on the method, see: Gerchberg, Ralph W. "A practical algorithm for the determination of phase from image and diffraction plane pictures." Optik 35 (1972): 237-246. Parameters ---------- field : torch.cfloat Complex field (MxN). distance : float Propagation distance. dx : float Size of one single pixel in the field grid (in meters). wavelength : float Wavelength of the electric field. slm_range : float Typically this is equal to two pi. See odak.wave.adjust_phase_only_slm_range() for more. propagation_type : str Type of the propagation (IR Fresnel, TR Fresnel, Fraunhofer). Result --------- hologram : torch.cfloat Calculated complex hologram. reconstruction : torch.cfloat Calculated reconstruction using calculated hologram. """ k = wavenumber(wavelength) reconstruction = field for i in range(n_iterations): hologram = propagate_beam(reconstruction, k, -distance, dx, wavelength, propagation_type) hologram = produce_phase_only_slm_pattern(hologram, slm_range) reconstruction = propagate_beam(hologram, k, distance, dx, wavelength, propagation_type) reconstruction = set_amplitude(hologram, field) reconstruction = propagate_beam(hologram, k, distance, dx, wavelength, propagation_type) return hologram, reconstruction
def main(): # Variables to be set. wavelength = 0.5 * pow(10, -6) pixeltom = 6 * pow(10, -6) distance = 10.0 propagation_type = 'Fraunhofer' k = wavenumber(wavelength) sample_field = np.random.rand(150, 150).astype(np.complex64) plane_field = plane_tilt(sample_field.shape[0], sample_field.shape[1], [0.3, 0.9, 1., 1.], dx=pixeltom, axis='x') lens_field = quadratic_phase_function(sample_field.shape[0], sample_field.shape[1], k, focal=0.3, dx=pixeltom) prism_field = prism_phase_function(sample_field.shape[0], sample_field.shape[1], k, angle=0.1, dx=pixeltom, axis='x') sample_field = sample_field * plane_field #from odak.visualize.plotly import detectorshow #detector = detectorshow() #detector.add_field(sample_field) #detector.show() #detector = detectorshow() #detector.add_field(hologram) #detector.show() #detector = detectorshow() #detector.add_field(reconstruction) #detector.show() assert True == True