1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| import os import glob import numpy as np from obspy import read from obspy.signal.cross_correlation import correlate import matplotlib.pyplot as plt
folder_path = './processed_sac/' file_pattern = os.path.join(folder_path, '20111122T184816*BHZ*.sac') file_list = sorted(glob.glob(file_pattern))
lowcut = 0.05 highcut = 2.0 filter_params = {'lowcut': lowcut, 'highcut': highcut, 'corners': 4, 'method': 'bandpass'}
data_list = []
def calculate_time_shift(signal1, signal2, dt, max_shift_seconds=10): """ 计算两个信号之间的时移,返回时移(秒)。 参数 max_shift_seconds 控制最大允许的滞后量。 """ max_shift_samples = int(max_shift_seconds / dt) correlation = correlate(signal1, signal2, max_shift_samples, demean=True, normalize=True) lag = np.argmax(correlation) - max_shift_samples lag_time = lag * dt return lag_time
for file in file_list: st = read(file) tr = st[0] data = tr.data dt = tr.stats.delta if np.all(data == 0): print(f"{file} 数据全为零,跳过。") continue tr.filter(type='bandpass', freqmin=lowcut, freqmax=highcut, corners=filter_params['corners']) filtered_data = tr.data depmin = np.min(filtered_data) depmax = np.max(filtered_data) if depmax != depmin: normalized_data = (filtered_data) / (depmax - depmin) else: normalized_data = filtered_data data_list.append(normalized_data)
data=data_list base_signal = data_list[0] time_shifts = []
for i, signal in enumerate(data_list[1:], start=1): time_shift = calculate_time_shift(base_signal, signal, dt) time_shifts.append(time_shift) print(f"信号 {i+1} 与基准信号的时移: {time_shift:.4f} 秒") shift_samples = int(round(time_shift / dt)) if shift_samples > 0: shifted_signal = np.concatenate((np.zeros(shift_samples), signal[:-shift_samples])) elif shift_samples < 0: shifted_signal = np.concatenate((signal[-shift_samples:], np.zeros(-shift_samples))) else: shifted_signal = signal data_list[i] = shifted_signal
data_array = np.array(data_list)
print(data_array.shape) import matplotlib.pyplot as plt import sequencer objects_list_simulated = np.vstack(data_array) num_rows = objects_list_simulated.shape[0] print(num_rows) shuffled_indices = np.random.permutation(num_rows)
print(shuffled_indices) objects_list_shuffled = objects_list_simulated[shuffled_indices, :]
objects_list_shuffled = objects_list_shuffled+1.0
plt.figure(1, figsize=(10, 5)) plt.subplot(1,2,1) plt.title("input dataset [lines]") for j, object_data in enumerate(data): grid = np.arange(len(object_data)) object_data_scaled_y = object_data + (j-1) *1.0 plt.plot(grid, object_data_scaled_y) plt.xlabel("x") plt.ylabel("scaled intensity")
plt.subplot(1,2,2) plt.title("input dataset [heat]") plt.pcolormesh(objects_list_shuffled,cmap='inferno') plt.colorbar() plt.xlabel("x") plt.ylabel("original index") plt.tight_layout()
output_path = "sequencer_output_directory" if not os.path.exists(output_path): os.makedirs(output_path)
estimator_list = ['EMD', 'energy', 'L2'] seq = sequencer.Sequencer(grid, objects_list_shuffled, estimator_list)
final_elongation, final_sequence = seq.execute(output_path)
estimator_name = 'EMD' scale = 1 print("Intermediate elongation for metric=%s and scale=%s: %s" % (estimator_name, scale, seq.return_elongation_of_weighted_products(estimator_name, scale)))
estimator_list, scale_list, elongation_list = seq.return_elongation_of_weighted_products_all_metrics_and_scales() for i in range(len(estimator_list)): print("metric=%s, scale=%s, elongation: %s" % (estimator_list[i], scale_list[i], elongation_list[i]))
estimator_list, scale_list, sequence_list = seq.return_sequence_of_weighted_products_all_metrics_and_scales()
plt.figure(1, figsize=(16, 16)) plt.subplot(2, 2, 1)
for j, object_data in enumerate(objects_list_shuffled): grid = np.arange(len(object_data)) object_data_scaled_y = object_data + (j-1) * 1 plt.plot(grid, object_data_scaled_y) plt.xlabel("x") plt.ylabel("Original")
plt.subplot(2, 2, 2) plt.title("ordered according to\n intermediate sequence using: \n metric=%s, scale=%s" % (estimator_list[0], scale_list[0])) sequnce = sequence_list[0]
for j, object_data in enumerate(objects_list_shuffled[sequnce]): grid = np.arange(len(object_data)) object_data_scaled_y = object_data + (j-1) * 1 plt.plot(grid, object_data_scaled_y) plt.xlabel("x") plt.ylabel("Original")
plt.subplot(2, 2, 3) plt.title("ordered according to\n intermediate sequence using: \n metric=%s, scale=%s" % (estimator_list[1], scale_list[1])) sequnce = sequence_list[1]
for j, object_data in enumerate(objects_list_shuffled[sequnce]): grid = np.arange(len(object_data)) object_data_scaled_y = object_data + (j-1) * 1 plt.plot(grid, object_data_scaled_y) plt.xlabel("x") plt.ylabel("Original")
plt.subplot(2, 2, 4) plt.title("ordered according to\n intermediate sequence using: \n metric=%s, scale=%s" % (estimator_list[2], scale_list[2])) sequnce = sequence_list[2]
for j, object_data in enumerate(objects_list_shuffled[sequnce]): grid = np.arange(len(object_data)) object_data_scaled_y = object_data + (j-1) * 1 plt.plot(grid, object_data_scaled_y) plt.xlabel("x") plt.ylabel("Original")
plt.tight_layout()
|