-
Notifications
You must be signed in to change notification settings - Fork 1
/
TDE.py
117 lines (89 loc) · 2.64 KB
/
TDE.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
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
# -*- coding: utf-8 -*-
import numpy as np
def GCC(xspec):
"""
Compute a time delay estimate based on discrete GCC.
parameters
----------
xspec: array_like (n_freq, )
Cross-spectrum
return
----------
discrete sample time delay estimate
"""
# Compute cross correlation
corr = np.real(np.fft.irfft(xspec))
# Get and return the location of its maximum
return np.argmax(corr)
def GCC_with_parafit(xspec, ret_GCC=False):
"""
Compute the sub-sample estimate by the quadratic interpolation of GCC.
parameters
----------
xspec: array_like (n_freq, )
Cross-spectrum
ret_GCC: bool, optional
If True, return the discrete estimate too
return
----------
sub-sample time delay estimate
if ret_GCC: discrete estimate too
"""
# Compute cross correlation
corr = np.real(np.fft.irfft(xspec))
# Get location of its maximum
t_max = np.argmax(corr)
# Get the value of three points around the maximum
if t_max == corr.shape[0] - 1:
y_vec = [corr[t_max - 1], corr[t_max], corr[0]]
else:
y_vec = [corr[t_max - 1], corr[t_max], corr[t_max + 1]]
# return
if ret_GCC:
return parafit(t_max, y_vec), t_max
else:
return parafit(t_max, y_vec)
def parafit(x_2, y_vec):
"""
parabolic interpolation: compute x coordinate of the vertex
of the quadratic function determined by the given three coordinates.
parameters
----------
x_2: float
Discrete x coordinate of the extremum, i.e., x_2 in the three points [x_1, x_2, x_3]
x_2 must be the median of the three adjacent discrete points
y_vec: array_like (3, )
y coordinates for the interpolation, y_vec = [y_1, y_2, y_3]'
return
----------
x coordinate of the vertex
"""
x_vtx = -0.5 * (y_vec[2] - y_vec[0]) / (y_vec[2] - 2 * y_vec[1] + y_vec[0])
return x_vtx + x_2
def para_coef(x, y):
"""
Compute coefficients of a quadratic function
parameters
----------
x: array_like (3, )
x coordinates for the interpolation, x = [x_1, x_2, x_3]'
y: array_like (3, )
y coordinates for the interpolation, y = [y_1, y_2, y_3]'
return
----------
a, b, c: float
coefficients of the quadratic function
"""
x32 = x[2] - x[1]
x32p = x[2] + x[1]
x31 = x[2] - x[0]
x12 = x[0] - x[1]
y32 = y[2] - y[1]
y31 = y[2] - y[0]
a = (x32 * y31 - x31 * y32) / x31 * x32 * x12
b = y32 / x32 - a * x32p
c = y[0] - a * x[0] ^ 2 - b * x[0]
return a, b, c
def para_func(x, a, b, c):
y = a * x ** 2 + b * x + c
return y