def get_fft(evnm,p_before, p_after,stns='001',ext='[E,N,Z]'): #Reading in the Events st = readandrotate(evnm,stns=stns,ext='[E,N,Z]') # Removing this we are going to use multiple traces and stack them ##Get the P components #st = get_p(st) #assert len(st) ==1 # Calculate P picks st_filt = st.copy() st_filt = st.filter('bandpass',freqmin=20.0,freqmax=200.0) p_pick, s_picks = picker(st_filt) #Set P picks to sac header this is probably bad style if not(np.isnan(p_pick)): for tr in st: tr.stats.sac.t0 = p_pick else: for tr in st: tr.stats.sac.t0 = -12345.0 # Slice of the P-wave windows st_p = window_trace(st,p_before,p_after) assert len(st_p) != 0 window_len_time = p_before + p_after # Calculates the noise window # Note uses the beginning of the trace if the P wave is near the beginning it will overlap st_n = noise_window_trace(st,window_len_time) #Calulate power spectral density of P,Noise for event and reference fft_p = np.zeros(len(st_n[0])+1) fft_n = np.zeros(len(st_n[0])+1) for i in range(3): fft_p_cmp,freq = calc_mtspec(st_p[i]) fft_n_cmp,freq = calc_mtspec(st_n[i]) fft_p += fft_p_cmp fft_n += fft_n_cmp return(fft_p,fft_n,freq,st,st_p,st_n)
def dts_p(evnm1,evnm_ref,fmin,fmax,snr_limit,station='001',p_before=0.1,p_after=0.2,diagnostic_plot=False): output_linefit=True #Reading in the Events st = readandrotate(evnm1,stns=station,ext='[E,N,Z]') st_ref = readandrotate(evnm_ref,stns=station,ext='[E,N,Z]') #Get the P components st = get_p(st) st_ref = get_p(st_ref) assert len(st) ==1 assert len(st_ref) ==1 # Slice of the P-wave windows st_p = window_trace(st,p_before,p_after) st_ref_p = window_trace(st_ref,p_before,p_after) assert len(st_p) != 0 assert len(st_ref_p) != 0 # Finds the length of P window in samples and checks all the windows are the same length window_len_samples = get_and_check_window_len(st_p,st_ref_p) window_len_time = p_before + p_after # Calculates the noise window # Note uses the beginning of the trace if the P wave is near the beginning it will overlap st_n = noise_window_trace(st,window_len_time) st_ref_n = noise_window_trace(st,window_len_time) #Calulate power spectral density of P,Noise for event and reference fft_p,freq = calc_mtspec(st_p[0]) fft_n,freq = calc_mtspec(st_n[0]) fft_ref_p,freq = calc_mtspec(st_ref_p[0]) fft_ref_n,freq = calc_mtspec(st_ref_n[0]) meas_tstar,meas_tstar_error,linefit = bs_lsr(fft_p, fft_ref_p,freq,fmin,fmax,fft_n,fft_ref_n,snr_limit=snr_limit) if diagnostic_plot: plt.figure(figsize=(8,12)) # --------------- # plt.subplot(5,2,1) plt.title('Event Whole Trace') plt.plot(st[0].times(),st[0].data) ppick = st[0].stats.sac.t0 plt.axvspan(ppick-p_before,ppick+p_after,color='r',alpha=0.5) plt.xlabel('Time (sec)') plt.ylabel('Amplitude') # --------------- # plt.subplot(5,2,2) plt.title('Reference Whole Trace') plt.plot(st_ref[0].times(),st_ref[0].data,'g') ppick = st_ref[0].stats.sac.t0 plt.axvspan(ppick-p_before,ppick+p_after,color='r',alpha=0.5) plt.xlabel('Time (sec)') plt.ylabel('Amplitude') # --------------- # plt.subplot(6,2,3) plt.title('Trace Window') plt.plot(st_p[0].times(),st_p[0].data,label='P-wave') plt.plot(st_n[0].times(),st_n[0].data,'r',label='Noise') plt.xlabel('Time (sec)') plt.ylabel('Amplitude') # --------------- # plt.subplot(6,2,4) plt.title('Reference Window') plt.plot(st_ref_p[0].times(),st_ref_p[0].data,'g',label='P-wave') plt.plot(st_ref_n[0].times(),st_ref_n[0].data,'r',label='Noise') plt.xlabel('Time (sec)') plt.ylabel('Amplitude') # --------------- # plt.subplot(6,2,5) plt.title('Trace FFT') plt.semilogy(freq,fft_p) plt.semilogy(freq,fft_n,'r') plt.xlabel('Freq (Hz)') plt.ylabel('Amplitude') # --------------- # plt.subplot(6,2,6) plt.title('Reference FFT') plt.semilogy(freq,fft_ref_p,'g') plt.semilogy(freq,fft_ref_n,'r') plt.xlabel('Freq (Hz)') plt.ylabel('Amplitude') # --------------- # plt.subplot(6,2,7) plt.title('Log-spectral-ratio') plt.plot(freq,np.log(fft_p/fft_ref_p)) plt.axvspan(fmin,fmax,color='r',alpha=0.5) plt.xlabel('Freq (Hz)') plt.ylabel('LSR') ind = (freq>fmin) & (freq<fmax) linefit_freq = freq[ind] line = linefit_freq*linefit.params[0]+linefit.params[1] plt.plot(linefit_freq,line,'-w') # --------------- # plt.subplot(6,2,8) plt.title('Signal/Noise') snr=fft_p/fft_n snr_ref=fft_ref_p/fft_ref_n plt.semilogy(freq,snr) plt.semilogy(freq,snr_ref,'g') plt.axhline(snr_limit,color='r',ls='--') plt.xlabel('Freq (Hz)') plt.ylabel('SNR') # --------------- # # --------------- # plt.subplot(3,1,3) plt.title('LSR in frequency band') plt.plot(freq,np.log(fft_p/fft_ref_p)) plt.axvspan(fmin,fmax,color='r',alpha=0.5) plt.xlabel('Freq (Hz)') plt.ylabel('LSR') plt.plot(linefit_freq,line,'-w') extra = 0.1*(fmax-fmin) plt.xlim(fmin-extra,fmax+extra) #TODO autoscale y axis # --------------- # plt.tight_layout() return meas_tstar,meas_tstar_error,linefit
def mean_corr(evnm1,evnm2): """Calculate the mean correlation between two events""" st = readandrotate(evnm1,stns='*',ext='[E,N,Z]') st2 = readandrotate(evnm2,stns='*',ext='[E,N,Z]')