Last active
February 6, 2022 02:52
-
-
Save kaanaksit/f9bcd8c72b4ef5e078dfb0a81b6806c5 to your computer and use it in GitHub Desktop.
A trichromat lens changing its phase delay over time looks precisely like an infinite loop. For odak visit: https://github.com/kunguz/odak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import sys | |
import math | |
from odak.learn.wave import propagate_beam, generate_complex_field, wavenumber, calculate_phase, calculate_amplitude, quadratic_phase_function | |
from odak.learn.tools import save_image, convolve2d | |
from odak.tools import check_directory, shell_command | |
from tqdm import tqdm | |
def generate(resolution, delay, distance=0.15, wavelength = 532e-9, pixel_pitch = 8e-6, propagation_type='TR Fresnel'): | |
k = wavenumber(wavelength) | |
field = torch.zeros(resolution[0], resolution[1]) | |
field[int(field.shape[0]/2.), int(field.shape[1]/2.)] = 1 | |
qwp = quadratic_phase_function(field.shape[0], field.shape[1], k, focal=-distance, dx=pixel_pitch).to(field.device) | |
qwp_phase = calculate_phase(qwp) + delay * 2 * math.pi | |
phase = convolve2d(field, qwp_phase) | |
return phase % (2* math.pi) / (2 * math.pi) * 255. | |
def main(resolution=[480, 640], output_directory='./output', n_iterations=50): | |
check_directory(output_directory) | |
amplitude = torch.rand(resolution[0], resolution[1], 3) | |
t = tqdm(range(n_iterations), leave=False) | |
delays = torch.linspace(0, 1, n_iterations) | |
wavelengths = [440e-9, 532e-9, 640e-9] | |
for i in t: | |
delay = torch.ones(3) / n_iterations * i | |
for ch in range(3): | |
delay = delays[i] + ch * 1. / n_iterations * i | |
wavelength = wavelengths[ch] | |
amplitude[:,:,ch] = generate(resolution, delay, wavelength=wavelength) | |
description = '{0}/output_{1:04d}.png'.format(output_directory, i) | |
t.set_description(description) | |
save_image('{0}/output_{1:04d}.png'.format(output_directory, i), amplitude) | |
create_gif(output_directory) | |
return True | |
def create_gif(output_directory): | |
cmd = [ | |
'ffmpeg', | |
'-y', | |
'-i', | |
'{}/output_%4d.png'.format(output_directory), | |
'{}/animation.gif'.format(output_directory) | |
] | |
shell_command(cmd) | |
return True | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment