def mask_time(D,shot,dt,dx,vel): # setup # dt = 4e-3 # dx = 10 # vel_ocean = 1490 nt,nr = D.size shot = shot - 1 # python stuffs start with zero x1 = np.arange(shot) x2 = np.arange(shot,nr) t1 = (shot-x1)*dx/(vel*dt) t2 = (x2-shot)*dx/(vel*dt) t_vec = np.concatenate((t1,t2),axis=None) + 140 # mask for sediment reflection t3 = np.sqrt(830**2 + ((shot-x1)*dx)**2/((vel*dt)**2)) t4 = np.sqrt(830**2 + ((x2-shot)*dx)**2/((vel*dt)**2)) t_vec2 = np.concatenate((t3,t4),axis=None) mask_t2 = np.zeros((nt,nr)) for i in np.arange(nr): mask_t2[int(t_vec[i]):int(t_vec2[i]),i] = 1 Sop_t = pylops.Smoothing2D(nsmooth=[11,3], dims=[nt, nr]) mask_2 = pylops.Diagonal(Sop_t*mask_t2.flatten()) D_mask = (mask_2*D.flatten()).reshape(nt,nr) return D_mask,mask_2
import pylops plt.close('all') ############################################################################### # Define the input parameters: number of samples of input signal (``N`` and ``M``) and # lenght of the smoothing filter regression coefficients # (:math:`n_{smooth,1}` and :math:`n_{smooth,2}`). In this first case the input # signal is one at the center and zero elsewhere. N, M = 11, 21 nsmooth1, nsmooth2 = 5, 3 A = np.zeros((N, M)) A[5, 10] = 1 Sop = pylops.Smoothing2D(nsmooth=[nsmooth1, nsmooth2], dims=[N, M], dtype='float64') B = Sop*A.flatten() B = np.reshape(B, (N, M)) ############################################################################### # After applying smoothing, we will also try to invert it. Aest = Sop/B.flatten() Aest = np.reshape(Aest, (N, M)) fig, axs = plt.subplots(1, 3, figsize=(10, 3)) im = axs[0].imshow(A, interpolation='nearest', vmin=0, vmax=1) axs[0].axis('tight') axs[0].set_title('Model') plt.colorbar(im, ax=axs[0]) im = axs[1].imshow(B, interpolation='nearest', vmin=0, vmax=1) axs[1].axis('tight')
loc = shot[i] x1 = np.arange(loc) x2 = np.arange(loc, nr) t1 = (loc - x1) * dx / (vel * dt) t2 = (x2 - loc) * dx / (vel * dt) t_vec = np.concatenate((t1, t2), axis=None) + 140 # mask for sediment reflection t3 = np.sqrt(830**2 + ((loc - x1) * dx)**2 / ((vel * dt)**2)) t4 = np.sqrt(830**2 + ((x2 - loc) * dx)**2 / ((vel * dt)**2)) t_vec2 = np.concatenate((t3, t4), axis=None) for k in np.arange(nr): mask_t[int(t_vec[k]):int(t_vec2[k]), k, i] = 1 Sop_t = pylops.Smoothing2D(nsmooth=[11, 3], dims=[nt, nr]) mask_t[:, :, i] = (Sop_t * mask_t[:, :, i].flatten()).reshape(nt, nr) mask_tt = pylops.Diagonal(mask_t.flatten()) D = (mask_tt * D.flatten()).reshape(nt, nr, ns) # mask on Fourier domain kn = 1 / (2 * dx) dk = 2 * kn / nr mask_fre_loc = np.zeros((nt, nr, ns)) vel = 1450 vn = 1 / (2 * dt) dv = 2 * vn / nt vs = np.arange(-vn, vn, dv)
# Numerical derivative Dop = pylops.FirstDerivative(nt * nx, dims=(nt, nx), dir=0, sampling=dt) xder = Dop * yn.flatten() xder = xder.reshape(nt, nx) # Regularized derivative Rop = pylops.Laplacian(dims=(nt, nx)) xreg = pylops.RegularizedInversion(Cop, [Rop], yn.flatten(), epsRs=[1e0], **dict(iter_lim=100, atol=1e-5)) xreg = xreg.reshape(nt, nx) # Preconditioned derivative Sop = pylops.Smoothing2D((11, 21), dims=(nt, nx)) xp = pylops.PreconditionedInversion(Cop, Sop, yn.flatten(), **dict(iter_lim=10, atol=1e-2)) xp = xp.reshape(nt, nx) # Visualize data and inversion vmax = 2 * np.max(np.abs(x)) fig, axs = plt.subplots(2, 3, figsize=(18, 12)) axs[0][0].imshow(x, cmap='seismic', vmin=-vmax, vmax=vmax) axs[0][0].set_title('Model') axs[0][0].axis('tight') axs[0][1].imshow(y, cmap='seismic', vmin=-vmax, vmax=vmax) axs[0][1].set_title('Data') axs[0][1].axis('tight') axs[0][2].imshow(yn, cmap='seismic', vmin=-vmax, vmax=vmax) axs[0][2].set_title('Noisy data')
def MovingAverage2D(window_shape: Union[tuple, list], shape: tuple, dtype='float64'): r""" 2D moving average. Apply moving average to a 2D array. Parameters ---------- window_size: Union[tuple, list] Shape of the window for moving average (sizes in each dimension must be *odd*). shape: tuple Shape of the input array. dtype: str Type of elements in input array. Returns ------- :py:class:`pycsou.linop.base.PyLopLinearOperator` 2D moving average operator. Examples -------- .. plot:: import numpy as np import matplotlib.pyplot as plt from pycsou.linop.conv import MovingAverage2D from scipy import signal sig = np.zeros(shape=(100,100)) sig[sig.shape[0] // 2 - 2:sig.shape[0] // 2 + 3, sig.shape[1] // 2 - 2:sig.shape[1] // 2 + 3] = 1 MAOp = MovingAverage2D(window_shape=(50,25), shape=sig.shape) moving_average = (MAOp * sig.ravel()).reshape(sig.shape) plt.figure() plt.subplot(1,2,1) plt.imshow(sig, cmap='plasma'); plt.title('Signal') plt.subplot(1,2,2) plt.imshow(moving_average, cmap='plasma'); plt.title('Moving Average') plt.show() Notes ----- The ``MovingAverage2D`` operator is a special type of convolution operator that convolves a 2D array with a constant 2d filter of size :math:`n_{smooth, 1} \quad \times \quad n_{smooth, 2}`: .. math:: y[i,j] = \frac{1}{n_{smooth, 1} n_{smooth, 2}} \sum_{l=-(n_{smooth,1}-1)/2}^{(n_{smooth,1}-1)/2} \sum_{m=-(n_{smooth,2}-1)/2}^{(n_{smooth,2}-1)/2} x[l,m] Note that since the filter is symmetrical, the ``MovingAverage2D`` operator is self-adjoint. """ PyLop = pylops.Smoothing2D(nsmooth=window_shape, dims=shape, nodir=None, dtype=dtype) return PyLopLinearOperator(PyLop)