forked from xiwenc1/DeepDIH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
52 lines (42 loc) · 1.46 KB
/
common.py
File metadata and controls
52 lines (42 loc) · 1.46 KB
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
import torch
import torch.nn as nn
import numpy as np
import PIL.Image as Image
import matplotlib.pyplot as plt
import torchvision.transforms as transforms
from torch import optim
from torch.autograd import Variable
import torch.nn.functional as F
def unwrap(x):
y = x % (2 * np.pi)
return torch.where(y > np.pi, 2 * np.pi - y, y)
def fft2dc(x):
return np.fft.fftshift(np.fft.fft2(x))
def ifft2dc(x):
return np.fft.ifft2(np.fft.fftshift(x))
def Phase_unwrapping(in_):
f = np.zeros((1000, 1000))
for ii in range(1000):
for jj in range(1000):
x = ii - 1000 / 2
y = jj - 1000 / 2
f[ii, jj] = x**2 + y**2
a = ifft2dc(fft2dc(np.cos(in_) * ifft2dc(fft2dc(np.sin(in_)) * f)) / (f + 0.000001))
b = ifft2dc(fft2dc(np.sin(in_) * ifft2dc(fft2dc(np.cos(in_)) * f)) / (f + 0.000001))
out = np.real(a - b)
return out
def propagator(Nx, Ny, z, wavelength, deltaX, deltaY):
k = 1 / wavelength
x = np.expand_dims(
np.arange(np.ceil(-Nx / 2), np.ceil(Nx / 2), 1) * (1 / (Nx * deltaX)), axis=0
)
y = np.expand_dims(
np.arange(np.ceil(-Ny / 2), np.ceil(Ny / 2), 1) * (1 / (Ny * deltaY)), axis=1
)
y_new = np.repeat(y, Nx, axis=1)
x_new = np.repeat(x, Ny, axis=0)
kp = np.sqrt(y_new**2 + x_new**2)
term = k**2 - kp**2
term = np.maximum(term, 0)
phase = np.exp(1j * 2 * np.pi * z * np.sqrt(term))
return phase