def test_xcorr(self): """ """ # example 1 - all samples are equal np.random.seed(815) # make test reproducible tr1 = np.random.randn(10000).astype(np.float32) tr2 = tr1.copy() shift, corr = xcorr(tr1, tr2, 100) self.assertEqual(shift, 0) self.assertAlmostEqual(corr, 1, 2) # example 2 - all samples are different tr1 = np.ones(10000, dtype=np.float32) tr2 = np.zeros(10000, dtype=np.float32) shift, corr = xcorr(tr1, tr2, 100) self.assertEqual(shift, 0) self.assertAlmostEqual(corr, 0, 2) # example 3 - shift of 10 samples tr1 = np.random.randn(10000).astype(np.float32) tr2 = np.concatenate((np.zeros(10), tr1[0:-10])) shift, corr = xcorr(tr1, tr2, 100) self.assertEqual(shift, -10) self.assertAlmostEqual(corr, 1, 2) shift, corr = xcorr(tr2, tr1, 100) self.assertEqual(shift, 10) self.assertAlmostEqual(corr, 1, 2) # example 4 - shift of 10 samples + small sine disturbance tr1 = (np.random.randn(10000) * 100).astype(np.float32) var = np.sin(np.arange(10000, dtype=np.float32) * 0.1) tr2 = np.concatenate((np.zeros(10), tr1[0:-10])) * 0.9 tr2 += var shift, corr = xcorr(tr1, tr2, 100) self.assertEqual(shift, -10) self.assertAlmostEqual(corr, 1, 2) shift, corr = xcorr(tr2, tr1, 100) self.assertEqual(shift, 10) self.assertAlmostEqual(corr, 1, 2)
def test_xcorr(self): """ """ # example 1 - all samples are equal np.random.seed(815) # make test reproducable tr1 = np.random.randn(10000).astype('float32') tr2 = tr1.copy() shift, corr = xcorr(tr1, tr2, 100) self.assertEquals(shift, 0) self.assertAlmostEqual(corr, 1, 2) # example 2 - all samples are different tr1 = np.ones(10000, dtype='float32') tr2 = np.zeros(10000, dtype='float32') shift, corr = xcorr(tr1, tr2, 100) self.assertEquals(shift, 0) self.assertAlmostEqual(corr, 0, 2) # example 3 - shift of 10 samples tr1 = np.random.randn(10000).astype('float32') tr2 = np.concatenate((np.zeros(10), tr1[0:-10])) shift, corr = xcorr(tr1, tr2, 100) self.assertEquals(shift, -10) self.assertAlmostEqual(corr, 1, 2) shift, corr = xcorr(tr2, tr1, 100) self.assertEquals(shift, 10) self.assertAlmostEqual(corr, 1, 2) # example 4 - shift of 10 samples + small sine disturbance tr1 = (np.random.randn(10000) * 100).astype('float32') var = np.sin(np.arange(10000, dtype='float32') * 0.1) tr2 = np.concatenate((np.zeros(10), tr1[0:-10])) * 0.9 tr2 += var shift, corr = xcorr(tr1, tr2, 100) self.assertEquals(shift, -10) self.assertAlmostEqual(corr, 1, 2) shift, corr = xcorr(tr2, tr1, 100) self.assertEquals(shift, 10) self.assertAlmostEqual(corr, 1, 2)
continue sta_name_split = sta_name.split('.') real_tr = read(os.path.join(real_add, ev_name, 'BH', 'dis.' + sta_name_split[1] + '.' + sta_name_split[2] + '.' + sta_name_split[-1]))[0] mfi_tr = convSTF(grf_tr, STF_tr, grp) # preprocessing all three waveforms mfi_tr = preprocess(tr=mfi_tr, lfreq=lfreq, hfreq=hfreq) real_tr = preprocess(tr=real_tr, lfreq=lfreq, hfreq=hfreq) print 'WARNING: you are using resample!!!' real_tr.resample(mfi_tr.stats.sampling_rate) real_tr = real_tr.slice(ev_time + phase_time - tb, ev_time + phase_time + ta) mfi_tr = mfi_tr.slice(ev_time + phase_time - tb, ev_time + phase_time + ta) np_xcorr, fac_xcorr = xcorr(mfi_tr.data, real_tr.data, int(15.*real_tr.stats.sampling_rate)) print fac_xcorr sys.stdout.flush() if fac_xcorr < min_xcorr: print '.', continue if np_xcorr > int(1.*real_tr.stats.sampling_rate): print '.', continue print '\n******************' print sta_name print 'Sampling rate difference: ', print real_tr.stats.sampling_rate - mfi_tr.stats.sampling_rate print 'xcorr: ', print np_xcorr, fac_xcorr