-
Notifications
You must be signed in to change notification settings - Fork 3
/
fourier.py
216 lines (154 loc) · 5.82 KB
/
fourier.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
"""
Simple functions for my Fourier notebooks.
"""
import numpy as np
from scipy import signal
# From notebook 3
def ft1d(func):
"""Calculate the Fourier Transform of func shifting the zer-frequency index to the center where needed."""
ft = np.fft.fftshift(np.fft.fft(np.fft.ifftshift(func)))
return ft
def ift1d(func):
"""Calculate the inverse Fourier Transform of func shifting the zer-frequency index to the center where needed."""
ift = np.fft.fftshift(np.fft.ifft(np.fft.ifftshift(func)))
return ift
def ft1d_freq(x):
"""Calculate the (spatial) frequency array based on the spatial array x."""
s = np.fft.fftshift(np.fft.fftfreq(x.size, d=x[-1]-x[-2]))
return s
def rect1d(x, ampl, tint):
"""Create the rectangle function with amplitude ample and an interval tint from -tint/2 to tint/2."""
if tint/2 >= np.max(x):
raise("Your interval is larger than the provided x array.")
func = ampl * np.ones_like(x)
leftzero = np.where(x < -tint/2)
rightzero = np.where(x > tint/2)
func[leftzero] = 0
func[rightzero] = 0
return func
def triangle(x, ampl, tint):
"""Create the triangle function with amplitude ampl on interval tint from -tint/2 to tint/2."""
pt = np.pi / tint
tri = ampl/2 * signal.sawtooth(pt*x + np.pi, width=0.5) + ampl/2
# Discard all but the central triangle
indl = np.where(x < -tint) # nulling left side
tri[indl] = 0
indr = np.where(x > tint) # nulling right side
tri[indr] = 0
return tri
def gaussian(x, ampl, c):
"""Calculate a simple Gaussian with amplitude ampl and FWHM c."""
func = ampl * np.exp(-np.pi * np.square(x) / (2 * c/2))
return func
def sinusoid1d(x, nu, phi, ampl):
func = A * np.cos(2*np.pi * nu * x - phi)
return func
# From notebook 5
def ft2d(func):
ft = np.fft.fftshift(np.fft.fft2(np.fft.ifftshift(func)))
return ft
def ift2d(func):
ift = np.fft.fftshift(np.fft.ifft2(np.fft.ifftshift(func)))
return ift
def rect2d_noshift(size):
"""Rectangluar aperture. size is a tuple (x,y)."""
rect = (np.abs(xx) <= (size[0]/2)) * (np.abs(yy) <= (size[1]/2))
return rect.astype('float')
def rect2d(size):
"""Rectangluar aperture. size is a tuple (x,y)."""
rect = (np.abs(xx) <= (size[0]/2)) * (np.abs(yy) <= (size[1]/2))
return rect.astype('float')
def gaussian2d(x, y, ampl, c):
"""Calculate a simple 2D Gaussian with amplitude ampl and FWHM c."""
#func = ampl * np.exp(-np.square(x) / (2*np.square(c)))
func = ampl * np.exp(-np.pi * (np.square(x) + np.square(y)) / (2 * c/2))
return func
def sinusoid2d_norot(x, y, nu, phi, A):
func = A * np.cos(2 * np.pi * nu * (x+y) - phi)
return func
def sinusoid2d(x, y, nu, phi, A, theta=0):
xr = x * np.cos(theta)
yr = y * np.sin(theta)
func = A * np.cos(2 * np.pi * nu * (xr+yr) - phi)
return func
# From Leiden HCI class
def displC(c,trim=0):
# displC - display a Complex number c as four plots
# as a (Real, Imaginary) pair and as
# an Amplitude, Phase plot
# optionally cut out the central square with size trim by trim pixels
c2 = np.copy(c)
if (trim>0): # if the user specifies a trim value, cut out the centre of the image
(nx,ny) = c.shape
dx = (nx-trim) / 2 + 1
dy = (nx-trim) / 2 + 1
c2 = c[dx:dx+trim,dy:dy+trim]
# set up the plot panels
fig=plt.figure(figsize=(10,8))
axre = fig.add_subplot(221)
axim = fig.add_subplot(222)
axamp = fig.add_subplot(223)
axpha = fig.add_subplot(224)
# plot out the panels
im = axre.imshow(c2.real)
im = axim.imshow(c2.imag)
im = axamp.imshow(np.abs(c2))
im = axpha.imshow(np.angle(c2))
axre.set_title('Real')
axim.set_title('Imag')
axamp.set_title('Amplitude')
axpha.set_title('Phase')
plt.show()
def padcplx(c, pad=5):
"""Puts a Complex array in the centre of a zero-filled Complex array.
pad defines the padding multiplier for the output array."""
(nx, ny) = c.shape
bignx = nx * pad + 1
bigny = ny * pad + 1
big_c = np.zeros((bignx, bigny),dtype=complex)
dx = int((nx * (pad-1)) / 2 + 1)
dy = int((ny * (pad-1)) / 2 + 1)
big_c[dx:dx+nx,dy:dy+ny] = c
return(big_c)
def circle_mask(im, xc, yc, rcirc):
"""Create a circular aperture centered on (xc, yc) with radius rcirc."""
x, y = np.shape(im)
newy, newx = np.mgrid[:y,:x]
circ = (newx-xc)**2 + (newy-yc)**2 < rcirc**2
return circ.astype('float')
def zoom(im,x,y,bb):
# cut out a square box from image im centered on (x,y) with half-box size bb
return(im[y-bb:y+bb,x-bb:x+bb])
def box(c,x,y,trim=0):
# chop out a square box from an array
c2 = np.copy(c)
(nx,ny) = c.shape
dx = x - trim
dy = y - trim
c2 = c[dy:dy+2*trim,dx:dx+2*trim]
return(c2)
def rotate2(img, angle, c_in):
# rotate image img by angle degrees about point c_in
# c_in should be an np.array((y,x))
# returns the rotated image with zeroes for unknown values
from scipy.ndimage.interpolation import affine_transform
a=angle*np.pi/180.0
transform=np.array([[np.cos(a),-np.sin(a)],[np.sin(a),np.cos(a)]])
offset=c_in-c_in.dot(transform)
dst=affine_transform(img,transform.T,order=2,offset=offset,output_shape=(img.shape),cval=0.0)
return(dst)
def r_theta(im, xc, yc):
# returns the radius rr and the angle phi for point (xc,yc)
ny, nx = im.shape
yp, xp = np.mgrid[0:ny,0:nx]
yp = yp - yc
xp = xp - xc
rr = np.sqrt(np.power(yp,2.) + np.power(xp,2.))
phi = np.arctan2(yp, xp)
return(rr, phi)
def phi_ramp(im, npx, npy):
ny, nx = im.shape
ly = np.linspace(-0.5, 0.5, ny) * np.pi * npy * 2
lx = np.linspace(-0.5, 0.5, nx) * np.pi * npx * 2
x, y = np.meshgrid(lx, ly)
return(x+y)