-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtl_corr_help.py
131 lines (117 loc) · 3.61 KB
/
rtl_corr_help.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
# this module will be imported in the into your flowgraph
import os
import numpy
import xmlrpclib as xmlrpc
import time
doneffts = False
def st_update(pace,port,files,srate,secs):
global doneffts
#
# We've already done this, leave without doing anything
#
if (doneffts == True):
return 1
#
# Compute how many bytes are required in the capture files
#
needed = int(srate * secs * 8.0)
doit = True
#
# Check each file to make sure that it is big enough
#
for f in files:
if (os.path.exists(f) != True):
doit = False
break
if (os.path.getsize(f) < needed):
doit = False
# We go here, time to do cross correlation using
# FFT-based fast convolution
#
if doit == True and doneffts == False:
#
# Make up a channel list consisting of buffers from the
# capture files
#
chans = []
for f in files:
chans.append(numpy.fromfile(f,dtype=numpy.csingle,sep=""))
#
# Pad the data with zeros to improve resolution
#
for i in range(len(chans)):
#
# Truncate to exactly "secs" worth of samples
#
chans[i] = chans[i][:int(srate*secs)]
#
# First channel is special--pad order is different
#
if (i == 0):
chans[i] += numpy.zeros(len(chans[i]),dtype=numpy.csingle)
else:
chans[i] = numpy.zeros(len(chans[i]),dtype=numpy.csingle)+chans[i]
#
# Now we have a list of FFTs to compute on the padded channel data
#
ffts = []
for i in range(len(chans)):
ffts.append(numpy.fft.fft(chans[i]))
time.sleep(0.25)
iffts = []
#
# Now we need to do conjugate multiply on each of the
# channels against channel 0
#
conjugate = numpy.conj(ffts[0])
for i in range(1,len(chans)):
conjm = numpy.multiply(conjugate,ffts[i])
iffts.append(numpy.fft.ifft(conjm))
time.sleep(0.25)
#
# Now we extract the location of max correlation
#
offsets = [0]
phases = [0]
for i in range(len(iffts)):
magn = numpy.multiply(numpy.absolute(iffts[i]),numpy.absolute(iffts[i]))
mx = numpy.argmax(magn)
mc = iffts[i][mx]
offsets.append(int(mx))
phases.append(float(numpy.angle(mc)))
time.sleep(0.125)
doneffts = True
try:
handle = xmlrpc.ServerProxy("http://localhost:%d" % port, allow_none=True)
except:
return 0
mxd = max(offsets)
ndx = 0
#
# Adjust delays an phases
#
for o in offsets:
offsets[ndx] = mxd - offsets[ndx]
phases[ndx] *= -1
ndx += 1
handle.set_delays(offsets)
#
# Invert phases
#
handle.set_phases(phases)
print "Delays " + str(offsets)
print "Phases " + str(phases)
return (0 if doneffts == 0 else 1)
def log(d,prefix):
ltp = time.gmtime()
fn = prefix+"%d%02d%02d.csv" % (ltp.tm_year, ltp.tm_mon, ltp.tm_mday)
fp = open(fn, "a")
fp.write("%02d,%02d,%02d," % (ltp.tm_hour, ltp.tm_min, ltp.tm_sec))
ndx = 0
for v in d:
fp.write("%11.9f,%11.9f" % (v.real, v.imag))
if (ndx < len(d)-1):
fp.write(",")
ndx += 1
fp.write("\n")
print d