def test_xcorr_plot(self): st = self.st.copy().detrend('simple').filter( 'bandpass', freqmin=2, freqmax=15) shifts, ccs = align_traces([st[0], st[1]], 40) shift = shifts[1] * st[1].stats.sampling_rate cc = ccs[1] fig = xcorr_plot(template=st[1].data, image=st[0].data, shift=shift, cc=cc, show=False, return_figure=True) return fig
def test_xcorr_plot_cc_vec(self): st = self.st.copy().detrend('simple').filter( 'bandpass', freqmin=2, freqmax=15) cc_vec = normxcorr2( template=st[0].data.astype(np.float32)[40:-40], image=st[1].data.astype(np.float32)) cc_vec = cc_vec[0] fig = xcorr_plot( template=st[1].data[40:-40], image=st[0].data, cc_vec=cc_vec, show=False, return_figure=True) return fig
def align_traces(trace_list, shift_len, master=False, positive=False, plot=False): """ Align traces relative to each other based on their cross-correlation value. Uses the :func:`eqcorrscan.core.match_filter.normxcorr2` function to find the optimum shift to align traces relative to a master event. Either uses a given master to align traces, or uses the trace with the highest MAD amplitude. :type trace_list: list :param trace_list: List of traces to align :type shift_len: int :param shift_len: Length to allow shifting within in samples :type master: obspy.core.trace.Trace :param master: Master trace to align to, if set to False will align to \ the largest amplitude trace (default) :type positive: bool :param positive: Return the maximum positive cross-correlation, or the \ absolute maximum, defaults to False (absolute maximum). :type plot: bool :param plot: If true, will plot each trace aligned with the master. :returns: list of shifts and correlations for best alignment in seconds. :rtype: list """ from eqcorrscan.core.match_filter import normxcorr2 from eqcorrscan.utils.plotting import xcorr_plot traces = deepcopy(trace_list) if not master: # Use trace with largest MAD amplitude as master master = traces[0] MAD_master = np.median(np.abs(master.data)) for i in range(1, len(traces)): if np.median(np.abs(traces[i].data)) > MAD_master: master = traces[i] MAD_master = np.median(np.abs(master.data)) else: print('Using master given by user') shifts = [] ccs = [] for i in range(len(traces)): if not master.stats.sampling_rate == traces[i].stats.sampling_rate: raise ValueError('Sampling rates not the same') cc_vec = normxcorr2(template=traces[i].data.astype( np.float32)[shift_len:-shift_len], image=master.data.astype(np.float32)) cc_vec = cc_vec[0] shift = np.abs(cc_vec).argmax() cc = cc_vec[shift] if plot: xcorr_plot(template=traces[i].data.astype( np.float32)[shift_len:-shift_len], image=master.data.astype(np.float32), shift=shift, cc=cc) shift -= shift_len if cc < 0 and positive: cc = cc_vec.max() shift = cc_vec.argmax() - shift_len shifts.append(shift / master.stats.sampling_rate) ccs.append(cc) return shifts, ccs