-
Notifications
You must be signed in to change notification settings - Fork 28
/
sonar2.py
executable file
·77 lines (73 loc) · 2 KB
/
sonar2.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
#!/usr/bin/env python
import audio
import numpy as np
import pylab as pl
import sys
import time
# http://poincare.matf.bg.ac.rs/~ezivkovm/publications/primpol1.pdf
polys = {
2: (1,),
3: (1,),
4: (1,),
5: (2,),
6: (1,),
7: (1,),
8: (1, 2, 7),
9: (4,),
10: (3,),
11: (2,),
12: (1, 2, 10),
13: (3, 5, 8),
14: (1, 11, 12),
15: (1,),
16: (10, 12, 15),
17: (3,),
}
def mls(n=13):
assert n in polys, "No primitive polynomial for n=%d" % n
poly = (0, n) + polys[n]
poly = np.array(poly)
r = 1
m = np.empty((1<<n)-1, np.uint8)
for i in range((1<<n)-1):
m[i] = r & 1
r = (r>>1) | (np.bitwise_xor.reduce(r >> poly) & 1)<<(n-1)
return m
class SonarTransciever:
def __init__(self, m):
self.period = m.size*2
m = m.astype(np.float32) * 2 - 1
m = np.concatenate((np.r_[m, m][:,None], np.r_[m, -m][:,None]), 1)
m *= 1e-3
self.inBufSize = 2048
self.outBufSize = 2048
targetLength = self.outBufSize+self.period-1
self.output = np.tile(m, ((targetLength+m.shape[0]-1) // m.shape[0], 1))
self.input_fragment = np.zeros(0, np.complex64)
def __len__(self):
return sys.maxsize
def __getitem__(self, sl):
start = sl.start % self.period
count = sl.stop - sl.start
return self.output[start:start+count]
def append(self, sequence):
stream = np.r_[self.input_fragment, sequence]
lookahead = self.pulse_duration-1 # needed for convolution
n = max(0, (stream.size-lookahead) // self.period)
advance = n*self.period
if n > 0:
y = stream[:(n+1)*advance+lookahead]
self.input_fragment = stream[advance:]
self.i += advance
n = 11
Fs = 96e3
print('Period: %.1f ms' % (1000*2*((1<<n)-1)/Fs,))
xcvr = SonarTransciever(mls(n))
ap = audio.AudioInterface()
try:
ap.play(xcvr, Fs)
ap.record(xcvr, Fs)
while True:
time.sleep(1.)
except KeyboardInterrupt:
ap.stop()