|
| 1 | +""" |
| 2 | +Uniform Discrete Curvelet Transform |
| 3 | +=================================== |
| 4 | +
|
| 5 | +This example shows how to use the :py:class:`pylops.signalprocessing.UDCT` operator. |
| 6 | +This operator performs the Uniform Discrete Curvelet Transform along all or a portion |
| 7 | +of the axes of multi-dimensional input array. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +from math import prod |
| 12 | + |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +import numpy as np |
| 15 | +from curvelets.utils import make_zone_plate |
| 16 | + |
| 17 | +import pylops |
| 18 | + |
| 19 | +plt.close("all") |
| 20 | + |
| 21 | +############################################################################### |
| 22 | +# To begin with consider the same input of the first tutorial of the |
| 23 | +# `curvelets <https://curvelets.readthedocs.io/en/stable/auto_examples/plot_01_zone_plate.html>`_ |
| 24 | +# library. However, we will use here the PyLops operator to apply the 2D UDCT. |
| 25 | +# A round-trip (i.e., forward-adjoing) is performed and the output is shown to be |
| 26 | +# equal to the input, meaning that the adjoint of this transform is also the inverse. |
| 27 | + |
| 28 | +shape = (256, 256) |
| 29 | +x = make_zone_plate(shape) |
| 30 | + |
| 31 | +Cop = pylops.signalprocessing.UDCT(shape) |
| 32 | + |
| 33 | +y = Cop @ x |
| 34 | +xinv = Cop.H @ y |
| 35 | + |
| 36 | +fig, axs = plt.subplots(1, 3, figsize=(10, 4)) |
| 37 | +axs[0].imshow(x, cmap="gray", vmin=-1, vmax=1) |
| 38 | +axs[0].set_title("Input") |
| 39 | +axs[1].imshow(xinv, cmap="gray", vmin=-1, vmax=1) |
| 40 | +axs[1].set_title("Round-trip") |
| 41 | +axs[2].imshow(x - xinv, cmap="gray", vmin=-1, vmax=1) |
| 42 | +axs[2].set_title("Difference") |
| 43 | +for ax in axs: |
| 44 | + ax.axis("tight") |
| 45 | + ax.axis("off") |
| 46 | +fig.tight_layout() |
| 47 | + |
| 48 | + |
| 49 | +############################################################################### |
| 50 | +# Next, the 2D version of the UDCT can also be applied along the last two axes |
| 51 | +# of a N-dimensional input. This can be easily done with the aid of the |
| 52 | +# :py:class:`pylops.Kronecker` operator. |
| 53 | + |
| 54 | +shape = (2, 3, 64, 32) |
| 55 | +x = make_zone_plate(shape[2:]) |
| 56 | + |
| 57 | +# replicate over the first two axes. |
| 58 | +x = np.tile(x[None, None], (shape[0], shape[1], 1, 1)) |
| 59 | + |
| 60 | +Cop = pylops.signalprocessing.UDCT(dims=shape[2:]) |
| 61 | +Iop = pylops.Identity(prod(shape[:2])) |
| 62 | +Cop = pylops.Kronecker(Iop, Cop) |
| 63 | + |
| 64 | +y = Cop @ x.ravel() |
| 65 | +xinv = (Cop.H @ y).reshape(shape) |
| 66 | + |
| 67 | +fig, axs = plt.subplots(1, 3, figsize=(10, 4)) |
| 68 | +axs[0].imshow(np.mean(x, axis=(0, 1)), cmap="gray", vmin=-1, vmax=1) |
| 69 | +axs[0].set_title("Input") |
| 70 | +axs[1].imshow(np.mean(xinv, axis=(0, 1)), cmap="gray", vmin=-1, vmax=1) |
| 71 | +axs[1].set_title("Round-trip") |
| 72 | +axs[2].imshow( |
| 73 | + np.mean(x, axis=(0, 1)) - np.mean(xinv, axis=(0, 1)), cmap="gray", vmin=-1, vmax=1 |
| 74 | +) |
| 75 | +axs[2].set_title("Difference") |
| 76 | +for ax in axs: |
| 77 | + ax.axis("tight") |
| 78 | + ax.axis("off") |
| 79 | +fig.tight_layout() |
| 80 | + |
| 81 | + |
| 82 | +############################################################################### |
| 83 | +# And similarly, if we want to apply the UDCT over any two axes, the |
| 84 | +# :py:class:`pylops.Transpose` can be used to re-organize the |
| 85 | +# axes of the input prior to applying the transform |
| 86 | +shape = (2, 64, 3, 32) |
| 87 | +x = make_zone_plate((shape[1], shape[3])) |
| 88 | + |
| 89 | +# replicate over the first two axes. |
| 90 | +x = np.tile(x[None, :, None], (shape[0], 1, shape[2], 1)) |
| 91 | + |
| 92 | +Cop = pylops.signalprocessing.UDCT(dims=(shape[1], shape[3])) |
| 93 | +Top = pylops.Transpose(shape, axes=(0, 2, 1, 3)) |
| 94 | +Iop = pylops.Identity(prod((shape[0], shape[2]))) |
| 95 | +Cop = pylops.Kronecker(Iop, Cop) @ Top |
| 96 | + |
| 97 | +y = Cop @ x.ravel() |
| 98 | +xinv = (Cop.H @ y).reshape(shape) |
| 99 | + |
| 100 | +fig, axs = plt.subplots(1, 3, figsize=(10, 4)) |
| 101 | +axs[0].imshow(np.mean(x, axis=(0, 2)), cmap="gray", vmin=-1, vmax=1) |
| 102 | +axs[0].set_title("Input") |
| 103 | +axs[1].imshow(np.mean(xinv, axis=(0, 2)), cmap="gray", vmin=-1, vmax=1) |
| 104 | +axs[1].set_title("Round-trip") |
| 105 | +axs[2].imshow( |
| 106 | + np.mean(x, axis=(0, 2)) - np.mean(xinv, axis=(0, 2)), cmap="gray", vmin=-1, vmax=1 |
| 107 | +) |
| 108 | +axs[2].set_title("Difference") |
| 109 | +for ax in axs: |
| 110 | + ax.axis("tight") |
| 111 | + ax.axis("off") |
| 112 | +fig.tight_layout() |
| 113 | + |
| 114 | + |
| 115 | +############################################################################### |
| 116 | +# Finally, if the :py:class:`pylops.signalprocessing.UDCT` is applied directly |
| 117 | +# to a 3-dimensional (or N-dimensional) input, the 3D/ND version of the |
| 118 | +# transform is used. |
| 119 | + |
| 120 | +shape = (16, 32, 16) |
| 121 | +x = make_zone_plate(shape[1:]) |
| 122 | + |
| 123 | +# replicate over the first axis. |
| 124 | +x = np.tile(x[None], (shape[0], 1, 1)) |
| 125 | + |
| 126 | +Cop = pylops.signalprocessing.UDCT(dims=shape) |
| 127 | + |
| 128 | +y = Cop @ x |
| 129 | +xinv = Cop.H @ y |
| 130 | + |
| 131 | +fig, axs = plt.subplots(1, 3, figsize=(10, 4)) |
| 132 | +axs[0].imshow(np.mean(x, axis=0), cmap="gray", vmin=-1, vmax=1) |
| 133 | +axs[0].set_title("Input") |
| 134 | +axs[1].imshow(np.mean(xinv, axis=0), cmap="gray", vmin=-1, vmax=1) |
| 135 | +axs[1].set_title("Round-trip") |
| 136 | +axs[2].imshow(np.mean(x, axis=0) - np.mean(xinv, axis=0), cmap="gray", vmin=-1, vmax=1) |
| 137 | +axs[2].set_title("Difference") |
| 138 | +for ax in axs: |
| 139 | + ax.axis("tight") |
| 140 | + ax.axis("off") |
| 141 | +fig.tight_layout() |
0 commit comments