common.loads() should return time only if asked to

This commit is contained in:
Roman Zeyde
2014-07-21 13:29:27 +03:00
parent 5d71431c7c
commit 3ccf4ff188
5 changed files with 15 additions and 10 deletions

View File

@@ -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))

View File

@@ -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):

View File

@@ -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))

View File

@@ -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)

View File

@@ -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)