mirror of
https://github.com/romanz/amodem.git
synced 2026-05-10 05:17:38 +08:00
refactor sampling module
This commit is contained in:
21
amodem/sampling.py
Executable file → Normal file
21
amodem/sampling.py
Executable file → Normal file
@@ -43,7 +43,8 @@ class Sampler(object):
|
|||||||
|
|
||||||
def take(self, size):
|
def take(self, size):
|
||||||
frame = np.zeros(size)
|
frame = np.zeros(size)
|
||||||
|
count = 0
|
||||||
|
try:
|
||||||
for frame_index in range(size):
|
for frame_index in range(size):
|
||||||
offset = self.offset
|
offset = self.offset
|
||||||
# offset = k + (j / self.resolution)
|
# offset = k + (j / self.resolution)
|
||||||
@@ -58,19 +59,17 @@ class Sampler(object):
|
|||||||
|
|
||||||
self.offset += self.freq
|
self.offset += self.freq
|
||||||
frame[frame_index] = np.dot(coeffs, self.buff) * self.gain
|
frame[frame_index] = np.dot(coeffs, self.buff) * self.gain
|
||||||
|
count = frame_index + 1
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
|
||||||
return frame
|
return frame[:count]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def resample(src, dst, df=0.0):
|
||||||
import common
|
import common
|
||||||
import sys
|
x = common.load(src)
|
||||||
df, = sys.argv[1:]
|
|
||||||
df = float(df)
|
|
||||||
|
|
||||||
x = common.load(sys.stdin)
|
|
||||||
sampler = Sampler(x, Interpolator())
|
sampler = Sampler(x, Interpolator())
|
||||||
sampler.freq += df
|
sampler.freq += df
|
||||||
y = np.array(list(sampler))
|
y = sampler.take(len(x))
|
||||||
y = common.dumps(y)
|
dst.write(common.dumps(y))
|
||||||
sys.stdout.write(y)
|
|
||||||
|
|||||||
4
scripts/resample.py
Executable file
4
scripts/resample.py
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
from amodem.sampling import resample
|
||||||
|
import sys
|
||||||
|
|
||||||
|
resample(src=sys.stdin, dst=sys.stdout, df=float(sys.argv[1]))
|
||||||
@@ -1,14 +1,22 @@
|
|||||||
from amodem import sampling
|
from amodem import sampling
|
||||||
|
from amodem import common
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
|
||||||
def test_resample():
|
def test_resample():
|
||||||
x = np.arange(300)
|
x = np.sin(2*np.pi * 10 * np.linspace(0, 1, 1001))
|
||||||
s = sampling.Sampler(x, interp=sampling.Interpolator())
|
src = BytesIO(common.dumps(x))
|
||||||
y = s.take(len(x) - s.interp.width - 1)
|
dst = BytesIO()
|
||||||
|
sampling.resample(src=src, dst=dst, df=0.0)
|
||||||
|
y = common.loads(dst.getvalue())
|
||||||
err = x[1:len(y)+1] - y
|
err = x[1:len(y)+1] - y
|
||||||
assert np.max(np.abs(err)) < 1e-10
|
assert np.max(np.abs(err)) < 1e-4
|
||||||
|
|
||||||
|
dst = BytesIO()
|
||||||
|
sampling.resample(src=BytesIO('\x00\x00'), dst=dst, df=0.0)
|
||||||
|
assert dst.tell() == 0
|
||||||
|
|
||||||
|
|
||||||
def test_coeffs():
|
def test_coeffs():
|
||||||
|
|||||||
Reference in New Issue
Block a user