convolution.py 2.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
Philipp Arras's avatar
Philipp Arras committed
14
# Copyright(C) 2019-2020 Max-Planck-Society
15
16
17
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

18
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
19
import nifty7 as ift
20
21
22
23
24
25
26
27
28


def convtest(test_signal, delta, func):
    domain = test_signal.domain

    # Create Convolution Operator
    conv_op = ift.FuncConvolutionOperator(domain, func)

    # Convolve, Adjoint-Convolve
29
    conv_signal = conv_op(test_signal)
30
31
    cac_signal = conv_op.adjoint_times(conv_signal)

32
33
    print(test_signal.integrate(), conv_signal.integrate(),
          cac_signal.integrate())
34
35
36
37
38
39

    # generate kernel image
    conv_delta = conv_op(delta)

    # Plot results
    plot = ift.Plot()
Philipp Arras's avatar
Philipp Arras committed
40
    plot.add(test_signal, title='Signal')
41
42
43
44
45
46
    plot.add(conv_signal, title='Signal Convolved')
    plot.add(cac_signal, title='Signal, Conv, Adj-Conv')
    plot.add(conv_delta, title='Kernel')
    plot.output()


Philipp Arras's avatar
Philipp Arras committed
47
48
49
50
def main():
    # Healpix test
    nside = 64
    npix = 12 * nside * nside
51

Philipp Arras's avatar
Philipp Arras committed
52
    domain = ift.HPSpace(nside)
53

Philipp Arras's avatar
Philipp Arras committed
54
55
56
57
    # Define test signal (some point sources)
    signal_vals = np.zeros(npix, dtype=np.float64)
    for i in range(0, npix, npix//12 + 27):
        signal_vals[i] = 500.
58

Philipp Arras's avatar
Philipp Arras committed
59
    signal = ift.makeField(domain, signal_vals)
60

Philipp Arras's avatar
Philipp Arras committed
61
62
63
    delta_vals = np.zeros(npix, dtype=np.float64)
    delta_vals[0] = 1.0
    delta = ift.makeField(domain, delta_vals)
64

Philipp Arras's avatar
Philipp Arras committed
65
66
67
68
    # Define kernel function
    def func(theta):
        ct = np.cos(theta)
        return 1. * np.logical_and(ct > 0.7, ct <= 0.8)
69

Philipp Arras's avatar
Philipp Arras committed
70
    convtest(signal, delta, func)
71

Philipp Arras's avatar
Philipp Arras committed
72
73
74
75
76
77
    domain = ift.RGSpace((100, 100))
    # Define test signal (some point sources)
    signal_vals = np.zeros(domain.shape, dtype=np.float64)
    signal_vals[35, 70] = 5000.
    signal_vals[45, 8] = 5000.
    signal = ift.makeField(domain, signal_vals)
78

Philipp Arras's avatar
Philipp Arras committed
79
80
81
82
    # Define delta signal, generate kernel image
    delta_vals = np.zeros(domain.shape, dtype=np.float64)
    delta_vals[0, 0] = 1.0
    delta = ift.makeField(domain, delta_vals)
83

Philipp Arras's avatar
Philipp Arras committed
84
    convtest(signal, delta, lambda d: 1. * np.logical_and(d > 0.1, d <= 0.2))
85
86


Philipp Arras's avatar
Philipp Arras committed
87
88
if __name__ == '__main__':
    main()