Created
January 26, 2015 05:34
-
-
Save mbeloshitsky/294a8759a08011b84383 to your computer and use it in GitHub Desktop.
Пример моделирование пульс-пары на языке Python методом инерциальных ячеек памяти
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Пример моделирование пульс-пары на языке Python | |
# методом инерциальных ячеек памяти | |
def makeI(up_delay, down_delay): | |
def I(dt, inp, out, acc): | |
untouched = 0, out, acc | |
if dt == 0: # Напрасный вызов за 0 времени | |
# ничего произойти не может | |
return untouched | |
if inp == out: # Система в покое | |
return untouched | |
# Пересчитываем аккумулятор | |
asign, adelay = inp and (1, up_delay) or (-1, down_delay) | |
acc = max(0, min(1, acc+asign*(dt/adelay))) | |
if inp == 0 and acc == 0: | |
return 0, False, 0 | |
if inp == 1 and acc == 1: | |
return 0, True, 1 | |
return (1.0-acc)*adelay, out, acc | |
return I | |
class Inertial: | |
def __init__(self, i): | |
self._i = i | |
self._acc = 0 | |
self._t = 0 | |
self._tnext = 0 | |
self._inp = False | |
self._out = False | |
def _refresh(self, t): | |
dtnext, self._out, self._acc = self._i(t-self._t, self._inp, self._out, self._acc) | |
self._t = t | |
self._tnext = t+dtnext | |
def set(self, t, val): | |
if self._inp == val: | |
return | |
self._refresh(t) | |
self._inp = val | |
self._refresh(t) | |
def get(self, t): | |
if t >= self._tnext: | |
self._refresh(t) | |
return self._out | |
from time import sleep | |
def test(): | |
A = Inertial(makeI(100.0, 300.0)) | |
B = Inertial(makeI(100.0, 500.0)) | |
t = 0 | |
while True: | |
A.set(t, not B.get(t)) | |
B.set(t, A.get(t)) | |
print "%02.2f: A=%d B=%d" % (t/1000.0, A.get(t), B.get(t)) | |
t += 100 | |
sleep(0.1) | |
if __name__ == '__main__': | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment