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

if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
6
    # In MPI mode, the random seed for numericalunits must be set by hand
Martin Reinecke's avatar
Martin Reinecke committed
7
8
    #nu.reset_units(43)
    dimensionality = 1
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
Martin Reinecke's avatar
Martin Reinecke committed
19
    # The signal to noise ratio ***CURRENTLY BROKEN***
Martin Reinecke's avatar
Martin Reinecke committed
20
    signal_to_noise = 70.7
21
22
23
24

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

    # Setting up the geometry

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

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

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

Martin Reinecke's avatar
Martin Reinecke committed
49
    mock_power = ift.PS_field(power_space, power_spectrum)
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
50
    mock_harmonic = ift.power_synthesize(mock_power, real_signal=True)
51
    print mock_harmonic.val[0]/nu.K/(nu.m**dimensionality)
52
    mock_signal = fft(mock_harmonic)
Martin Reinecke's avatar
Martin Reinecke committed
53
    print "msig",mock_signal.val[0:10]/nu.K
54

Martin Reinecke's avatar
Martin Reinecke committed
55
56
57
    sensitivity = (1./nu.m)**dimensionality/nu.K
    R = ift.ResponseOperator(signal_space, sigma=(0.*response_sigma,),
                             sensitivity=(sensitivity,))
58
    data_domain = R.target[0]
Martin Reinecke's avatar
Martin Reinecke committed
59
    R_harmonic = R*fft
60

Martin Reinecke's avatar
Martin Reinecke committed
61
62
    noise_amplitude = 1./signal_to_noise*field_sigma*sensitivity*((L/N_pixels)**dimensionality)
    print noise_amplitude
63
    N = ift.DiagonalOperator(
Martin Reinecke's avatar
Martin Reinecke committed
64
        ift.Field.full(data_domain, noise_amplitude**2))
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
65
66
    noise = ift.Field.from_random(
        domain=data_domain, random_type='normal',
Martin Reinecke's avatar
Martin Reinecke committed
67
68
69
70
71
        std=noise_amplitude, mean=0)
    data = R(mock_signal)
    print data.val[5:10]
    data += noise
    print data.val[5:10]
Martin Reinecke's avatar
Martin Reinecke committed
72
     # Wiener filter
73
74

    j = R_harmonic.adjoint_times(N.inverse_times(data))
Martin Reinecke's avatar
Martin Reinecke committed
75
    print "xx",j.val[0]*nu.K*(nu.m**dimensionality)
Martin Reinecke's avatar
Martin Reinecke committed
76
    exit()
Martin Reinecke's avatar
adjust    
Martin Reinecke committed
77
    ctrl = ift.GradientNormController(
Martin Reinecke's avatar
Martin Reinecke committed
78
        verbose=True, tol_abs_gradnorm=1e-40/(nu.K*(nu.m**dimensionality)))
Martin Reinecke's avatar
Martin Reinecke committed
79
    inverter = ift.ConjugateGradient(controller=ctrl)
80
81
    wiener_curvature = ift.library.WienerFilterCurvature(
        S=S, N=N, R=R_harmonic, inverter=inverter)
82
83
84
85

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

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

88
    ift.plot(ift.Field(sspace2, mock_signal.val)/nu.K, name="mock_signal.png")
Martin Reinecke's avatar
Martin Reinecke committed
89
90
    data = ift.dobj.to_global_data(data.val).reshape(sspace2.shape)
    data = ift.Field(sspace2, val=ift.dobj.from_global_data(data))
91
92
    ift.plot(ift.Field(sspace2, val=data), name="data.png")
    ift.plot(ift.Field(sspace2, m_s.val)/nu.K, name="map.png")