def test_xcorr_xcorrt(self): N = 1001 data1 = np.sin(np.arange(N // 2 + 1) / 100.) data2 = np.e**(-(np.arange(N) - 500)**2 / 100.) - np.e**( -(np.arange(N) - 50)**2 / 100.) + 5 * np.e**(-(np.arange(N) - 950)**2 / 100.) cor1 = correlate(data1 - np.mean(data1), data2 - np.mean(data2), 'full') cor2 = xcorrt(data2, data1, 750, window=N, ndat2d=N // 2 + 1)[::-1] cor3 = xcorrt(data1, data2, 750, window=N, ndat1d=N // 2 + 1) #@UnusedVariable cor3b = xcorrt(data1, data2, 750, window=0) #@UnusedVariable cor3c = xcorrt(data2, data1, 750, window=0)[::-1] #@UnusedVariable cor1 *= max(cor2) / max(cor1) cor4 = correlate(data2, data1, 'full') cor5 = xcorrt(data2, data1, 750, demean=False) cor5b = xcorrt(data2, data1, 750, shift_zero=-100, demean=False) cor5c = xcorrt(data2, data1, 750, shift_zero=100, demean=False) cor4 *= max(cor5) / max(cor4) cor7 = correlate(data1, data2, 'full') cor8 = xcorrt(data1, data2, 750, demean=False, normalize=False) cor10 = xcorrt(data1, data2, 750, oneside=True, demean=False, normalize=False) #@UnusedVariable # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1, label='scipy.signal all demeaned and normalized') # plot(cor2, label='xcorrt ndat1 > ndat2 ndatxd') # plot(cor3, label='xcorrt ndat1 < ndat2 ndatxd') # plot(cor3b, label='xcorrt ndat1 > ndat2 window = 0') # plot(cor3c, label='xcorrt ndat1 < ndat2 window = 0') # legend() # subplot(413) # plot(cor4, label='scipy.signal all normalized') # plot(cor5, label='xcorrt') # plot(cor5b, label='xcorrt shifted -100') # plot(cor5c, label='xcorrt shifted 100') # legend() # subplot(414) # plot(cor7, label='scipy.signal') # plot(cor8, label='xcorrt') # plot(cor10, label='xcorrt oneside=True') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor4, cor5) np.testing.assert_array_almost_equal(cor7, cor8) np.testing.assert_array_almost_equal(cor5[200:300], cor5b[100:200]) np.testing.assert_array_almost_equal(cor5[200:300], cor5c[300:400])
def test_xcorr_all_again(self): seed(42) data1 = random(1200) * np.sin(np.arange(1200)) * np.arange(1200) - 0.5 data2 = random(1000) * np.sin(np.arange(1000) * 0.4) * 500 - 0.5 data1 -= np.mean(data1[100:1100]) data2 -= np.mean(data2) data2_pad = np.hstack((np.zeros(100), data2, np.zeros(100))) cor1a = correlate(data1, data2, 'valid') cor1a2 = xcorr_obspy(data1, data2_pad, 100) cor1b = xcorrt(data1, data2, 100) cor1c = xcorrf(data1, data2, 100) cor1a *= max(cor1b) / max(cor1a) cor1a2 *= max(cor1b) / max(cor1a2) cor3 = xcorrf(data2, data1, 100)[::-1] #@UnusedVariable cor4 = xcorrt(data1, data2, 100, demean=False, normalize=False) cor5 = xcorrf(data1, data2, 100, demean=False, normalize=False) val = max (cor4) cor4 /= val cor5 /= val cor6 = xcorrt(data1, data2, 100, shift_zero=100) cor7 = xcorrf(data1, data2, 100, shift_zero=100) cor8 = xcorrt(data1, data2, 500, window=200) cor9 = xcorrf(data1, data2, 500, window=200) # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1a, label='convolve') # plot(cor1b, label='xcorrt') # plot(cor1c, label='xcorrf') # plot(cor1a2, label='xcorr_obspy') # plot(cor6, label='xcorrt shift') # plot(cor7, label='xcorrf shift') # legend() # subplot(413) # plot(cor4, label='xcorrt all not demeaned not normalized') # plot(cor5, label='xcorrf') # legend() # subplot(414) # plot(cor8, label='xcorrt window') # plot(cor9, label='xcorrf window') # legend() # show() np.testing.assert_array_almost_equal(cor1a, cor1b) np.testing.assert_array_almost_equal(cor1a, cor1a2) np.testing.assert_array_almost_equal(cor1b, cor1c) np.testing.assert_array_almost_equal(cor1c[20:30], cor7[120:130]) np.testing.assert_array_almost_equal(cor4, cor5) np.testing.assert_array_almost_equal(cor6, cor7) np.testing.assert_array_almost_equal(cor8, cor9)
def test_xcorr_all_again(self): seed(42) data1 = random(1200) * np.sin(np.arange(1200)) * np.arange(1200) - 0.5 data2 = random(1000) * np.sin(np.arange(1000) * 0.4) * 500 - 0.5 data1 -= np.mean(data1[100:1100]) data2 -= np.mean(data2) data2_pad = np.hstack((np.zeros(100), data2, np.zeros(100))) cor1a = correlate(data1, data2, 'valid') cor1a2 = xcorr_obspy(data1, data2_pad, 100) cor1b = xcorrt(data1, data2, 100) cor1c = xcorrf(data1, data2, 100) cor1a *= max(cor1b) / max(cor1a) cor1a2 *= max(cor1b) / max(cor1a2) cor3 = xcorrf(data2, data1, 100)[::-1] #@UnusedVariable cor4 = xcorrt(data1, data2, 100, demean=False, normalize=False) cor5 = xcorrf(data1, data2, 100, demean=False, normalize=False) val = max(cor4) cor4 /= val cor5 /= val cor6 = xcorrt(data1, data2, 100, shift_zero=100) cor7 = xcorrf(data1, data2, 100, shift_zero=100) cor8 = xcorrt(data1, data2, 500, window=200) cor9 = xcorrf(data1, data2, 500, window=200) # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1a, label='convolve') # plot(cor1b, label='xcorrt') # plot(cor1c, label='xcorrf') # plot(cor1a2, label='xcorr_obspy') # plot(cor6, label='xcorrt shift') # plot(cor7, label='xcorrf shift') # legend() # subplot(413) # plot(cor4, label='xcorrt all not demeaned not normalized') # plot(cor5, label='xcorrf') # legend() # subplot(414) # plot(cor8, label='xcorrt window') # plot(cor9, label='xcorrf window') # legend() # show() np.testing.assert_array_almost_equal(cor1a, cor1b) np.testing.assert_array_almost_equal(cor1a, cor1a2) np.testing.assert_array_almost_equal(cor1b, cor1c) np.testing.assert_array_almost_equal(cor1c[20:30], cor7[120:130]) np.testing.assert_array_almost_equal(cor4, cor5) np.testing.assert_array_almost_equal(cor6, cor7) np.testing.assert_array_almost_equal(cor8, cor9)
def test_xcorr_xcorrt(self): N = 1001 data1 = np.sin(np.arange(N // 2 + 1) / 100.) data2 = np.e ** (-(np.arange(N) - 500) ** 2 / 100.) - np.e ** (-(np.arange(N) - 50) ** 2 / 100.) + 5 * np.e ** (-(np.arange(N) - 950) ** 2 / 100.) cor1 = correlate(data1 - np.mean(data1), data2 - np.mean(data2), 'full') cor2 = xcorrt(data2, data1, 750, window=N, ndat2d=N // 2 + 1)[::-1] cor3 = xcorrt(data1, data2, 750, window=N, ndat1d=N // 2 + 1) #@UnusedVariable cor3b = xcorrt(data1, data2, 750, window=0) #@UnusedVariable cor3c = xcorrt(data2, data1, 750, window=0)[::-1] #@UnusedVariable cor1 *= max(cor2) / max(cor1) cor4 = correlate(data2, data1, 'full') cor5 = xcorrt(data2, data1, 750, demean=False) cor5b = xcorrt(data2, data1, 750, shift_zero= -100, demean=False) cor5c = xcorrt(data2, data1, 750, shift_zero=100, demean=False) cor4 *= max(cor5) / max(cor4) cor7 = correlate(data1, data2, 'full') cor8 = xcorrt(data1, data2, 750, demean=False, normalize=False) cor10 = xcorrt(data1, data2, 750, oneside=True, demean=False, normalize=False) #@UnusedVariable # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1, label='scipy.signal all demeaned and normalized') # plot(cor2, label='xcorrt ndat1 > ndat2 ndatxd') # plot(cor3, label='xcorrt ndat1 < ndat2 ndatxd') # plot(cor3b, label='xcorrt ndat1 > ndat2 window = 0') # plot(cor3c, label='xcorrt ndat1 < ndat2 window = 0') # legend() # subplot(413) # plot(cor4, label='scipy.signal all normalized') # plot(cor5, label='xcorrt') # plot(cor5b, label='xcorrt shifted -100') # plot(cor5c, label='xcorrt shifted 100') # legend() # subplot(414) # plot(cor7, label='scipy.signal') # plot(cor8, label='xcorrt') # plot(cor10, label='xcorrt oneside=True') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor4, cor5) np.testing.assert_array_almost_equal(cor7, cor8) np.testing.assert_array_almost_equal(cor5[200:300], cor5b[100:200]) np.testing.assert_array_almost_equal(cor5[200:300], cor5c[300:400])
def test_xcorr_realdata(self): stream = self.stream.copy() data = stream.select(component='Z')[0].data shift = 100 cor1 = xcorrf(data, data[shift:-shift], shift) cor2 = xcorrt(data, data[shift:-shift], shift) # from pylab import plot, show # plot(cor1) # plot(cor2) # show() np.testing.assert_array_almost_equal(cor1, cor2)
def test_xcorr_xcorrf(self): N = 1001 data1 = np.sin(np.arange(N // 2 + 1) / 100.) data2 = np.e**(-(np.arange(N) - 500)**2 / 100.) - np.e**( -(np.arange(N) - 50)**2 / 100.) + 5 * np.e**(-(np.arange(N) - 950)**2 / 100.) cor1 = xcorrt(data1, data2, 750) cor2 = xcorrf(data1, data2, 750) cor3 = xcorrf(data2, data1, 750)[::-1] cor4 = xcorrt(data1, data2, 750, demean=False, normalize=False) cor5 = xcorrf(data1, data2, 750, demean=False, normalize=False) cor6 = xcorrt(data1, data2, 750, shift_zero=100) cor7 = xcorrf(data1, data2, 750, shift_zero=100) cor8 = xcorrt(data1, data2, 750, window=200) cor9 = xcorrf(data1, data2, 750, window=200) # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1, label='xcorrt') # plot(cor2, label='xcorrf ndat1 > ndat2') # plot(cor3, label='xcorrf ndat2 > ndat1') # legend() # subplot(413) # plot(cor4, label='xcorrt all not demeaned not normalized') # plot(cor5, label='xcorrf') # legend() # subplot(414) # plot(cor6, label='xcorrt shift') # plot(cor7, label='xcorrf shift') # plot(cor8, label='xcorrt window') # plot(cor9, label='xcorrf window') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor1, cor3) np.testing.assert_array_almost_equal(cor4, cor5, 5) np.testing.assert_array_almost_equal(cor6, cor7) np.testing.assert_array_almost_equal(cor8, cor9)
def test_xcorr_acorrt(self): data1 = np.sin(np.arange(1001) * 2 * np.pi / 500.) cor1 = xcorrt(data1, data1, 1000) cor2 = acorrt(data1, 1000, oneside=False, clipdata=False) # from pylab import plot, show, subplot, legend # subplot(211) # plot(data1) # subplot(212) # plot(cor1, label='xcorrt') # plot(cor2, label='acorr clip=False') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2)
def test_xcorr_xcorrf(self): N = 1001 data1 = np.sin(np.arange(N // 2 + 1) / 100.) data2 = np.e ** (-(np.arange(N) - 500) ** 2 / 100.) - np.e ** (-(np.arange(N) - 50) ** 2 / 100.) + 5 * np.e ** (-(np.arange(N) - 950) ** 2 / 100.) cor1 = xcorrt(data1, data2, 750) cor2 = xcorrf(data1, data2, 750) cor3 = xcorrf(data2, data1, 750)[::-1] cor4 = xcorrt(data1, data2, 750, demean=False, normalize=False) cor5 = xcorrf(data1, data2, 750, demean=False, normalize=False) cor6 = xcorrt(data1, data2, 750, shift_zero=100) cor7 = xcorrf(data1, data2, 750, shift_zero=100) cor8 = xcorrt(data1, data2, 750, window=200) cor9 = xcorrf(data1, data2, 750, window=200) # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1, label='xcorrt') # plot(cor2, label='xcorrf ndat1 > ndat2') # plot(cor3, label='xcorrf ndat2 > ndat1') # legend() # subplot(413) # plot(cor4, label='xcorrt all not demeaned not normalized') # plot(cor5, label='xcorrf') # legend() # subplot(414) # plot(cor6, label='xcorrt shift') # plot(cor7, label='xcorrf shift') # plot(cor8, label='xcorrt window') # plot(cor9, label='xcorrf window') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor1, cor3) np.testing.assert_array_almost_equal(cor4, cor5, 5) np.testing.assert_array_almost_equal(cor6, cor7) np.testing.assert_array_almost_equal(cor8, cor9)
def deconvt(rsp_list, src, spiking, shift, length=None, normalize=True): """ Time domain deconvolution. Calculate Toeplitz auto-correlation matrix of source, invert it, add noise and multiply it with cross-correlation vector of response and source. In a formula: RF = (STS + spiking*I)^-1 * STR This function calculates RF. N... length ( S0 S-1 S-2 ... S-N+1 ) ( S1 S0 S-1 ... S-N+2 ) S = ( S2 ... ) ( ... ) ( SN-1 ... S0 ) R = (R0 R1 ... RN-1)^T RF = (RF0 RF1 ... RFN-1)^T S... source matrix (shape N*N) R... response vector (length N) RF... receiver function (deconvolution) vector (length N) STS = S^T*S = Toeplitz autocorrelation matrix STR = S^T*R = cross-correlation vector I... Identity :parameters: rsp_list (list of) data containing the response src data of source spiking random noise added to autocorrelation (eg. 1.0, 0.1) shift shift the source by that amount of samples to the left side to get onset in RF at the right time (negative -> shift source to the right side) shift = (middle of rsp window - middle of src window) + (0 - middle rf window) length number of data points of deconvolution normalize if True normalize all deconvolutions so that the maximum of the first deconvolution is 1 :return: (list of) deconvolutions (length N) """ # time_rf = winrf[1]-winrf[0] = length / samp # shift_zero=int(samp*( # (winrsp[1] - winrsp[0] - winsrc[1] + winsrc[0] - time_rf) / 2 # + winrsp[0] - winsrc[0] - winrf[0])) # = shift_zero=int(samp*( # (winrsp[1] + winrsp[0] - winsrc[1] - winsrc[0] -winrf[1] - winrf[0]) / 2 # # if length == None: length = len(src) flag = False RF_list = [] STS = acorrt(src, length, demean=False, clipdata=False) STS[0] += spiking #print shift, len(src), len(rsp_list), spiking, length if not isinstance(rsp_list, (list, tuple)): flag = True rsp_list = [rsp_list] for rsp in rsp_list: STR = xcorrt(rsp, src, length // 2, shift, demean=False) if len(STR) > len(STS): STR = np.delete(STR, -1) RF = toeplitz(STS, STR) RF_list.append(RF) if normalize: norm = 1 / np.max(np.abs(RF_list[0])) for RF in RF_list: RF *= norm if flag: return RF else: return RF_list # comment return commands for testing samp = 50. from pylab import plot, subplot, show subplot(411) plot(np.arange(len(src)) / samp, src) plot(np.arange(len(rsp)) / samp, rsp) subplot(412) plot(np.arange(len(STS)) / samp, STS) subplot(413) plot(np.arange(len(STR)) / samp, STR) subplot(414) plot(np.arange(len(RF)) / samp, RF_list[0]) show()