wiener_filter_via_curvature.py 2.79 KB
Newer Older
1
2
3
4
import numpy as np

from nifty import RGSpace, PowerSpace, Field, FFTOperator, ComposedOperator,\
                  DiagonalOperator, ResponseOperator, plotting,\
5
                  create_power_operator, nifty_configuration
6
7
8
9
10
from nifty.library import WienerFilterCurvature


if __name__ == "__main__":

11
12
    nifty_configuration['default_distribution_strategy'] = 'fftw'
    nifty_configuration['harmonic_rg_base'] = 'real'
13
14
15
16

    # Setting up variable parameters

    # Typical distance over which the field is correlated
17
    correlation_length = 0.05
18
19
20
    # Variance of field in position space sqrt(<|s_x|^2>)
    field_variance = 2.
    # smoothing length of response (in same unit as L)
21
    response_sigma = 0.01
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    # 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

    signal_space = RGSpace([N_pixels, N_pixels], distances=L/N_pixels)
    harmonic_space = FFTOperator.get_default_codomain(signal_space)
40
41
    fft = FFTOperator(harmonic_space, target=signal_space)
    power_space = PowerSpace(harmonic_space)
42
43

    # Creating the mock data
44
    S = create_power_operator(harmonic_space, power_spectrum=power_spectrum)
45

46
    mock_power = Field(power_space, val=power_spectrum)
47
48
    np.random.seed(43)
    mock_harmonic = mock_power.power_synthesize(real_signal=True)
49
50
    if nifty_configuration['harmonic_rg_base'] == 'real':
        mock_harmonic = mock_harmonic.real
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    mock_signal = fft(mock_harmonic)

    R = ResponseOperator(signal_space, sigma=(response_sigma,))
    data_domain = R.target[0]
    R_harmonic = ComposedOperator([fft, R], default_spaces=[0, 0])

    N = DiagonalOperator(data_domain,
                         diagonal=mock_signal.var()/signal_to_noise,
                         bare=True)
    noise = Field.from_random(domain=data_domain,
                              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))
    wiener_curvature = WienerFilterCurvature(S=S, N=N, R=R_harmonic)

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

    plotter = plotting.RG2DPlotter()
75
    plotter.path = 'mock_signal.html'
76
    plotter(mock_signal.real)
77
    plotter.path = 'data.html'
78
79
80
    plotter(Field(
                signal_space,
                val=data.val.get_full_data().real.reshape(signal_space.shape)))
81
    plotter.path = 'map.html'
82
    plotter(m_s.real)