Files
amodem/loop.py
2014-07-22 14:19:51 +03:00

39 lines
1.1 KiB
Python

import numpy as np
import recv
import sampling
import sigproc
class Filter(object):
def __init__(self, b, a=()):
self.b = b
self.a = a
self.x = [0] * len(b)
self.y = [0] * len(a)
def __call__(self, x):
self.x = [x] + self.x
self.x = self.x[:len(self.b)]
self.y = self.y[:len(self.a)]
y = np.dot(self.x, self.b) + np.dot(self.y, self.a)
self.y = [y] + self.y
return y
class FreqLoop(object):
def __init__(self, x, freq):
self.sampler = sampling.Sampler(x, sampling.Interpolator())
self.symbols = recv.extract_symbols(self.sampler, freq)
Kp, Ki = 0.2, 0.01
b = np.array([1, -1])*Kp + np.array([0.5, 0.5])*Ki
self.filt = Filter(b=b, a=[1])
self.correction = 0.0
def correct(self, actual, expected):
self.err = np.angle(expected / actual) / np.pi
self.err = sigproc.clip(self.err, [-0.1, 0.1])
self.correction = self.filt(self.err)
self.sampler.correct(offset=self.correction)
def __iter__(self):
return iter(self.symbols)