【发布时间】:2018-05-20 13:18:11
【问题描述】:
我有两个时间信号,每个信号都包含两个相同的脉冲,但位置不同。
描述两个信号的图片:
如何使用 python 获得每个脉冲的两个信号之间的时间偏移? 互相关似乎不是完成这项工作的可靠方法...... 你可以在那里看到互相关函数和我想恢复的两个时移:
如果我们只有一个脉冲,虽然时间偏移是从互相关函数的最大值完美获得的,但您可以看到它在多个脉冲的情况下没有多大帮助。
这是我的测试程序:
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
N = 200 # Number of points in initial, unshifted signals
N_pad = 500 # Total number of points at the end
t = np.linspace(-1, 1, N) # Dummy time vector
dt = t[1]-t[0] # Time step
Fs = 1.0/dt # Sampling frequency
pulse1 = signal.gausspulse(t, fc=5) # Create a pulse at 5 Hz
pulse2 = signal.gausspulse(t, fc=8) # Create a pulse at 8 Hz
# Shift and pad the pulses
pulse1_shifted = np.concatenate((pulse1,np.zeros(50)), axis=0)
pulse2_shifted = np.concatenate((pulse2,np.zeros(100)), axis=0)
pulse1_shifted_padded = np.concatenate((np.zeros(N_pad-len(pulse1_shifted)),pulse1_shifted), axis=0)
pulse2_shifted_padded = np.concatenate((np.zeros(N_pad-len(pulse2_shifted)),pulse2_shifted), axis=0)
# Create signal 1 as the sum of the two pulses
sig1 = pulse1_shifted_padded + pulse2_shifted_padded
# Different time shift
pulse1_shifted = np.concatenate((pulse1,np.zeros(60)), axis=0)
pulse2_shifted = np.concatenate((pulse2,np.zeros(150)), axis=0)
pulse1_shifted_padded = np.concatenate((np.zeros(N_pad-len(pulse1_shifted)),pulse1_shifted), axis=0)
pulse2_shifted_padded = np.concatenate((np.zeros(N_pad-len(pulse2_shifted)),pulse2_shifted), axis=0)
# Create signal 2 as the sum of the two pulses
sig2 = pulse1_shifted_padded + pulse2_shifted_padded
# Create new time vector at the same sampling rate
t = np.arange(dt*N_pad,step=dt)
# Plot the two signals
plt.figure()
plt.plot(t,sig1,label="Signal 1")
plt.plot(t,sig2,label="Signal 2")
plt.legend()
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("The two signals. Orange and blue has been recorded at 100 m distance")
# Plot the cross correlation between the two signals
corr = signal.correlate(sig1,sig2)
dt = np.arange(1-N_pad,N_pad)/Fs # Time shift vector
plt.figure()
plt.plot(dt,corr)
plt.plot([0.1,0.1],[-20,20],"--")
plt.plot([0.5,0.5],[-20,20],"--")
plt.ylim([-15,15])
plt.xlabel("Time shift (s)")
plt.ylabel("Cross correlation function")
你有解决办法吗?
非常感谢
【问题讨论】:
-
如果脉冲具有与您的示例中不同的频率基础分量,那么它们可能更容易被 FFT 分离
标签: python signal-processing cross-correlation