-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCross_Correlation_Method.py
57 lines (34 loc) · 1.74 KB
/
Cross_Correlation_Method.py
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
def Cross_Correlate(Reference, Waveform):
Reference_X = [round(float(t),10) for t,v in zip(Reference[:,0],Reference[:,1])]
Reference_Y = norm([v for t,v in zip(Reference[:,0],Reference[:,1])])
sr_ref = round((Reference_X[1] - Reference_X[0])**(-1))
sr_signal = round((Waveform[1,0] - Waveform[0,0])**(-1))
if sr_signal != sr_ref:
Ref, Wave, sr = resample_to_match(Reference,Waveform)
Ref_X = Ref[:,0]
Ref_Y = Ref[:,1]
X = [round(float(t),10) for t in Wave[:,0]]
Y_short = norm(Wave[:,1])
Y_ref = np.append(Ref_Y, [y for y in np.zeros(1 + int((max(X) - max(Ref_X))*sr))])
Y_corr = np.append([y for y in np.zeros(int((min(X))*sr))], Y_short)
corr = signal.correlate(Y_corr, Y_ref, mode="full")
corr = np.insert(corr, 0, 0.0)
corr = norm(corr)
n = len(Y_corr)
delay_array = np.linspace(-n/sr, n/sr, 2*n)
arrival = (delay_array[np.argmax(corr)])
return arrival, corr, delay_array, Ref, Wave
else:
Ref_X = Reference[:,0]
Ref_Y = Reference[:,1]
X = [round(float(t),10) for t in Waveform[:,0]]
Y_short = norm(Waveform[:,1])
Y_ref = np.append(Reference_Y, [y for y in np.zeros(1 + int((max(X) - max(Ref_X))*sr_ref))])
Y_corr = np.append([y for y in np.zeros(int((min(X))*sr_ref))], Y_short)
corr = signal.correlate(Y_corr, Y_ref, mode="full")
corr = np.insert(corr, 0, 0.0)
corr = norm(corr)
n = len(Y_corr)
delay_array = np.linspace(-n/sr_ref, n/sr_ref, 2*n)
arrival = (delay_array[np.argmax(corr)])
return arrival, corr, delay_array, Reference, Waveform