58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
import unittest
|
|
|
|
import numpy as np
|
|
|
|
from calibrate import EXCITATION, GRID, RATE, filter_curve, fit, probe, response
|
|
|
|
|
|
def artificial_recording(drift=1.0):
|
|
x = probe().astype(float) / 32768
|
|
n = 1 << (len(x) - 1).bit_length()
|
|
f = np.fft.rfftfreq(n, 1 / RATE)
|
|
omega = 2 * np.pi * f / RATE
|
|
omega0 = 2 * np.pi * 315 / RATE
|
|
a = 10 ** (6 / 40)
|
|
alpha = np.sin(omega0) / (2 * 1.3)
|
|
z = np.exp(-1j * omega)
|
|
h = ((1 + alpha*a) - 2*np.cos(omega0)*z + (1-alpha*a)*z**2) / (
|
|
(1 + alpha/a) - 2*np.cos(omega0)*z + (1-alpha/a)*z**2
|
|
)
|
|
y = np.fft.irfft(np.fft.rfft(x, n) * h, n)[:len(x)]
|
|
y = np.interp(np.arange(round(len(y)*drift)) / drift, np.arange(len(y)), y)
|
|
y = np.r_[np.zeros(RATE), y, np.zeros(RATE)]
|
|
return 0.05 * y + np.random.default_rng(3).normal(0, 2e-6, len(y))
|
|
|
|
|
|
class CalibrationTest(unittest.TestCase):
|
|
def test_recovers_peak_and_clock_drift(self):
|
|
trace, quality = response(artificial_recording(1.0005))
|
|
self.assertGreater(quality['snrDb'], 25)
|
|
self.assertAlmostEqual(quality['driftPpm'], 500, delta=30)
|
|
self.assertGreater(trace[np.argmin(abs(GRID - 315))], 4)
|
|
self.assertLess(abs(trace[np.argmin(abs(GRID - 2000))]), 1)
|
|
|
|
def test_fits_shared_peak_without_boosting_dips(self):
|
|
trace, _ = response(artificial_recording())
|
|
traces = np.stack([trace, trace - 0.2, trace + 0.3])
|
|
_, _, _, curve, filters, preamp, dry = fit(traces, False)
|
|
self.assertTrue(filters)
|
|
self.assertLessEqual(curve.max(), 1e-6)
|
|
self.assertGreaterEqual(curve.min(), -6.01)
|
|
self.assertLess(curve[np.argmin(abs(GRID-315))], -1.5)
|
|
self.assertLessEqual(dry, preamp)
|
|
self.assertGreaterEqual(filter_curve(315, 1.3, -6).min(), -6.0001)
|
|
|
|
def test_refuses_clipping_and_unsynchronized_audio(self):
|
|
audio = artificial_recording()
|
|
audio[0] = 1
|
|
with self.assertRaisesRegex(ValueError, 'перегружен'):
|
|
response(audio)
|
|
with self.assertRaises(ValueError):
|
|
response(np.zeros(len(EXCITATION) * 2))
|
|
with self.assertRaisesRegex(ValueError, 'синхрометки'):
|
|
response(artificial_recording(1.02))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|