-
Notifications
You must be signed in to change notification settings - Fork 48
/
util.py
117 lines (95 loc) · 2.95 KB
/
util.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
#@version1.0 date: 11/04/2019 by zhenyuhuang
#@author: [email protected]
'''
This file contains some matrix operator or unit convert function
'''
import os
import inspect
import numpy as np
from scipy.linalg import cholesky, inv, schur
from parameters import eps
def hermitian(X):
'''
Compute and return Hermitian transpose
'''
return X.conj().T
def jn(n):
'''
Used in beampattern design
Jacobi-Anger expansion中余弦级数展开中除去第一类贝塞尔函数的部分
'''
# assert isinstance(n, int)
if n == 0:
v = 1
else:
v = 2 * (1j) ** n
return v
def jeig(A, B, sort=True):
'''
Joint Diagonalization Function from original Matlab script
'''
L = cholesky(B, lower=True) #lower triangle
G = inv(L)
C = G.dot(A).dot(G.conj().T)
[D, Q] = schur(C)
X = G.conj().T.dot(Q)
if sort:
d = np.diag(D)
D = np.diag(np.sort(d)[::-1])
idx = np.argsort(d)[::-1]
X = X[:,idx]
return X, D
def dB(signal, power=False):
'''
Compute to dB unit for a given power
'''
if power is True:
return 10 * np.log10(np.abs(signal) + eps)
else:
return 20 * np.log10(np.abs(signal) + eps)
def make_directory_if_not_exists(dir_name):
'''
Create a directory if it does not exist already.
'''
path = os.path.abspath(dir_name)
if not os.path.isdir(path):
os.makedirs(path)
def get_base_path():
'''
Get the root directory of beamforming.
'''
basepath = os.path.abspath(os.path.dirname(os.path.abspath(__file__))
+ os.path.sep + ".")
return basepath.replace('\\', '/')
def import_beamforming(beam_domain, beam_type, beam_name):
'''
import beamforming class
Parameters
----------
beam_domain: string
beamforming domain, time or frequency
beam_type: string
beamforming type, such as fixed/adaptive and so on
beam_name: string
beamformig name, such as DelayAndSum ..
'''
#beam_domain and beam_type must in below strings
assert beam_domain in ['frequency', 'time']
assert beam_type in ['fixed', 'adaptive', 'differential', 'pattern_design']
beam_object = '{}_domain_beamforming.{}_beamforming'.format(beam_domain, beam_type)
beam_file = '{}.py'.format(beam_object.replace('.', '/'))
basepath = get_base_path()
if os.path.isfile('{}/{}'.format(basepath, beam_file)):
mod = __import__(beam_object, fromlist=[beam_name])
mode_class_list = [beam[0] for beam in inspect.getmembers(mod)]
# judge beamforming class exists
if beam_name in mode_class_list:
klass = getattr(mod, beam_name)
else:
raise ValueError('No {} beamforming class in {}!'.format(beam_name, beam_file))
else:
raise ValueError('No {} file!'.format(beam_file))
return klass
if __name__ == "__main__":
x = get_base_path()
print(x)