1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
import numpy
|
8
|
import math
|
9
|
import matplotlib
|
10
|
import matplotlib.pyplot as plt
|
11
|
import datetime
|
12
|
import time
|
13
|
import pickle
|
14
|
|
15
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22
|
def create_pseudo_random_code(len=10000,seed=0):
|
23
|
numpy.random.seed(seed)
|
24
|
phases = numpy.array(numpy.exp(1.0j*2.0*math.pi*numpy.random.random(len)),
|
25
|
dtype=numpy.complex64)
|
26
|
return(phases)
|
27
|
|
28
|
def periodic_convolution_matrix(envelope,rmin=0,rmax=100):
|
29
|
|
30
|
|
31
|
L = len(envelope)
|
32
|
ridx = numpy.arange(rmin,rmax)
|
33
|
A = numpy.zeros([L,rmax-rmin],dtype=numpy.complex64)
|
34
|
for i in numpy.arange(L):
|
35
|
A[i,:] = envelope[(i-ridx)%L]
|
36
|
result = {}
|
37
|
result['A'] = A
|
38
|
result['ridx'] = ridx
|
39
|
return(result)
|
40
|
|
41
|
def analyze_prc_file(fname="data-000001.gdf",clen=10000,station=0,Nranges=1000):
|
42
|
z = numpy.fromfile(fname,dtype=numpy.complex64)
|
43
|
code = create_pseudo_random_code(len=clen,seed=station)
|
44
|
N = len(z)/clen
|
45
|
res = numpy.zeros([N,Nranges],dtype=numpy.complex64)
|
46
|
idx = numpy.arange(clen)
|
47
|
r = create_estimation_matrix(code=code,cache=True)
|
48
|
B = r['B']
|
49
|
spec = numpy.zeros([N,Nranges],dtype=numpy.float32)
|
50
|
|
51
|
for i in numpy.arange(N):
|
52
|
res[i,:] = numpy.dot(B,z[idx + i*clen])
|
53
|
for i in numpy.arange(Nranges):
|
54
|
spec[:,i] = numpy.abs(numpy.fft.fft(res[:,i]))
|
55
|
r['res'] = res
|
56
|
r['spec'] = spec
|
57
|
return(r)
|
58
|
|
59
|
B_cache = 0
|
60
|
C_cache = 0
|
61
|
D_cache = 0
|
62
|
H_cache = 0
|
63
|
r_cache = 0
|
64
|
r_cache1= 0
|
65
|
r_cache2= 0
|
66
|
B_cached = False
|
67
|
def create_estimation_matrix(code,rmin=0,rmax=1000,cache=True):
|
68
|
'''
|
69
|
global B_cache
|
70
|
global C_cache
|
71
|
global D_cache
|
72
|
global r_cache
|
73
|
global r_cache1
|
74
|
global r_cache2
|
75
|
global B_cached
|
76
|
'''
|
77
|
if cache == False or B_cached == False:
|
78
|
r_cache = periodic_convolution_matrix(envelope=code,rmin=rmin,rmax=rmax)
|
79
|
A = r_cache['A']
|
80
|
Ah = numpy.transpose(numpy.conjugate(A))
|
81
|
B_cache = numpy.dot(numpy.linalg.inv(numpy.dot(Ah,A)),Ah)
|
82
|
r_cache['B'] = B_cache
|
83
|
'''
|
84
|
code = create_pseudo_random_code(len=10000,seed=1)
|
85
|
#code = numpy.roll(code,-250)
|
86
|
r_cache1 = periodic_convolution_matrix(envelope=code,rmin=rmin,rmax=rmax)
|
87
|
C = r_cache1['A']
|
88
|
Ch = numpy.transpose(numpy.conjugate(C))
|
89
|
C_cache = numpy.dot(numpy.linalg.inv(numpy.dot(Ch,C)),Ch)
|
90
|
r_cache['C'] = C_cache
|
91
|
|
92
|
code = create_pseudo_random_code(len=10000,seed=2)
|
93
|
#code = numpy.roll(code,-500)
|
94
|
|
95
|
r_cache2 = periodic_convolution_matrix(envelope=code,rmin=rmin,rmax=rmax)
|
96
|
D = r_cache2['A']
|
97
|
Dh = numpy.transpose(numpy.conjugate(D))
|
98
|
D_cache = numpy.dot(numpy.linalg.inv(numpy.dot(Dh,D)),Dh)
|
99
|
r_cache['D'] = D_cache
|
100
|
|
101
|
B_cached = True
|
102
|
return(r_cache)
|
103
|
'''
|
104
|
else:
|
105
|
return(r_cache)
|
106
|
|
107
|
def create_estimation_matrixNEWH(code,rmin=0,rmax=1000,cache=True):
|
108
|
global r_cache
|
109
|
global r_cache1
|
110
|
global r_cache2
|
111
|
global H_cache
|
112
|
global B_cached
|
113
|
print code.shape,"1"
|
114
|
|
115
|
if cache == False or B_cached == False:
|
116
|
code = numpy.roll(code,-2500)
|
117
|
r_cache = periodic_convolution_matrix(envelope=code,rmin=rmin,rmax=rmax)
|
118
|
A = r_cache['A']
|
119
|
print A.shape
|
120
|
|
121
|
|
122
|
|
123
|
|
124
|
code = create_pseudo_random_code(len=10000,seed=1)
|
125
|
code = numpy.roll(code,-5000)
|
126
|
|
127
|
r_cache1 = periodic_convolution_matrix(envelope=code,rmin=rmin,rmax=rmax)
|
128
|
C = r_cache1['A']
|
129
|
|
130
|
|
131
|
|
132
|
|
133
|
|
134
|
code = create_pseudo_random_code(len=10000,seed=2)
|
135
|
code = numpy.roll(code,-7500)
|
136
|
r_cache2 = periodic_convolution_matrix(envelope=code,rmin=rmin,rmax=rmax)
|
137
|
D = r_cache2['A']
|
138
|
|
139
|
|
140
|
|
141
|
|
142
|
H=numpy.concatenate((A ,C,D))
|
143
|
print H.shape,"1.3"
|
144
|
Hh = numpy.transpose(numpy.conjugate(H))
|
145
|
|
146
|
|
147
|
|
148
|
|
149
|
|
150
|
H_cache= numpy.dot(numpy.linalg.inv(numpy.dot(Hh,H)),Hh)
|
151
|
|
152
|
r_cache['H']=H_cache
|
153
|
B_cached = True
|
154
|
return(r_cache)
|
155
|
else:
|
156
|
return(r_cache)
|
157
|
|
158
|
def grid_search1d(fun,xmin,xmax,nstep=100):
|
159
|
vals = numpy.linspace(xmin,xmax,num=nstep)
|
160
|
min_val=fun(vals[0])
|
161
|
best_idx = 0
|
162
|
for i in range(nstep):
|
163
|
try_val = fun(vals[i])
|
164
|
if try_val < min_val:
|
165
|
min_val = try_val
|
166
|
best_idx = i
|
167
|
return(vals[best_idx])
|
168
|
|
169
|
def fit_velocity(z,t,var,frad=440.2e6):
|
170
|
zz = numpy.exp(1.0j*numpy.angle(z))
|
171
|
def ssfun(x):
|
172
|
freq = 2.0*frad*x/scipy.constants.c
|
173
|
model = numpy.exp(1.0j*2.0*scipy.constants.pi*freq*t)
|
174
|
ss = numpy.sum((1.0/var)*numpy.abs(model-zz)**2.0)
|
175
|
|
176
|
|
177
|
|
178
|
return(ss)
|
179
|
v0 = grid_search1d(ssfun,-800.0,800.0,nstep=50)
|
180
|
|
181
|
|
182
|
return(v0)
|
183
|
|
184
|
def fit_velocity_and_power(z,t,var,frad=440.2e6):
|
185
|
zz = numpy.exp(1.0j*numpy.angle(z))
|
186
|
def ssfun(x):
|
187
|
freq = 2.0*frad*x/scipy.constants.c
|
188
|
model = numpy.exp(1.0j*2.0*scipy.constants.pi*freq*t)
|
189
|
ss = numpy.sum((1.0/var)*numpy.abs(model-zz)**2.0)
|
190
|
return(ss)
|
191
|
v0 = grid_search1d(ssfun,-800.0,800.0,nstep=50)
|
192
|
v0 = scipy.optimize.fmin(ssfun,numpy.array([v0]),full_output=False,disp=False,retall=False)
|
193
|
freq = 2.0*frad*v0/scipy.constants.c
|
194
|
dc = numpy.real(numpy.exp(-1.0j*2.0*scipy.constants.pi*freq*t)*z)
|
195
|
p0 = (1.0/numpy.sum(1.0/var))*numpy.sum((1.0/var)*dc)
|
196
|
|
197
|
|
198
|
|
199
|
|
200
|
|
201
|
|
202
|
return([v0,p0])
|
203
|
|
204
|
def save_object(obj, filename):
|
205
|
with open(filename, 'wb') as output:
|
206
|
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
|
207
|
|
208
|
def load_object(filename):
|
209
|
with open(filename, 'rb') as input:
|
210
|
return(pickle.load(input))
|
211
|
|
212
|
def unix2date(x):
|
213
|
return datetime.datetime.utcfromtimestamp(x)
|
214
|
|
215
|
def unix2datestr(x):
|
216
|
return(unix2date(x).strftime('%Y-%m-%d %H:%M:%S'))
|
217
|
|
218
|
def compr(x,fr=0.001):
|
219
|
sh = x.shape
|
220
|
x = x.reshape(-1)
|
221
|
xs = numpy.sort(x)
|
222
|
mini = xs[int(fr*len(x))]
|
223
|
maxi = xs[int((1.0-fr)*len(x))]
|
224
|
mx = numpy.ones_like(x)*maxi
|
225
|
mn = numpy.ones_like(x)*mini
|
226
|
print int(fr*len(x))," ",int((1.0-fr)*len(x))
|
227
|
x = numpy.where(x < maxi, x, mx)
|
228
|
x = numpy.where(x > mini, x, mn)
|
229
|
x = x.reshape(sh)
|
230
|
return(x)
|
231
|
|
232
|
def comprz(x):
|
233
|
""" Compress signal in such a way that elements less than zero are set to zero. """
|
234
|
zv = x*0.0
|
235
|
return(numpy.where(x>0,x,zv))
|
236
|
|
237
|
def comprz_dB(xx,fr=0.05):
|
238
|
""" Compress signal in such a way that is logarithmic but also avoids negative values """
|
239
|
x = numpy.copy(xx)
|
240
|
sh = xx.shape
|
241
|
x = x.reshape(-1)
|
242
|
x = comprz(x)
|
243
|
x = numpy.setdiff1d(x,numpy.array([0.0]))
|
244
|
xs = numpy.sort(x)
|
245
|
mini = xs[int(fr*len(x))]
|
246
|
mn = numpy.ones_like(xx)*mini
|
247
|
xx = numpy.where(xx > mini, xx, mn)
|
248
|
xx = xx.reshape(sh)
|
249
|
return(10.0*numpy.log10(xx))
|
250
|
|
251
|
def decimate(x,dec=2):
|
252
|
Nout = int(math.floor(len(x)/dec))
|
253
|
idx = numpy.arange(Nout)*dec
|
254
|
|
255
|
res = x[idx]*0.0
|
256
|
|
257
|
for i in numpy.arange(dec):
|
258
|
res = res + x[idx+i]
|
259
|
return(res/float(dec))
|
260
|
|
261
|
def plot_cts(x,plot_abs=False,plot_show=True):
|
262
|
time_vec = numpy.linspace(0,len(x)-1,num=len(x))
|
263
|
plt.clf()
|
264
|
plt.plot(time_vec,numpy.real(x),"blue")
|
265
|
plt.plot(time_vec,numpy.imag(x),"red")
|
266
|
if plot_abs:
|
267
|
plt.plot(time_vec,numpy.abs(x),"black")
|
268
|
if plot_show:
|
269
|
plt.show()
|
270
|
|
271
|
def hanning(L=1000):
|
272
|
n = numpy.linspace(0.0,L-1,num=L)
|
273
|
return(0.5*(1.0-numpy.cos(2.0*scipy.constants.pi*n/L)))
|
274
|
|
275
|
def spectrogram(x,window=1024,wf=hanning):
|
276
|
wfv = wf(L=window)
|
277
|
Nwindow = int(math.floor(len(x)/window))
|
278
|
res = numpy.zeros([Nwindow,window])
|
279
|
for i in range(Nwindow):
|
280
|
res[i,] = numpy.abs(numpy.fft.fftshift(numpy.fft.fft(wfv*x[i*window + numpy.arange(window)])))**2
|
281
|
return(res)
|
282
|
|
283
|
|