wiener_filter_via_curvature.py 2.79 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
14
15
16
    correlation_length = 0.05*nu.m
    # sigma of field in position space sqrt(<|s_x|^2>)
    field_sigma = 2.* nu.K
    # 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
25
        cldim = correlation_length**(2*dimensionality)
        a = 4/(2*np.pi) * cldim * field_sigma**2
        return a / (1 + (k*correlation_length)**(2*dimensionality)) ** 2 # to be integrated over spherical shells later on
26
27
28
29

    # Setting up the geometry

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

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

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

Martin Reinecke's avatar
Martin Reinecke committed
44
    mock_power = ift.Field(power_space, val=power_spectrum(power_space.k_lengths))
45
    mock_harmonic = mock_power.power_synthesize(real_signal=True)
Martin Reinecke's avatar
Martin Reinecke committed
46
    mock_harmonic = mock_harmonic.real
47
48
    mock_signal = fft(mock_harmonic)

49
50
    exposure = 1.
    R = ift.ResponseOperator(signal_space, sigma=(response_sigma,),exposure=(exposure,))
51
    data_domain = R.target[0]
52
    R_harmonic = ift.ComposedOperator([fft, R])
53

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

    # Wiener filter

    j = R_harmonic.adjoint_times(N.inverse_times(data))
64
    ctrl = ift.GradientNormController(verbose=True,tol_abs_gradnorm=1e-4/nu.K)
Martin Reinecke's avatar
Martin Reinecke committed
65
    inverter = ift.ConjugateGradient(controller=ctrl)
Martin Reinecke's avatar
Martin Reinecke committed
66
    wiener_curvature = ift.library.WienerFilterCurvature(S=S, N=N, R=R_harmonic, inverter=inverter)
67
68
69
70

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

71
72
73
74
75
76
    sspace2=ift.RGSpace(shape, distances=L/N_pixels/nu.m)

    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")