wiener_filter_via_curvature.py 3.04 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
    dimensionality = 2
Martin Reinecke's avatar
Martin Reinecke committed
7
    np.random.seed(43)
8
9
10
11

    # Setting up variable parameters

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

    # Setting up the geometry

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

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

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

Martin Reinecke's avatar
adjust    
Martin Reinecke committed
46
47
48
    mock_power = ift.Field(power_space,
                           val=power_spectrum(power_space.k_lengths))
    mock_harmonic = ift.power_synthesize(mock_power, real_signal=True)
Martin Reinecke's avatar
Martin Reinecke committed
49
    mock_harmonic = mock_harmonic.real
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]
56
    R_harmonic = ift.ComposedOperator([fft, R])
57

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

    # Wiener filter

    j = R_harmonic.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
68
69
70
71
    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
72
    inverter = ift.ConjugateGradient(controller=ctrl)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
73
    wiener_curvature = ift.InversionEnabler(wiener_curvature, 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

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