refactor loop

This commit is contained in:
Roman Zeyde
2014-07-05 09:39:57 +03:00
parent c1302a2d82
commit c9d7f8e2f2
2 changed files with 21 additions and 44 deletions

View File

@@ -1,31 +1,10 @@
import numpy as np
import pylab
import recv
import common
import sigproc
import sampling
import loop
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 = loop.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)
import pylab
def main():
f0 = 10e3
_, x = common.load(file('recv_10kHz.pcm', 'rb'))
@@ -34,7 +13,7 @@ def main():
S = []
Y = []
symbols = FreqLoop(x, f0)
symbols = loop.FreqLoop(x, f0)
prefix = 100
for s in symbols:
S.append(s)

40
loop.py
View File

@@ -1,5 +1,9 @@
import numpy as np
import recv
import sampling
import sigproc
class Filter(object):
def __init__(self, b, a=()):
self.b = b
@@ -15,26 +19,20 @@ class Filter(object):
self.y = [y] + self.y
return y
class Loop(object):
def __init__(self, x, A, k, h):
self.x = np.array(x)
self.A = np.array(A)
self.k = np.array(k)
self.h = np.array(h)
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 __call__(self, y):
self.err = y - np.dot(self.h, self.x)
self.dx = self.k * self.err
self.x = self.x + self.dx
self.x = np.dot(self.A, self.x)
class Integrator(Loop):
def __init__(self, phase, freq, k):
x = [phase, freq] # state variable vector
# evolution matrix:
# | phase' = phase + freq
# | freq' = freq
A = [[1, 1], [0, 1]]
h = [1, 0] # phase = dot(h, x)
Loop.__init__(self, x=x, A=A, k=k, h=h)
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)