"""Wavelets: Ricker, zero-phase statistical wavelet, constant phase rotation, and well-based estimation. All wavelets have an odd number of samples, are centred, and are sampled at `dt_ms`. """ from __future__ import annotations import numpy as np from scipy import signal from scipy.linalg import toeplitz def ricker(f_peak_hz: float, dt_ms: float = 4.0, length_ms: float = 128.0) -> np.ndarray: n = int(round(length_ms / dt_ms)) // 2 t = np.arange(-n, n + 1) * dt_ms / 1000.0 a = (np.pi * f_peak_hz * t) ** 2 return (1 - 2 * a) * np.exp(-a) def rotate_phase(w: np.ndarray, phase_deg: float) -> np.ndarray: """Constant phase rotation: w·cos φ − H[w]·sin φ (H: Hilbert transform).""" h = np.imag(signal.hilbert(w)) p = np.deg2rad(phase_deg) return w * np.cos(p) - h * np.sin(p) def statistical_wavelet(traces: np.ndarray, dt_ms: float = 4.0, length_ms: float = 128.0) -> np.ndarray: """Zero-phase wavelet whose amplitude spectrum equals the mean spectrum of the traces (Hann taper), normalised to unit peak.""" traces = np.atleast_2d(traces) nt = traces.shape[1] nfft = int(2 ** np.ceil(np.log2(4 * nt))) spec = np.mean(np.abs(np.fft.rfft(traces * np.hanning(nt), nfft)), axis=0) w = np.fft.fftshift(np.fft.irfft(spec, nfft)) n = int(round(length_ms / dt_ms)) // 2 c = nfft // 2 w = w[c - n : c + n + 1] * np.hanning(2 * n + 1) return w / np.max(np.abs(w)) def estimate_wavelet_ls( reflectivity: list[np.ndarray], seismic: list[np.ndarray], dt_ms: float = 4.0, length_ms: float = 128.0, damping: float = 0.05, ) -> np.ndarray: """Wavelet (amplitude in seismic units) minimising Σ‖s − w∗r‖² + ε‖w‖² over multiple wells. Toeplitz normal equations built from the autocorrelation of r and the r–s cross-correlation; ε is relative to the diagonal. Series must be finite and pairwise of equal length.""" n = int(round(length_ms / dt_ms)) // 2 m = 2 * n + 1 R = np.zeros(m) g = np.zeros(m) for r, s in zip(reflectivity, seismic): L = len(r) acf = np.correlate(r, r, mode="full")[L - 1 : L - 1 + m] R += acf ccf = np.correlate(s, r, mode="full") # ccf[L-1+k] = Σ s[i+k] r[i] g += ccf[L - 1 - n : L + n] A = toeplitz(R) A[np.diag_indices(m)] += damping * R[0] w = np.linalg.solve(A, g) return w * np.hanning(m + 2)[1:-1] def convolve_same(r: np.ndarray, w: np.ndarray) -> np.ndarray: return np.convolve(r, w, mode="same")