-
Notifications
You must be signed in to change notification settings - Fork 0
/
_tests.py
executable file
·201 lines (175 loc) · 6.73 KB
/
_tests.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
# Copyright 2018 Kabir Marwah
# (Please add yourself if you make changes)
#
# This file is part of doamusic.
#
# doamusic is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# doamusic is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with doamusic. If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import, division, print_function
import cProfile
import sys
from time import time
import itertools
import numpy as np
import scipy as sp
import scipy.misc
import scipy.constants
from scipy import pi
if __name__ == "__main__" and __package__ is None:
sys.path.append('..')
__package__ = "doamusic"
import doamusic
from doamusic import music
from doamusic import _music
from doamusic import util
# 16 element unit circle in the y-z plane
antx = sp.arange(16)
circarray = sp.array([0*antx, sp.cos(antx), sp.sin(antx)]).T
# 3 offset circles in planes parallel to y-z
front = circarray + [1,0,0]
back = circarray - [1,0,0]
triplecircarray = sp.concatenate((front,circarray,back))
# unit spacing grid (5x5)
gridarray = sp.array(
[ (0,y,z) for y,z in itertools.product(range(-3,4),repeat=2) ]
)
# unit spacing linear
linarray = sp.array( [ (0,y,0) for y in range(10) ] )
# Arrays as constructed.
operating_frequency = 2.477e9
wavelength = scipy.constants.c/operating_frequency
randarray = sp.loadtxt("arrays/randarray.dat")/wavelength
ourlinarray = sp.loadtxt("arrays/linarray.dat")/wavelength
ourcircarray = sp.loadtxt("arrays/circarray.dat")/wavelength
#Test Parameters
ants = randarray
nsamp = 2000
snr = -30
s1_aoa_deg = [80, 0]
s2_aoa_deg = [120, 0]
s1_aoa = (sp.deg2rad(s1_aoa_deg[0]),sp.deg2rad(s1_aoa_deg[1]))
#s2_aoa = (pi/2 + sp.randn()/2, sp.randn()/2)
s2_aoa = (sp.deg2rad(s2_aoa_deg[0]),sp.deg2rad(s2_aoa_deg[1]))
s1 = util.makesamples(ants,s1_aoa[0],s1_aoa[1],nsamp)
s2 = util.makesamples(ants,s2_aoa[0],s2_aoa[1],nsamp)
samples = s2 + s1
samples = util.awgn(samples,snr)
# add noise to s1 and s2
s1 = util.awgn(s1,snr)
s2 = util.awgn(s2,snr)
R = music.covar(samples)
est = music.Estimator(ants,R,nsignals=2)
def spectest(n=256):
t = time()
spec = est.spectrum((n,n))
elapsed = time() - t
print("spectrum calculation time: {}".format(elapsed))
return spec
def sumspectest(dim=512,n=16):
accum = sp.zeros((dim,dim))
for i in range(n):
mys1 = util.makesamples(ants,s1_aoa[0],s1_aoa[1],nsamp)
mys2 = util.makesamples(ants,s2_aoa[0],s2_aoa[1],nsamp)
mysamples = mys2 + mys1
mysamples = util.awgn(mysamples,snr)
cov = music.covar(mysamples)
accum += music.Estimator(ants,cov,nsignals=2).spectrum((dim,dim))
scipy.misc.imsave("accumspec.png",accum/accum.max())
def doatest():
print("s1 is {}".format(sp.rad2deg(s1_aoa)))
print("s2 is {}".format(sp.rad2deg(s2_aoa)))
s1_est = music.Estimator(ants,music.covar(s1),nsignals=1)
s2_est = music.Estimator(ants,music.covar(s2),nsignals=1)
# s1
t1 = time()
s1_res = s1_est.doasearch()[0]
t1 = time() - t1
s1_err = sp.rad2deg(util.aoa_diff_rad(s1_res,s1_aoa))
#print("s1 estimated value: {} in {}s, error {} deg".format(sp.int_(sp.rad2deg(s1_res)),t1,s1_err))
print("s1 estimated value: {} in {}s, error {} deg".format(sp.round_(sp.rad2deg(s1_res)),t1,s1_err))
#print("s1 estimated value: {} in {}s, error {} deg".format(sp.rad2deg(s1_res),t1,s1_err))
# s2
t2 = time()
s2_res = s2_est.doasearch()[0]
t2 = time() - t2
s2_err = sp.rad2deg(util.aoa_diff_rad(s2_res,s2_aoa))
print("s2 estimated value: {} in {}s, error {} deg".format(sp.round_(sp.rad2deg(s2_res)),t2,s2_err))
# both signals
bothres = est.doasearch()
#print("Both signals:\n{}".format(sp.rad2deg(bothres)))
# timing
def cspec_error(n=64):
specpy = est.spectrum((n,n),method=music._spectrum)
specc = est.spectrum((n,n),method=_music.spectrum)
scipy.misc.imsave("c-spectrum.png",specc/np.max(specc))
scipy.misc.imsave("python-spectrum.png",specpy/np.max(specpy))
return sp.mean(abs(specc-specpy))
def timetrial(reps=5):
result = {}
for i in range(5,10): # 32-512
times = []
for j in range(reps):
t = time()
_ = est.spectrum((2**i,2**i))
times.append(time() - t)
result[2**i] = min(times)
return result
def indeptest(dim):
R1 = music.covar(s1)
R2 = music.covar(s2)
s1spec = music.Estimator(ants,R1,nsignals=1).spectrum(dim)
s2spec = music.Estimator(ants,R2,nsignals=1).spectrum(dim)
bothspec = music.Estimator(ants,R,nsignals=2).spectrum(dim)
scipy.misc.imsave("s1spec.png",s1spec/s1spec.max())
scipy.misc.imsave("s2spec.png",s2spec/s2spec.max())
scipy.misc.imsave("bothspec.png",bothspec/bothspec.max())
def profile():
cProfile.run("_ = est.spectrum((512,512))","spectrum.gprofile")
cProfile.run("_ = doatest()","doasearch.gprofile")
if __name__ == '__main__':
if sys.argv[1] == "profile":
cProfile.run("_ = spectest(128)","spectrum.gprofile")
cProfile.run("_ = doatest()","doasearch.gprofile")
elif sys.argv[1] == "spectrum":
if len(sys.argv) == 3:
size = int(sys.argv[2])
else:
size = 512
spec = spectest(size)
scipy.misc.imsave("spectrum.png",spec/np.max(spec))
logspec = sp.log(spec/spec.min()) #positive only
scipy.misc.imsave("spectrum-log.png",logspec/logspec.max())
# excluding the top 5%
q95 = sp.sort(spec,axis=None)[int(-np.floor(0.05*(size**2)))] #changed this and line 172 to int
lowspec = sp.clip(spec,0,q95)
logq95 = sp.sort(spec,axis=None)[int(-np.floor(0.05*(size**2)))]
lowlogspec = sp.clip(logspec,0,logq95)
scipy.misc.imsave("spectrum-log-lows.png",lowlogspec)
scipy.misc.imsave("spectrum-lows.png",lowspec)
elif sys.argv[1] == "check":
print("Mean absolute deviation from python: {}".format(cspec_error()))
elif sys.argv[1] == "timetrial":
print("Times:")
for i in timetrial().items():
print("{}\t{}".format(*i))
elif sys.argv[1] == "doasearch":
indeptest((256,256))
doatest()
elif sys.argv[1] == "indep":
indeptest((512,1024))
elif sys.argv[1] == "sumspec":
sumspectest(dim=512,n=int(sys.argv[2]))
else:
print("Bad arguments to _tests.py")
exit(1)