mirror of
https://github.com/romanz/amodem.git
synced 2026-03-24 03:11:15 +08:00
Refactor iteration.
This commit is contained in:
22
common.py
22
common.py
@@ -52,6 +52,28 @@ def dumps(sym, n=1):
|
||||
def norm(x):
|
||||
return np.sqrt(np.dot(x.conj(), x).real)
|
||||
|
||||
|
||||
def iterate(data, bufsize, offset=0, advance=1, func=None):
|
||||
assert bufsize > 0
|
||||
assert offset >= 0
|
||||
assert advance > 0
|
||||
buf = np.zeros(bufsize)
|
||||
buf_index = 0
|
||||
for data_index, value in enumerate(data):
|
||||
if data_index < offset:
|
||||
continue
|
||||
|
||||
buf[buf_index] = value
|
||||
buf_index += 1
|
||||
|
||||
if buf_index == bufsize:
|
||||
result = func(buf) if func else buf
|
||||
print offset, result
|
||||
yield offset, result
|
||||
buf[:-advance] = buf[advance:]
|
||||
buf_index = max(0, buf_index - advance)
|
||||
offset += advance
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
import pylab
|
||||
|
||||
Reference in New Issue
Block a user