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


if __name__ == "__main__":
Martin Reinecke's avatar
Martin Reinecke committed
6
    np.random.seed(43)
7
8
9
10

    # Setting up variable parameters

    # Typical distance over which the field is correlated
11
    correlation_length = 0.05
12
13
14
    # Variance of field in position space sqrt(<|s_x|^2>)
    field_variance = 2.
    # smoothing length of response (in same unit as L)
15
    response_sigma = 0.01
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    # 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):
        a = 4 * correlation_length * field_variance**2
        return a / (1 + k * correlation_length) ** 4

    # Setting up the geometry

    # Total side-length of the domain
    L = 2.
    # Grid resolution (pixels per axis)
    N_pixels = 512

Martin Reinecke's avatar
Martin Reinecke committed
32
33
34
35
    signal_space = ift.RGSpace([N_pixels, N_pixels], distances=L/N_pixels)
    harmonic_space = signal_space.get_default_codomain()
    fft = ift.FFTOperator(harmonic_space, target=signal_space)
    power_space = ift.PowerSpace(harmonic_space)
36
37

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

Martin Reinecke's avatar
Martin Reinecke committed
41
    mock_power = ift.Field(power_space, val=power_spectrum(power_space.kindex))
42
    mock_harmonic = mock_power.power_synthesize(real_signal=True)
Martin Reinecke's avatar
Martin Reinecke committed
43
    mock_harmonic = mock_harmonic.real
44
45
    mock_signal = fft(mock_harmonic)

Martin Reinecke's avatar
Martin Reinecke committed
46
    R = ift.ResponseOperator(signal_space, sigma=(response_sigma,))
47
    data_domain = R.target[0]
Martin Reinecke's avatar
Martin Reinecke committed
48
    R_harmonic = ift.ComposedOperator([fft, R], default_spaces=[0, 0])
49

Martin Reinecke's avatar
Martin Reinecke committed
50
    N = ift.DiagonalOperator(data_domain,
51
52
                         diagonal=mock_signal.var()/signal_to_noise,
                         bare=True)
Martin Reinecke's avatar
Martin Reinecke committed
53
    noise = ift.Field.from_random(domain=data_domain,
54
55
56
57
58
59
60
61
                              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))
Martin Reinecke's avatar
Martin Reinecke committed
62
63
    ctrl = ift.DefaultIterationController(verbose=True,tol_abs_gradnorm=1e-2)
    inverter = ift.ConjugateGradient(controller=ctrl)
Martin Reinecke's avatar
Martin Reinecke committed
64
    wiener_curvature = ift.library.WienerFilterCurvature(S=S, N=N, R=R_harmonic, inverter=inverter)
65
66
67
68

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

Martin Reinecke's avatar
Martin Reinecke committed
69
    plotter = ift.plotting.RG2DPlotter()
70
    plotter.path = 'mock_signal.html'
71
    plotter(mock_signal.real)
72
    plotter.path = 'data.html'
Martin Reinecke's avatar
Martin Reinecke committed
73
    plotter(ift.Field(
74
                signal_space,
Martin Reinecke's avatar
Martin Reinecke committed
75
                val=data.val.real.reshape(signal_space.shape)))
76
    plotter.path = 'map.html'
77
    plotter(m_s.real)