Python中Cross Correlation信号同步异常问题求助
互相关法信号同步失效问题排查
我尝试对两个形状相同但长度不同的信号做同步,采用互相关(Cross Correlation)方法后得到的最佳滞后约为269000,但实际能让信号完美对齐的最佳滞后应为129000,无法确定互相关失效的原因。
实现代码
def sync_signals(self): accel_path = self.accel_path_text.text() samrate, data = wavfile.read(str(accel_path)) accelData = pd.DataFrame(data) rafale_path = self.rafale_path_text.text() rafaleData = pd.read_csv(rafale_path, delimiter=';', header="infer") stat_path = self.stat_path_text.text() df = pd.read_csv(stat_path) start_time = df.iloc[1][2] end_time = df.iloc[1][3] measurement_time = int(end_time)-int(start_time) temps_entre_points_accel = measurement_time/(len(accelData)) accelData.insert(0, 'Heure', accelData.index * temps_entre_points_accel) def str_to_milliseconds(time_str): # 拆分时间字符串组件 hours, minutes, seconds, milliseconds = map(int, time_str.split(':')) # 计算总毫秒数 total_milliseconds = (hours * 3600 + minutes * 60 + seconds) * 1000 + milliseconds return total_milliseconds rafaleData.insert(0, 'Temps', [str_to_milliseconds(time_str) for time_str in rafaleData.iloc[:, 0]]) # 将信号转为numpy数组 accel_signal = accelData.values[:, 1] accel_time = accelData.values[:, 0] rafale_signal = rafaleData['GAMMAZ (g)'].values rafale_time = rafaleData['Temps'].values # 判断哪个信号更短 if len(accel_signal) < len(rafale_signal): short_signal = accel_signal short_time = accel_time long_signal = rafale_signal long_time = rafale_time else: short_signal = rafale_signal short_time = rafale_time long_signal = accel_signal long_time = accel_time long_time_simulated = np.arange(short_time[0],short_time[len(short_time)-1],20) interpolation_function = interp1d(short_time, short_signal, kind='linear') resampled_signal = interpolation_function(long_time_simulated) print(pd.DataFrame(long_time)) print(pd.DataFrame(long_time_simulated)) shifted_versions = [ (pd.DataFrame(long_signal*0.00390625), pd.DataFrame(resampled_signal)) ] print(shifted_versions) def shift_for_maximum_correlation(x, y): correlation = correlate(x, y, mode="full") lags = correlation_lags(x.size, y.size, mode="full") lag = lags[np.argmax(correlation)] print(f"Best lag: {lag}") if lag < 0: y = y.iloc[abs(lag):].reset_index(drop=True) else: x = x.iloc[lag:].reset_index(drop=True) return x, y def plot_correlation(x, y, text): # 绘制对比图 plt.subplots(figsize=(10, 6)) plt.plot(x) plt.plot(y) plt.legend(loc="best") plt.show() for x, y in shifted_versions: shifted_x, shifted_y = shift_for_maximum_correlation(x, y) plot_correlation(shifted_x, shifted_y, text="after shifting")
相关数据
long_time(长信号时间轴)
0 0.0 1 20.0 2 40.0 3 60.0 4 80.0 ... ... 475245 9504900.0 475246 9504920.0 475247 9504940.0 475248 9504960.0 475249 9504980.0
long_time_simulated(重采样后的短信号时间轴)
0 1742887 1 1742907 2 1742927 3 1742947 4 1742967 ... ... 86236 3467607 86237 3467627 86238 3467647 86239 3467667 86240 3467687
long_signal(长信号数据)
0 0.382812 1 0.371094 2 0.425781 3 0.410156 4 0.503906 ... ... 475245 -0.074219 475246 -0.078125 475247 -0.062500 475248 -0.046875 475249 -0.046875
resampled_signal(重采样后的短信号数据)
0 0.935748 1 0.935736 2 0.935724 3 0.935712 4 0.935700 ... ... 86236 0.164273 86237 0.143966 86238 0.123658 86239 0.103350 86240 0.083042
同步结果
- 当前互相关计算得到的同步信号未对齐
- 无法达到预期的完美同步效果
- 原始未同步信号可作为参考对比
已尝试的排查措施
- 对含噪声的长信号进行滤波处理,无改善
- 更换互相关的不同计算模式,结果依旧不符合预期
内容的提问来源于stack exchange,提问作者gabriel bastier
相关产品推荐
相关产品推荐

