forked from romanz/amodem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calib.py
executable file
·55 lines (47 loc) · 1.38 KB
/
calib.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
#!/usr/bin/env python
import numpy as np
import common
import config
import sigproc
import wave
Tsample = 1
t = np.arange(int(Tsample * config.Fs)) * config.Ts
sig = np.exp(2j * np.pi * config.Fc * t)
sig_dump = common.dumps(sig)
def send():
p = wave.play('-', stdin=wave.sp.PIPE)
while True:
try:
p.stdin.write(sig_dump)
except IOError:
return
def recv():
out = wave.record('-', stdout=wave.sp.PIPE).stdout
while True:
data = out.read(len(sig_dump))
if len(data) < len(sig_dump):
return
try:
x = common.loads(data)
except common.SaturationError as e:
print('saturation: {}'.format(e))
continue
x = x - np.mean(x)
c = np.abs(np.dot(x, sig)) / (np.sqrt(0.5 * len(x)) * sigproc.norm(x))
z = np.dot(x, sig.conj()) / (0.5 * len(x))
amp = np.abs(z)
phase = np.angle(z)
peak = np.max(np.abs(x))
print('coherence={:.3f} amp={:.3f} phase={:.1f} peak={:.3f}'.format(
c, amp, phase * 180 / np.pi, peak))
if __name__ == '__main__':
import argparse
p = argparse.ArgumentParser()
sub = p.add_subparsers()
sub.add_parser('send').set_defaults(func=send)
sub.add_parser('recv').set_defaults(func=recv)
args = p.parse_args()
try:
args.func()
except KeyboardInterrupt:
pass