-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_single_aux.py
More file actions
151 lines (122 loc) · 3.62 KB
/
plot_single_aux.py
File metadata and controls
151 lines (122 loc) · 3.62 KB
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
# -*- coding: utf-8 -*-
import argparse
from multiprocessing import Pool
import matplotlib.pyplot as plt
import numpy as np
import AuxTDE
from functions.STFT import mSTFT
def parse_cmd_line_arguments():
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"-i", "--init_tau", help="Initial time delay estimates", type=str, default="1",
)
parser.add_argument(
"-m", "--n_mesh", help="# of mesh for plot", type=int, default=300,
)
parser.add_argument(
"-o", "--out_path", type=str, help="Path for save figure", default=None,
)
parser.add_argument(
"-w",
"--n_worker",
help="# of workers for parallel processing",
type=int,
default=4,
)
return parser.parse_args()
def cost_function(args):
x = args[0]
tau = np.array([0, x])
return AuxTDE.cost_function(a, tau, A, phi, w)
def auxiliary_function(args):
x = args[0]
tau = np.array([0, x])
return AuxTDE.auxiliary_function(a, tau, init_tau, A, phi, w)
def mp_init():
import mkl
mkl.set_num_threads(1)
if __name__ == "__main__":
np.random.seed(577)
args = parse_cmd_line_arguments()
global a
global A
global w
global phi
global init_tau
# parameters
l_sig = 2 ** 10
frlen = l_sig
frsft = frlen // 2
n_iter = 30
n_ch = 2
true_tdoa = np.array([1.5, 3])
true_tdoa = np.append(0, true_tdoa)
# simulation
x = np.zeros([l_sig, n_ch])
x[:, 0] = np.random.randn(l_sig)
x1spec = np.fft.rfft(x[:, 0])
freq = np.arange(0, l_sig // 2 + 1)
w = 2 * np.pi * freq / l_sig
# delayed signal
for ch in range(1, n_ch):
tmp = np.fft.rfft(x[:, 0]) * np.exp(1j * w * true_tdoa[ch])
x[:, ch] = np.fft.irfft(tmp)
# STFT
wnd = np.ones(frlen)
X = mSTFT(x, frlen, frsft, wnd, zp=False).transpose(2, 0, 1)
n_freq, n_ch, n_frame = X.shape
# compute variables/parameters
w = 2.0 * np.pi * np.arange(0, n_freq) / frlen
w2 = w ** 2
V = AuxTDE.calc_SCM(X)
A = np.abs(V)
phi = np.angle(V / A)
A /= frlen
A[:, :, 1:-1] *= 2
a = np.ones([n_freq, n_ch, 1])
## Objective function
# set range
tau = np.linspace(-3, 5, args.n_mesh)
n_col = len(X)
# compute objective function
cost = np.zeros([n_col])
with Pool(args.n_worker, initializer=mp_init) as p:
cost = p.map(cost_function, list(zip(np.ravel(tau))))
## auxiliary function
init_tau = np.array(np.append([0], float(args.init_tau)))
# compute auxiliary function
af = np.zeros([n_col])
with Pool(args.n_worker, initializer=mp_init) as p:
af = np.array(p.map(auxiliary_function, list(zip(np.ravel(tau)))))
af = np.ma.masked_where(af < 0, af)
mask = np.ma.masked_where(af >= 0, af)
## plot
mm = 1 / 25.4
figdpi = 300
plt.figure(figsize=(80 * mm, 62 * mm), dpi=figdpi)
# styles
styles = {
"xtick.labelsize": 6,
"ytick.labelsize": 6,
"lines.linewidth": 1,
"axes.labelsize": 9,
"axes.linewidth": 0.5,
"xtick.major.size": 2,
"xtick.major.width": 0.5,
"ytick.major.size": 2,
"ytick.major.width": 0.5,
}
plt.rcParams.update(styles)
# plot
plt.plot(tau, cost, color="#0000ff99")
plt.plot(tau, af, color="#eb3929ff")
plt.plot(tau, mask, color="#ff000000")
plt.xlabel(r"Time delay $\tau$ [sample]")
plt.ylabel("Objective function")
plt.ylim(0,)
plt.tight_layout()
# save or show
if args.out_path is not None:
plt.savefig(args.out_path, dpi=figdpi, bbox_inches="tight")
else:
plt.show()