Reviewed-on: #13 Co-authored-by: Janis Heuel <janis.heuel@ruhr-uni-bochum.de> Co-committed-by: Janis Heuel <janis.heuel@ruhr-uni-bochum.de>
4.9 KiB
4.9 KiB
In [ ]:
# Load all required packages
import os
import numpy as np
from scipy.signal import stft, istft
import matplotlib.pyplot as pltIn [ ]:
# Load our earthquake data and plot
d = np.load(os.path.join(os.path.expanduser('~'),'work', 'data', 'events', 'bug2019mgoh_Z.npz'))
#d = np.load(os.path.join(os.path.expanduser('~'),'work', 'data', 'events', 'bug2019gbbo_Z.npz'))
#d = np.load(os.path.join(os.path.expanduser('~'),'work', 'data', 'events', 'bug2019ibsd_Z.npz'))
plt.figure(figsize=(15, 8))
plt.plot(d['data']);In [ ]:
# Compute spectrogram of data
f, t, X = stft(d['data'], fs=1/0.01, nfft=198, nperseg=99) # Be careful with choice of nfft and nperseg,
# because istft does not work for all pairs
print(X.shape)
plt.figure(figsize=(15, 8))
plt.pcolormesh(t, f, np.abs(X))
plt.xlabel("Frequency (Hz)")
plt.ylabel("Time (s)");In [ ]:
# Functions for thresholding
def threshold(X, quantile=0.99):
"""
X: time-frequency coefficients
q: quantile [0-1]
"""
# Loop over frequencies to build threshold function
beta = np.zeros(X.shape[0])
for i in range(X.shape[0]):
beta[i] = np.quantile(np.abs(X[i, :]), q=quantile)
return beta
def modify_spectrogram(X, beta):
# Loop over all items in X and apply threshold
# Task: modify X!
return XIn [ ]:
# Compute STFT of data before first arrival of P
_, _, X_thres = stft(d['data'][:2500], fs=1/0.01, nfft=198, nperseg=99)
# Estimate threshold fucntion
beta = threshold(X_thres)
# Modifiy original spectrogram
X_mod = modify_spectrogram(X, beta)
# Plot modified spectrogram
plt.figure(figsize=(15, 8))
plt.pcolormesh(t, f, np.abs(X_mod))
plt.xlabel("Frequency (Hz)")
plt.ylabel("Time (s)");In [ ]:
# Inverse STFT of modified spectrogram
t, x_mod = istft(X_mod, fs=1/0.01, nfft=198, nperseg=99)
# Plot corrected seismogram
plt.figure(figsize=(15, 8))
plt.plot(t, x_mod);In [ ]: