"""Seismic attributes for B0b, computed on the full trace (extended window) before cropping to the main window.""" from __future__ import annotations import numpy as np from scipy import signal FEATURES = ("amplitude", "envelope", "cos_phase", "sin_phase", "inst_freq", "relative_ai", "twt") def trace_attributes(trace: np.ndarray, times_ms: np.ndarray, dt_ms: float = 4.0) -> np.ndarray: """(L, 7) matrix in the order of FEATURES.""" s = np.asarray(trace, dtype=float) a = signal.hilbert(s) env = np.abs(a) ph = np.unwrap(np.angle(a)) f_inst = np.gradient(ph) / (2 * np.pi * dt_ms / 1000.0) # envelope-weighted instantaneous frequency, smoothed over 5 samples to suppress spikes k = np.ones(5) f_inst = np.convolve(f_inst * env**2, k, "same") / (np.convolve(env**2, k, "same") + 1e-12) f_inst = np.clip(f_inst, 0.0, 500.0 / dt_ms) # relative AI: integrated trace with the < 5 Hz trend removed rai = np.cumsum(s) sos = signal.butter(4, 5.0, btype="highpass", fs=1000.0 / dt_ms, output="sos") rai = signal.sosfiltfilt(sos, rai) return np.column_stack([s, env, np.cos(np.angle(a)), np.sin(np.angle(a)), f_inst, rai, times_ms])