pylint common.py

This commit is contained in:
Roman Zeyde
2014-07-12 08:11:31 +03:00
parent f81518c8d5
commit 728919ee2f

View File

@@ -21,6 +21,7 @@ SATURATION_THRESHOLD = 1.0
LENGTH_FORMAT = '<I' LENGTH_FORMAT = '<I'
def to_bits(bytes_list): def to_bits(bytes_list):
for val in bytes_list: for val in bytes_list:
for i in range(8): for i in range(8):
@@ -28,26 +29,32 @@ def to_bits(bytes_list):
yield (1 if (val & mask) else 0) yield (1 if (val & mask) else 0)
bit_weights = [1 << i for i in range(8)] bit_weights = [1 << i for i in range(8)]
def to_byte(bits): def to_byte(bits):
assert len(bits) == 8 assert len(bits) == 8
byte = int(np.dot(bits, bit_weights)) byte = int(np.dot(bits, bit_weights))
return chr(byte) return chr(byte)
def load(fileobj): def load(fileobj):
return loads(fileobj.read()) return loads(fileobj.read())
def loads(data): def loads(data):
x = np.fromstring(data, dtype='int16') x = np.fromstring(data, dtype='int16')
x = x / scaling x = x / scaling
t = np.arange(len(x)) / Fs t = np.arange(len(x)) / Fs
return t, x return t, x
def dumps(sym, n=1): def dumps(sym, n=1):
sym = sym.imag * scaling sym = sym.imag * scaling
sym = sym.astype('int16') sym = sym.astype('int16')
data = sym.tostring() data = sym.tostring()
return data * n return data * n
def iterate(data, bufsize, offset=0, advance=1, func=None): def iterate(data, bufsize, offset=0, advance=1, func=None):
assert bufsize > 0 assert bufsize > 0
assert offset >= 0 assert offset >= 0
@@ -68,7 +75,9 @@ def iterate(data, bufsize, offset=0, advance=1, func=None):
buf_index = max(0, buf_index - advance) buf_index = max(0, buf_index - advance)
offset += advance offset += advance
class Splitter(object): class Splitter(object):
def __init__(self, iterable, n): def __init__(self, iterable, n):
self.iterable = iter(iterable) self.iterable = iter(iterable)
self.read = [True] * n self.read = [True] * n
@@ -92,9 +101,11 @@ class Splitter(object):
self.read[index] = True self.read[index] = True
yield self.last[index] yield self.last[index]
def split(iterable, n): def split(iterable, n):
return Splitter(iterable, n).generators return Splitter(iterable, n).generators
def icapture(iterable, result): def icapture(iterable, result):
for i in iter(iterable): for i in iter(iterable):
result.append(i) result.append(i)
@@ -103,13 +114,10 @@ def icapture(iterable, result):
if __name__ == '__main__': if __name__ == '__main__':
import pylab import pylab
def plot(f): t = pylab.linspace(0, Tsym, 1e3)
t = pylab.linspace(0, Tsym, 1e3) x = pylab.sin(2 * pylab.pi * Fc * t)
x = pylab.sin(2 * pylab.pi * f * t) pylab.plot(t / Tsym, x)
pylab.plot(t / Tsym, x) t = pylab.linspace(0, Tsym, Nsym + 1)
t = pylab.linspace(0, Tsym, Nsym + 1) x = pylab.sin(2 * pylab.pi * Fc * t)
x = pylab.sin(2 * pylab.pi * f * t) pylab.plot(t / Tsym, x, '.k')
pylab.plot(t / Tsym, x, '.k')
plot(Fc)
pylab.show() pylab.show()