wiener_filter_easy.py 1.99 KB
Newer Older
theos's avatar
theos committed
1
2

from nifty import *
Martin Reinecke's avatar
Martin Reinecke committed
3
4
#import plotly.offline as pl
#import plotly.graph_objs as go
theos's avatar
theos committed
5
6
7
8
9
10
11
12

from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.rank


if __name__ == "__main__":

Martin Reinecke's avatar
Martin Reinecke committed
13
    distribution_strategy = 'not'
14
15
16
17
18
19
20
21
    
    #Setting up physical constants
    #total length of Interval or Volume the field lives on, e.g. in meters
    L = 2.
    #typical distance over which the field is correlated (in same unit as L)
    correlation_length = 0.1
    #variance of field in position space sqrt(<|s_x|^2>) (in unit of s)
    field_variance = 2.
22
    #smoothing length of response (in same unit as L)
23
24
25
26
27
28
29
30
31
32
33
34
35
    response_sigma = 0.1
    
    #defining resolution (pixels per dimension)
    N_pixels = 512
    
    #Setting up derived constants
    k_0 = 1./correlation_length
    #note that field_variance**2 = a*k_0/4. for this analytic form of power
    #spectrum
    a = field_variance**2/k_0*4.
    pow_spec = (lambda k: a / (1 + k/k_0) ** 4)
    pixel_width = L/N_pixels
    
36
    # Setting up the geometry
37
    s_space = RGSpace([N_pixels, N_pixels], distances = pixel_width)
theos's avatar
theos committed
38
39
40
41
    fft = FFTOperator(s_space)
    h_space = fft.target[0]
    p_space = PowerSpace(h_space, distribution_strategy=distribution_strategy)

42
43

    # Creating the mock data
theos's avatar
theos committed
44
45
46
47
48
49
50
51
52

    S = create_power_operator(h_space, power_spectrum=pow_spec,
                              distribution_strategy=distribution_strategy)

    sp = Field(p_space, val=pow_spec,
               distribution_strategy=distribution_strategy)
    sh = sp.power_synthesize(real_signal=True)
    ss = fft.inverse_times(sh)

53
    R = SmoothingOperator(s_space, sigma=response_sigma)
theos's avatar
theos committed
54
55
56
57
58
59
60
61
62

    signal_to_noise = 1
    N = DiagonalOperator(s_space, diagonal=ss.var()/signal_to_noise, bare=True)
    n = Field.from_random(domain=s_space,
                          random_type='normal',
                          std=ss.std()/np.sqrt(signal_to_noise),
                          mean=0)

    d = R(ss) + n
63
64

    # Wiener filter
65

theos's avatar
theos committed
66
67
68
69
    j = R.adjoint_times(N.inverse_times(d))
    D = PropagatorOperator(S=S, N=N, R=R)

    m = D(j)