Measuring eGf signal-to-noise ratio and pulse width
from obspy import read
from obspy import UTCDateTime
from obspy.clients.fdsn import Client
from obspy.geodetics.base import gps2dist_azimuth
from obspy.signal.rotate import rotate_ne_rt
from obspy.signal.rotate import rotate_rt_ne
from obspy.signal.rotate import rotate2zne
import obspy as ob
print("# obspy version = ",ob.__version__)
import numpy as np
import scipy as sp
import matplotlib as mpl
print("# numpy version = ",np.__version__)
print("# scipy version = ",sp.__version__)
print("# matplotlib version = ",mpl.__version__)
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib.dates as mdates
from scipy import signal
from scipy import ndimage
from scipy.fftpack import fft, ifft
from scipy.linalg import norm
from scipy.stats import pearsonr
from scipy.stats import spearmanr
from scipy.stats import kendalltau
import sys
import os
# font size
SMALL_SIZE = 16
MEDIUM_SIZE = 18
BIGGER_SIZE = 20
#SMALL_SIZE = 32
#MEDIUM_SIZE = 32
#BIGGER_SIZE = 36
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
MRF file in SAC format has 1kHz sampling (interpolated with SAC). I may includ the interpolation part in here or use "raw" MRF file (most of them are 100Hz sampling data)
#st = read("http://ncedc.org/ftp/outgoing/taira/eGf/CLC.stack.sac")
st = read("http://ncedc.org/ftp/outgoing/taira/eGf/stack_indep0.001.sac")
#print(st)
_plot = st.plot(size=(1000,300))
#print(st[0].stats)
tr = st[0]
tr.stats
sta = tr.stats.station
print("# sta = ", sta)
#print(tr.data)
sampling_rateOUT = int(np.round(tr.stats.sampling_rate))
print("# sampling_rateOUT = ", sampling_rateOUT)
#plt.plot(tr.times(), tr.data)
tw is the length of window, st_signal = starting time of signal, st_noise = starting time of noise
delay = 20.0 # sec where we will see MRF onset
tw = 5.0 # sec window lengh of signal and noise.
# signal part 19.0-24.0
st_signal = 19.0 # sec
et_signal = st_signal + tw # sec
# noise part 5.0-10.sec
st_noise = 5.0 # sec
et_noise = st_noise + tw # sec
time = tr.times() #
signal_time_array = time[0:int(tw*sampling_rateOUT )]
noise_time_array = time[0:int(tw*sampling_rateOUT )]
#st_signal / sampling_rateOUT
st_signal_array = int(sampling_rateOUT * st_signal)
et_signal_array = int(sampling_rateOUT * et_signal)
print("# st_signal_array = ", st_signal_array)
print("# et_signal_array = ", et_signal_array)
st_noise_array = int(sampling_rateOUT * st_noise)
et_noise_array = int(sampling_rateOUT * et_noise)
print("# st_noise_array = ", st_noise_array)
print("# et_noise_array = ", et_noise_array)
signal = tr.data[st_signal_array:et_signal_array]
noise = tr.data[st_noise_array:et_noise_array]
#print(len(signal))
#print(len(noise))
signal_amp = norm(signal, 1)/len(signal)
noise_amp = norm(noise, 1)/len(noise)
amp_ratio = signal_amp / noise_amp
print("# amp_ratio = ", amp_ratio)
amp_ratio_out = "{:.3f}".format(amp_ratio)
print("# amp_ratio_out = ", amp_ratio_out)
max_pos = np.argmax(signal)
print("# max_pos = ", max_pos)
min_pos = np.argmin(signal)
print("# min_pos = ", min_pos)
#print(time)
zero_crossings = np.where(np.diff(np.sign(signal)))[0]
#print(zero_crossings)
This would be the end of MRF pulse
post_cross = zero_crossings[np.where(zero_crossings > max_pos)][0]
print("# post_cross = ", post_cross)
This would be the start of MRF pulse
pre_cross = zero_crossings[np.where(zero_crossings < max_pos)][-1]
print("# pre_cross = ", pre_cross)
pulse_width = (post_cross - pre_cross)/sampling_rateOUT
print("# pulse_width = ", pulse_width, "sec")
pulse_width_out = "{:.3f}".format(pulse_width)
print("# pulse_width_out = ", pulse_width_out, "sec")
ymin = signal[min_pos] * 1.15
ymax = signal[max_pos] * 1.15
plt.figure(figsize=(18, 4))
plt.plot(signal_time_array, signal, label="signal")
plt.plot(noise_time_array, noise, label="noise")
plt.vlines(x=pre_cross/sampling_rateOUT , ymin=ymin, ymax=ymax, color="black", linewidth = 1.25, linestyle = "--")
plt.vlines(x=post_cross/sampling_rateOUT , ymin=ymin, ymax=ymax, color="black", linewidth = 1.25, linestyle = "--")
plt.xlim([0, tw])
plt.ylim([ymin, ymax])
plt.title(sta+" amp_ratio = "+amp_ratio_out+" pulse_width = "+pulse_width_out+"sec")
plt.ylabel("MRF amplitude")
plt.xlabel("Elapsed time (sec)")
plt.legend(loc = "upper right")