wiener_filter_via_curvature.py 3 KB
Newer Older
1
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
2
import nifty2go as ift
3
import numericalunits as nu
4
5

if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
6
7
    # In MPI mode, the random seed for numericalunits must be set by hand
    nu.reset_units(43)
8
    dimensionality = 2
Martin Reinecke's avatar
Martin Reinecke committed
9
    np.random.seed(43)
10
11
12
13

    # Setting up variable parameters

    # Typical distance over which the field is correlated
14
15
    correlation_length = 0.05*nu.m
    # sigma of field in position space sqrt(<|s_x|^2>)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
16
    field_sigma = 2. * nu.K
17
18
    # smoothing length of response
    response_sigma = 0.01*nu.m
19
20
21
22
23
24
    # The signal to noise ratio
    signal_to_noise = 0.7

    # note that field_variance**2 = a*k_0/4. for this analytic form of power
    # spectrum
    def power_spectrum(k):
25
26
        cldim = correlation_length**(2*dimensionality)
        a = 4/(2*np.pi) * cldim * field_sigma**2
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
27
28
        # to be integrated over spherical shells later on
        return a / (1 + (k*correlation_length)**(2*dimensionality)) ** 2
29
30
31
32

    # Setting up the geometry

    # Total side-length of the domain
33
    L = 2.*nu.m
34
35
    # Grid resolution (pixels per axis)
    N_pixels = 512
36
    shape = [N_pixels]*dimensionality
37

38
    signal_space = ift.RGSpace(shape, distances=L/N_pixels)
Martin Reinecke's avatar
Martin Reinecke committed
39
40
41
    harmonic_space = signal_space.get_default_codomain()
    fft = ift.FFTOperator(harmonic_space, target=signal_space)
    power_space = ift.PowerSpace(harmonic_space)
42
43

    # Creating the mock data
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
44
45
    S = ift.create_power_operator(harmonic_space,
                                  power_spectrum=power_spectrum)
Martin Reinecke's avatar
Martin Reinecke committed
46
    np.random.seed(43)
47

Martin Reinecke's avatar
Martin Reinecke committed
48
    mock_power = ift.PS_field(power_space, power_spectrum)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
49
    mock_harmonic = ift.power_synthesize(mock_power, real_signal=True)
50
51
    mock_signal = fft(mock_harmonic)

52
    exposure = 1.
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
53
54
    R = ift.ResponseOperator(signal_space, sigma=(response_sigma,),
                             exposure=(exposure,))
55
    data_domain = R.target[0]
Martin Reinecke's avatar
Martin Reinecke committed
56
    R_harmonic = R*fft
57

58
59
60
    N = ift.DiagonalOperator(
        ift.Field.full(data_domain,
                       mock_signal.var()/signal_to_noise).weight(1))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
61
62
63
    noise = ift.Field.from_random(
        domain=data_domain, random_type='normal',
        std=mock_signal.std()/np.sqrt(signal_to_noise), mean=0)
64
65
66
67
68
    data = R(mock_signal) + noise

    # Wiener filter

    j = R_harmonic.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
69
70
    ctrl = ift.GradientNormController(
        verbose=True, tol_abs_gradnorm=1e-4/nu.K/(nu.m**(0.5*dimensionality)))
Martin Reinecke's avatar
Martin Reinecke committed
71
    inverter = ift.ConjugateGradient(controller=ctrl)
72
73
    wiener_curvature = ift.library.WienerFilterCurvature(
        S=S, N=N, R=R_harmonic, inverter=inverter)
74
75
76
77

    m = wiener_curvature.inverse_times(j)
    m_s = fft(m)

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
78
    sspace2 = ift.RGSpace(shape, distances=L/N_pixels/nu.m)
79

80
    ift.plotting.plot(ift.Field(sspace2, mock_signal.val)/nu.K,
Martin Reinecke's avatar
Martin Reinecke committed
81
                      name="mock_signal.png")
82
    data = ift.dobj.to_global_data(data.val).reshape(sspace2.shape)/nu.K
Martin Reinecke's avatar
Martin Reinecke committed
83
    data = ift.Field(sspace2, val=ift.dobj.from_global_data(data))/nu.K
Martin Reinecke's avatar
Martin Reinecke committed
84
    ift.plotting.plot(ift.Field(sspace2, val=data), name="data.png")
85
    ift.plotting.plot(ift.Field(sspace2, m_s.val)/nu.K, name="map.png")