mirror of
https://github.com/romanz/amodem.git
synced 2026-04-21 05:36:42 +08:00
Add frequency resampling
This commit is contained in:
67
drift.py
67
drift.py
@@ -50,19 +50,19 @@ class Interpolator(object):
|
|||||||
h = np.sinc(u / resolution) * window
|
h = np.sinc(u / resolution) * window
|
||||||
self.filt = []
|
self.filt = []
|
||||||
for index in range(resolution): # split into multiphase filters
|
for index in range(resolution): # split into multiphase filters
|
||||||
self.filt.append(h[index::resolution])
|
filt = h[index::resolution]
|
||||||
|
filt = filt[::-1]
|
||||||
|
self.filt.append(filt)
|
||||||
lengths = map(len, self.filt)
|
lengths = map(len, self.filt)
|
||||||
assert set(lengths) == set([2*width])
|
assert set(lengths) == set([2*width])
|
||||||
assert len(self.filt) == resolution
|
assert len(self.filt) == resolution
|
||||||
|
|
||||||
def get(self, offset):
|
def get(self, offset):
|
||||||
|
# offset = k + (j / self.resolution)
|
||||||
k = int(offset)
|
k = int(offset)
|
||||||
j = np.round((offset - k) * self.resolution)
|
j = int((offset - k) * self.resolution)
|
||||||
index = (self.resolution - int(j)) % self.resolution
|
coeffs = self.filt[j]
|
||||||
coeffs = self.filt[index]
|
return coeffs, k - self.width
|
||||||
|
|
||||||
offset = int(np.ceil(offset))
|
|
||||||
return coeffs, offset - self.width
|
|
||||||
|
|
||||||
class Sampler(object):
|
class Sampler(object):
|
||||||
def __init__(self, src, interp=None):
|
def __init__(self, src, interp=None):
|
||||||
@@ -76,12 +76,19 @@ class Sampler(object):
|
|||||||
def sample(self):
|
def sample(self):
|
||||||
coeffs, begin = self.interp.get(self.offset)
|
coeffs, begin = self.interp.get(self.offset)
|
||||||
end = begin + len(coeffs)
|
end = begin + len(coeffs)
|
||||||
for s in self.src:
|
# C = ', '.join(['%.3f' % c for c in coeffs[18:23]])
|
||||||
self.buff.append(s)
|
# print '%.3f [%s] %d %d' % (self.offset, C, begin, end)
|
||||||
self.index += 1
|
while True:
|
||||||
if self.index == end:
|
if self.index == end:
|
||||||
self.buff = self.buff[-len(coeffs):]
|
self.buff = self.buff[-len(coeffs):]
|
||||||
return np.dot(coeffs, self.buff)
|
return np.dot(coeffs, self.buff)
|
||||||
|
try:
|
||||||
|
s = self.src.next()
|
||||||
|
except StopIteration:
|
||||||
|
break
|
||||||
|
|
||||||
|
self.buff.append(s)
|
||||||
|
self.index += 1
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
self.offset += self.freq
|
self.offset += self.freq
|
||||||
@@ -95,46 +102,40 @@ def main():
|
|||||||
x = x[100:]
|
x = x[100:]
|
||||||
y = []
|
y = []
|
||||||
sampler = Sampler(x)
|
sampler = Sampler(x)
|
||||||
sampler.freq = 1
|
sampler.freq = 1.0 + 0.112/f0
|
||||||
while True:
|
while True:
|
||||||
u = sampler.sample()
|
u = sampler.sample()
|
||||||
if u is None:
|
if u is None:
|
||||||
break
|
break
|
||||||
y.append(u)
|
y.append(u)
|
||||||
sampler.next()
|
sampler.next()
|
||||||
x = np.array(y)
|
x_ = np.array(y)
|
||||||
|
S = recv.extract_symbols(x_, f0)
|
||||||
S = recv.extract_symbols(x, f0)
|
|
||||||
S = np.array(list(S))
|
S = np.array(list(S))
|
||||||
|
|
||||||
y = S #np.array(list(calib(S)))
|
y = S #np.array(list(calib(S)))
|
||||||
pylab.subplot(1,2,1)
|
|
||||||
pylab.plot(y.real, y.imag, '.'); pylab.axis('equal')
|
|
||||||
pylab.subplot(1,2,2)
|
|
||||||
phase = np.unwrap(np.angle(y))
|
phase = np.unwrap(np.angle(y))
|
||||||
|
|
||||||
phase_error_per_1ms = (phase[-1] - phase[0]) / (len(phase) - 1)
|
phase_error = (phase[-1] - phase[0])
|
||||||
|
phase_error_per_1ms = phase_error / (len(phase) - 1)
|
||||||
freq_error = phase_error_per_1ms * 1000.0 / (2 * np.pi)
|
freq_error = phase_error_per_1ms * 1000.0 / (2 * np.pi)
|
||||||
|
print phase_error, len(phase)
|
||||||
|
print phase_error_per_1ms
|
||||||
print freq_error
|
print freq_error
|
||||||
pylab.plot(phase)
|
|
||||||
pylab.grid('on')
|
if 1:
|
||||||
#pylab.show()
|
pylab.figure()
|
||||||
|
pylab.plot(y.real, y.imag, '.')
|
||||||
|
pylab.grid('on')
|
||||||
|
pylab.show()
|
||||||
return
|
return
|
||||||
|
|
||||||
t = np.arange(64) * common.Ts
|
I = Interpolator()
|
||||||
x = np.sin(2 * np.pi * 3.456e3 * t)
|
f = I.filt
|
||||||
r = Interpolator()
|
|
||||||
|
|
||||||
# pylab.figure()
|
|
||||||
y = []
|
|
||||||
k = 32 + np.linspace(-5, 5, 1001)
|
|
||||||
for offset in k:
|
|
||||||
h, i = r.get(offset)
|
|
||||||
y.append( np.dot(x[i:i+len(h)], h) )
|
|
||||||
|
|
||||||
pylab.figure()
|
pylab.figure()
|
||||||
pylab.plot(t, x, '.', k * common.Ts, y, '-')
|
pylab.plot(zip(*f[::100]))
|
||||||
pylab.show()
|
pylab.show()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user