-
Notifications
You must be signed in to change notification settings - Fork 0
/
stft_stastic.py
169 lines (137 loc) · 4.78 KB
/
stft_stastic.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
import struct
import matplotlib.pyplot as plt
import numpy as np
import os
from scipy import signal
from math import pi,sin,cos
plt.rc('font',family='Times New Roman',size=10)
markers_freq = ['.','o','v','1','2','3','4','^','<','>','s','p','*','h','H','+','x','D','d','|','_',',']
fig = plt.figure(figsize=(5,3))
num = np.array([1,0,-1])
den = np.array([1,-1.9039,0.9195])
sgn = 1.0
interval = 5
dt = 0.0000001
fs = 10000000.0
fftnum = 500#64#512#1024#8192
std = 125#16#128#256#1500
axis_xf = [11]
freq = np.array([fs * i / fftnum for i in axis_xf])
win = signal.gaussian(fftnum, std)
basis = []
for k in [11]:
basis.append([complex(cos(2*pi/fftnum*k*n),sin(2*pi/fftnum*k*n)) for n in range(fftnum)])
basis = np.transpose(basis)
result = []
pwd = os.getcwd()
for dd in np.arange(42,163,10):
count = 0
record = []
while True:
filepath = pwd + "//" + "input_data" + "//" + "4" + "//" + str(dd) + "//" + str(count)
if os.path.isfile(filepath) == False:
break
fb = open(filepath, "rb")
x = 0
data1 = []
data2 = []
while True:
data = fb.read(4)
if not data:
break
ch1, ch2 = struct.unpack('<HH', data)
ch1 = (float(ch1) - 8192) / 8192 * 2.5
ch2 = (float(ch2) - 8192) / 8192 * 2.5
ch1 = float(ch1)
ch2 = float(ch2)
data1.append(ch1)
data2.append(ch2)
x = x + 1
fb.close()
datay1 = data1[250000:350000]
datay2 = data2[260000:340000]
#datay1 = signal.lfilter(num,den,data1[250000:350000])
#datay2 = signal.lfilter(num,den,data2[260000:340000])
count1 = len(datay1)
count2 = len(datay2)
index = 0
magnitude1 = []
magnitude2 = []
while True:
data1 = np.array(datay1[index:index + fftnum])
data1 = np.multiply(data1,win)
data1 = np.dot(data1,basis)
data1 = np.abs(data1)
magnitude1.append(data1)
if index + fftnum <= count2:
data2 = np.array(datay2[index:index + fftnum])
data2 = np.multiply(data2,win)
data2 = np.dot(data2,basis)
data2 = np.abs(data2)
magnitude2.append(data2)
index += interval
if index + fftnum > count1:
break
temp = signal.correlate(magnitude1,magnitude2, mode='valid',method='fft')
corr = ((np.where(temp == max(temp))[0][0] - 10000 / interval ) * interval * dt * 1000)
print(corr)
record.append(corr)
#magnitude1 = np.array(magnitude1).T
#magnitude2 = np.array(magnitude2).T
#corr = []
#for i in range(len(magnitude1)):
# mean1 = magnitude1[i].mean()
# magnitude1[i] = magnitude1[i] / mean1
# mean2 = magnitude2[i].mean()
# magnitude2[i] = magnitude2[i] / mean2
# temp = signal.correlate(magnitude1[i],magnitude2[i], mode='valid',method='fft')
# corr.append((np.where(temp == max(temp))[0][0] - 10000 / interval ) * interval * dt * 1000)
#print(corr)
#record.append(corr[0])
##
## tdoa = max(corr,key = lambda x:x[0])
## print tdoa[2],(tdoa[1] - 10000 / interval ) * interval * dt * 1000
##
## E=207 * pow(10,9) #203#207
## p=7.86 * 1000 #7.93#7.86
## o=0.27
## h=0.002
##
## param = E * h * h * pi * pi / 3.0 / p / (1.0 - o * o)
## c = pow(param * pow(freq,2),0.25)
## time = sgn * (100.0 - dd) * 2.0 / 100.0 / c * 1000.0
##
## plt.plot(time,corr,'+')
## for i in range(len(freq)):
## fr.write(str(int(freq[i])) + " " + str(time[i]) + " " + str(corr[i]) + "\r\n")
count = count + 1
if count > 20:
break
result.append(record)
##fr.close()
##plt.xlabel('theory dt/ms')
##plt.ylabel('cross correlation dt/ms')
##
##plt.xlim(-2,2)
##plt.ylim(-1.5,1.5)
##
##ax = plt.gca()
##box = ax.get_position()
##ax.set_position([box.x0, box.y0, box.width, box.height])
##plt.legend(loc='upper left',bbox_to_anchor=(1,1),markerscale=2)
##plt.subplots_adjust(bottom = 0.2,left = 0.15,right=0.7)
##
##plt.show()
#result = np.transpose(result)
#for index,item in enumerate(result):
# plt.plot(item,marker = markers_freq[index], label = str((axis_xf[index] + 1) * 20) + 'kHz')
for index,item in enumerate(result):
plt.plot(item,marker = markers_freq[index], label = str((100 - (index + 4) * 10) * 2) + 'cm')
plt.xlabel('order')
plt.ylabel('dt/ms')
ax = plt.gca()
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width, box.height])
plt.legend(loc='upper left',bbox_to_anchor=(1,1),markerscale=2)
plt.subplots_adjust(bottom = 0.2,left = 0.15,right=0.7)
plt.show()