def get_polarization_stuff(st, env): Ztrace = st.select(component="Z")[0] Ntrace = st.select(component="N")[0] Etrace = st.select(component="E")[0] smooth_env = sp.smooth(env) imax = np.argmax(smooth_env) end_window = int(np.round(imax / 3.)) # normalise to get decent numbers maxZ = max(abs(Ztrace.data)) xP = Etrace.data[0:end_window] / maxZ yP = Ntrace.data[0:end_window] / maxZ zP = Ztrace.data[0:end_window] / maxZ try: MP = np.cov(np.array([xP, yP, zP])) w, v = np.linalg.eig(MP) except np.linalg.linalg.LinAlgError: return np.NaN, np.NaN, np.NaN, np.NaN indexes = np.argsort(w) DP = w[indexes] pP = v[:, indexes] rectilinP = 1 - ((DP[0] + DP[1]) / (2 * DP[2])) azimuthP = np.arctan(pP[1, 2] / pP[0, 2]) * 180. / np.pi dipP = np.arctan( pP[2, 2] / np.sqrt(pP[1, 2]**2 + pP[0, 2]**2)) * 180 / np.pi Plani = 1 - (2 * DP[0]) / (DP[1] + DP[2]) return rectilinP, azimuthP, dipP, Plani
def get_CorrStuff(tr): min_peak_height = 0.4 # This value is sligtly greater than the matlab one, to account for # differences in floating precision cor = np.correlate(tr.data, tr.data, mode='full') cor = cor / np.max(cor) # find number of peaks cor_env = np.abs(hilbert(cor)) cor_smooth = sp.smooth(cor_env) npts = len(cor_smooth) max_cor = np.max(cor_smooth) CorPeakNumber, itriggers = sp.find_peaks(cor_smooth, min_peak_height) # integrate over bands ilag_0 = np.argmax(cor_smooth) + 1 ilag_third = ilag_0 + npts / 6 # note that these integrals are flase really (dt is not correct) int1 = np.trapz(cor_smooth[ilag_0:ilag_third + 1] / max_cor) int2 = np.trapz(cor_smooth[ilag_third:] / max_cor) int_ratio = int1 / int2 return CorPeakNumber, int_ratio
def test_smooth(self): imax_fraction = 0.2 max_amp = 300 tr, modulation = syn.modulate_trace_triangle(self.tr, imax_fraction, max_amp) smooth_modulation = sp.smooth(modulation) self.assertEqual(len(modulation), len(smooth_modulation)) self.assertEqual(np.argmax(modulation), np.argmax(smooth_modulation))
def get_RappStuff(tr, env=None): # get max over mean / median / std if env is None: env = envelope(tr.data) max_env = max(env) smooth_norm_env = sp.smooth(env / max_env) RappMaxMean = 1. / np.mean(smooth_norm_env) RappMaxMedian = 1. / np.median(smooth_norm_env) RappMaxStd = 1. / np.std(smooth_norm_env) return RappMaxMean, RappMaxMedian, RappMaxStd
def get_AsDec(tr, env=None): # smooth data using a filter of the same length as the sampling rate # to give 1-second smoothing window if env is None: env = envelope(tr.data) smooth_env = sp.smooth(env) imax = np.argmax(smooth_env) AsDec = (imax + 1) / float(len(tr.data) + 1 - (imax + 1)) # note : the +1s are to avoid zeros or division by zero return AsDec
def get_KurtoSkew(tr, env=None): if env is None: env = envelope(tr.data) max_env = max(env) smooth_norm_env = sp.smooth(env / max_env) max_sig = max(tr.data) norm_sig = tr.data / max_sig KurtoEnv = kurtosis(smooth_norm_env, fisher=False) KurtoSig = kurtosis(norm_sig, fisher=False) SkewnessEnv = skew(smooth_norm_env) SkewnessSig = skew(norm_sig) return KurtoEnv, KurtoSig, SkewnessEnv, SkewnessSig
import numpy as np from copy import deepcopy from obspy.signal.filter import envelope from scipy.signal import spline_filter, hilbert #sig1 = syn.gen_gaussian_noise(0.01, 54321, 100) dt = 0.01 npts = 3000 t_vect = np.arange(npts) * dt sig1 = syn.gen_sweep_poly(dt, npts, [0.05, -0.75, 0.1, 1.0]) sig2 = sig1.copy() sig2, modulation = syn.modulate_trace_triangle(sig2, 0.2, 300) env = envelope(sig2.data) smooth_env1 = sp.smooth(env, window='hanning') smooth_env2 = sp.smooth(env, window='flat') assert (len(env) == len(smooth_env1)) rep = syn.gen_repeat_signal(dt, npts, 100, 5) cor = np.correlate(rep.data, rep.data, mode='full') t_vect_cor = np.arange(len(cor)) * dt - len(cor) / 2 * dt cor_env = np.abs(hilbert(cor)) cor_smooth = sp.smooth(cor_env) max_cor = np.max(cor_smooth) cor_smooth2 = sp.smooth(cor_smooth / max_cor) NyF = 1 / (2.0 * dt) sig3 = syn.gen_gaussian_noise(dt, npts, 100) FFI = [0.1, 1, 3, 10, 20] nf = len(FFI)
def get_full_spectrum_stuff(tr): sps = tr.stats.sampling_rate NyF = sps / 2.0 data = tr.data npts = tr.stats.npts n = sp.nextpow2(2 * npts - 1) df = n / 2 * NyF Freq1 = np.linspace(0, 1, n / 2) * NyF FFTdata = 2 * np.abs(np.fft.fft(data, n=n)) / float(npts * npts) FFTsmooth = sp.smooth(FFTdata[0:len(FFTdata) / 2]) FFTsmooth_norm = FFTsmooth / max(FFTsmooth) MeanFFT = np.mean(FFTsmooth_norm) MedianFFT = np.median(FFTsmooth_norm) VarFFT = np.var(FFTsmooth_norm, ddof=1) MaxFFT = np.max(FFTsmooth) iMaxFFT = np.argmax(FFTsmooth) FmaxFFT = Freq1[iMaxFFT] xCenterFFT = np.sum((np.arange(len(FFTsmooth_norm))) * FFTsmooth_norm) / np.sum(FFTsmooth_norm) i_xCenterFFT = int(np.round(xCenterFFT)) xCenterFFT_1quart = np.sum((np.arange(i_xCenterFFT+1)) * FFTsmooth_norm[0:i_xCenterFFT+1]) /\ np.sum(FFTsmooth_norm[0:i_xCenterFFT+1]) i_xCenterFFT_1quart = int(np.round(xCenterFFT_1quart)) xCenterFFT_3quart = np.sum((np.arange(len(FFTsmooth_norm) - i_xCenterFFT)) * FFTsmooth_norm[i_xCenterFFT:]) /\ np.sum(FFTsmooth_norm[i_xCenterFFT:]) + i_xCenterFFT+1 i_xCenterFFT_3quart = int(np.round(xCenterFFT_3quart)) FCentroid = Freq1[i_xCenterFFT] Fquart1 = Freq1[i_xCenterFFT_1quart] Fquart3 = Freq1[i_xCenterFFT_3quart] min_peak_height = 0.75 NpeakFFT, ipeaks = sp.find_peaks(FFTsmooth_norm, min_peak_height) npeaks, nb = ipeaks.shape sum_peaks = 0. for i in xrange(npeaks): i1 = ipeaks[i, 0] i2 = ipeaks[i, 1] if i2 > i1: sum_peaks += max(FFTsmooth_norm[i1:i2]) MeanPeaksFFT = sum_peaks / float(npeaks) npts = len(FFTsmooth_norm) E1FFT = np.trapz(FFTsmooth_norm[0:npts / 4], dx=df) E2FFT = np.trapz(FFTsmooth_norm[npts / 4 - 1:2 * npts / 4], dx=df) E3FFT = np.trapz(FFTsmooth_norm[2 * npts / 4 - 1:3 * npts / 4], dx=df) E4FFT = np.trapz(FFTsmooth_norm[3 * npts / 4 - 1:npts], dx=df) moment = np.empty(3, dtype=float) for j in xrange(3): moment[j] = np.sum(Freq1**j * FFTsmooth_norm[0:n / 2]**2) gamma1 = moment[1] / moment[0] gamma2 = np.sqrt(moment[2] / moment[0]) gammas = np.sqrt(np.abs(gamma1**2 - gamma2**2)) return MeanFFT, MaxFFT, FmaxFFT, MedianFFT, VarFFT, FCentroid, Fquart1,\ Fquart3, NpeakFFT, MeanPeaksFFT, E1FFT, E2FFT, E3FFT, E4FFT, gamma1,\ gamma2, gammas