From 3ccf4ff188570fb51c39d930f13d1e2210265db5 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Mon, 21 Jul 2014 13:29:27 +0300 Subject: [PATCH] common.loads() should return time only if asked to --- calib.py | 2 +- common.py | 16 +++++++++++----- sampling.py | 2 +- show.py | 2 +- stream.py | 3 +-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/calib.py b/calib.py index 1dc68ee..960c141 100755 --- a/calib.py +++ b/calib.py @@ -25,7 +25,7 @@ def recv(): data = out.read(len(sig_dump)) if len(data) < len(sig_dump): return - _, x = common.loads(data) + x = common.loads(data) x = x - np.mean(x) c = np.abs(np.dot(x, sig)) / (np.sqrt(0.5 * len(x)) * sigproc.norm(x)) diff --git a/common.py b/common.py index c321341..b61c6d0 100644 --- a/common.py +++ b/common.py @@ -42,15 +42,21 @@ def to_byte(bits): return chr(byte) -def load(fileobj): - return loads(fileobj.read()) +def load(fileobj, time=False): + return loads(fileobj.read(), time=time) -def loads(data): +def loads(data, time=False): x = np.fromstring(data, dtype='int16') x = x / scaling - t = np.arange(len(x)) / Fs - return t, x + if np.max(np.abs(x)) > SATURATION_THRESHOLD: + raise ValueError('saturation') + + if time: + t = np.arange(len(x)) / Fs + return t, x + else: + return x def dumps(sym, n=1): diff --git a/sampling.py b/sampling.py index 40605dd..638eaba 100644 --- a/sampling.py +++ b/sampling.py @@ -72,7 +72,7 @@ if __name__ == '__main__': df, = sys.argv[1:] df = float(df) - _, x = common.load(sys.stdin) + x = common.load(sys.stdin) sampler = Sampler(x, Interpolator()) sampler.freq += df y = np.array(list(sampler)) diff --git a/show.py b/show.py index 145a530..84f5dfc 100755 --- a/show.py +++ b/show.py @@ -29,7 +29,7 @@ if __name__ == '__main__': import common for fname in sys.argv[1:]: - t, x = common.load(open(fname, 'rb')) + t, x = common.load(open(fname, 'rb'), time=True) pylab.figure() pylab.title(fname) spectrogram(t, x, common.Fs) diff --git a/stream.py b/stream.py index b17aade..5473921 100644 --- a/stream.py +++ b/stream.py @@ -27,8 +27,7 @@ class Reader(object): if data: block.extend(data) if len(block) == self.BUFSIZE: - _, values = common.loads(str(block)) - return values + return common.loads(str(block)) time.sleep(self.WAIT)