-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_threshold.py
More file actions
65 lines (52 loc) · 2.31 KB
/
filter_threshold.py
File metadata and controls
65 lines (52 loc) · 2.31 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
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
from functions import conv2
class filter_threshold:
"""
Class that that takes care of the filtering and thresholding.
It helps ameliorate numerical issues ( i.e. pixel-by-pixel design variations) and introduce a weak sense of geometric scale.
"""
def __init__(self,
fR,
nElX,
nElY,
eta,
beta):
"""
@ fR: Filtering radius.
@ nElX: Number of elements in the X axis.
@ nElY: Number of elements in the Y axis.
@ eta: parameter that controls threshold value.
@ beta: parameter that controls the threshold sharpness.
"""
self.fR = fR
self.nElX = nElX
self.nElY = nElY
self.eta = eta
self.beta = beta
self.filKer, self.filSca = self.density_filter_setup()
def density_filter_setup(self):
"""
Setup of a convolution-based filter.
"""
dy, dx = np.meshgrid(np.arange(-np.ceil(self.fR)+1,np.ceil(self.fR)),
np.arange(-np.ceil(self.fR)+1,np.ceil(self.fR))) # we create a grid of values around fR for w(r)
kernel = np.maximum(np.zeros_like(dy, dtype="complex128"), self.fR*np.ones_like(dy, dtype="complex128")-np.sqrt(dx**2+dy**2)) # we create the kernel w(r)
scaling = conv2(np.ones((self.nElY, self.nElX), dtype="complex128"), kernel, 'same') # we calculate the scaling by convolving
return kernel, scaling
def density_filter(self, FilterScalingA, FilterScalingB , x, func):
"""
Application of a convolution-based filter.
"""
return conv2((x*func)/FilterScalingA, self.filKer, 'same')/FilterScalingB
def threshold(self,x):
"""
Application of a thresholding by means of a smoothed Heaviside-like function.
"""
x_out = (np.tanh(self.beta*self.eta)+np.tanh(self.beta*(x-self.eta)))/(np.tanh(self.beta*self.eta)+np.tanh(self.beta*(1-self.eta)))
return x_out
def deriv_threshold(self,x):
"""
Derivative of the thresholding used in the sensitivity calculation.
"""
x_out = (1-np.tanh(self.beta*(x-self.eta))**2)*self.beta/(np.tanh(self.beta*self.eta)+np.tanh(self.beta*(1-self.eta)))
return x_out