wiener_filter_via_curvature.py 3.14 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__":
6
    nu.reset_units("SI")
7
    dimensionality = 2
Martin Reinecke's avatar
Martin Reinecke committed
8
    np.random.seed(43)
9
10
11
12

    # Setting up variable parameters

    # Typical distance over which the field is correlated
13
14
    correlation_length = 0.05*nu.m
    # sigma of field in position space sqrt(<|s_x|^2>)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
15
    field_sigma = 2. * nu.K
16
17
    # smoothing length of response
    response_sigma = 0.01*nu.m
18
19
20
21
22
23
    # 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):
24
25
        cldim = correlation_length**(2*dimensionality)
        a = 4/(2*np.pi) * cldim * field_sigma**2
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
26
27
        # to be integrated over spherical shells later on
        return a / (1 + (k*correlation_length)**(2*dimensionality)) ** 2
28
29
30
31

    # Setting up the geometry

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

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

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

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
47
    mock_power = ift.Field(power_space,
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
48
                           val=ift.dobj.from_global_data(power_spectrum(power_space.k_lengths)))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
49
    mock_harmonic = ift.power_synthesize(mock_power, real_signal=True)
Martin Reinecke's avatar
Martin Reinecke committed
50
    mock_harmonic = mock_harmonic.real
51
52
    mock_signal = fft(mock_harmonic)

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

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

    # Wiener filter

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

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

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

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
82
83
84
    ift.plotting.plot(ift.Field(sspace2, mock_signal.real.val)/nu.K,
                      name="mock_signal.pdf")
    ift.plotting.plot(ift.Field(
85
        sspace2, val=ift.dobj.from_global_data(ift.dobj.to_global_data(data.val.real).reshape(signal_space.shape)))/nu.K,
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
86
87
        name="data.pdf")
    ift.plotting.plot(ift.Field(sspace2, m_s.real.val)/nu.K, name="map.pdf")