Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    def test_acorr(self):
        cor1 = xcorrf(self.data1, self.data1, self.N // 4, oneside=True)
        cor2 = acorrf(self.data1, self.N // 4)
#        from pylab import plot, show
#        plot(cor1)
#        plot(cor2)
#        show()
        np.testing.assert_array_almost_equal(cor1, cor2, 3)
        self.assertTrue((cor2[0] - 1) ** 2 < 1e-5)
Beispiel #4
0
 def test_acorr(self):
     cor1 = xcorrf(self.data1, self.data1, self.N // 4, oneside=True)
     cor2 = acorrf(self.data1, self.N // 4)
     #        from pylab import plot, show
     #        plot(cor1)
     #        plot(cor2)
     #        show()
     np.testing.assert_array_almost_equal(cor1, cor2, 3)
     self.assertTrue((cor2[0] - 1)**2 < 1e-5)
Beispiel #5
0
    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)
Beispiel #6
0
 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)
Beispiel #7
0
    def test_xcorr(self):
        cor1 = correlate(self.data1, self.data2, 'same')
        cor2 = xcorrf(self.data1.copy(), self.data2.copy(), self.N // 2, demean=False, normalize=False)
#        from pylab import plot, show, subplot
#        subplot(211)
#        plot(data1)
#        plot(data2)
#        subplot(212)
#        plot(cor1)
#        plot(cor2)
#        show()
        np.testing.assert_array_almost_equal(cor1, cor2)
Beispiel #8
0
 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)
Beispiel #9
0
    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)
Beispiel #10
0
 def test_xcorr(self):
     cor1 = correlate(self.data1, self.data2, 'same')
     cor2 = xcorrf(self.data1.copy(),
                   self.data2.copy(),
                   self.N // 2,
                   demean=False,
                   normalize=False)
     #        from pylab import plot, show, subplot
     #        subplot(211)
     #        plot(data1)
     #        plot(data2)
     #        subplot(212)
     #        plot(cor1)
     #        plot(cor2)
     #        show()
     np.testing.assert_array_almost_equal(cor1, cor2)
Beispiel #11
0
def _noisexcorr_traces(tr1, tr2, shift_sec, correlation):
    sr = tr1.stats.sampling_rate
    if 'is_fft' not in tr1.stats or not tr1.stats.is_fft:
        tr1.fft()
    if 'is_fft' not in tr2.stats or not tr2.stats.is_fft:
        tr2.fft()
    cor = xcorrf(tr1.data, tr2.data, int(shift_sec * sr), freq_domain=True,
                 N1=tr1.stats.npts_data, N2=tr2.stats.npts_data,
                 stdev1=tr1.stats.stdev, stdev2=tr2.stats.stdev)

    # mirror cor function so that it displays waves from station1 to
    # station2 for positive lag times
    tr_cor = Trace(data=cor[::-1], header=tr1.stats.copy())
    tr_cor.stats.npts = len(cor)
    tr_cor.stats.sampling_rate = (tr_cor.stats.npts - 1.) / 2 / shift_sec
    tr_cor.stats.station = '-'.join(correlation)
    tr_cor.stats.is_fft = False
    tr_cor.stats.filter += 'IFFTXcorr%s' % shift_sec
    return tr_cor