test_twopart.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. """Unit tests of round 190nm's fitting additions: two components per partial (modal_analysis.fit_two: a two-stage
  3. decay, a beating pair, a pair starting in antiphase), the tracking window's correction of a fast partial's level
  4. (modal_analysis.window_gain) and the LPM antiphase bit.
  5. python3 -m unittest discover -s tools/physmod -p 'test_*.py'
  6. """
  7. import os
  8. import sys
  9. import unittest
  10. import numpy as np
  11. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  12. import lpm # noqa: E402
  13. import modal_analysis as ma # noqa: E402
  14. SR = 44100
  15. def two(f1, f2, t1, t2, a1, a2, seconds=6.0, noise=1e-5):
  16. t = np.arange(int(seconds * SR)) / SR
  17. rng = np.random.default_rng(3)
  18. return (a1 * np.exp(-6.9078 * t / t1) * np.sin(2 * np.pi * f1 * t) + a2 * np.exp(-6.9078 * t / t2) * np.sin(2 * np.pi * f2 * t)
  19. + noise * rng.standard_normal(len(t)))
  20. def fit(x, f=500.0):
  21. win = ma.track_window(f, [3 * f], SR)
  22. t, z = ma.track_complex(x, SR, f, win, int(0.005 * SR))
  23. return ma.fit_two(t, z, ma.fit_decay(t, np.abs(z)))
  24. class TwoPartTests(unittest.TestCase):
  25. def test_two_stage_decay(self):
  26. r = fit(two(500, 500, 0.5, 6.0, 1.0, 0.3))
  27. self.assertIsNotNone(r)
  28. fast, slow = sorted(r['parts'], key=lambda p: p['t60'])
  29. self.assertAlmostEqual(fast['t60'], 0.5, delta=0.03)
  30. self.assertAlmostEqual(slow['t60'], 6.0, delta=0.3)
  31. self.assertAlmostEqual(slow['a0'] / fast['a0'], 0.3, delta=0.03)
  32. self.assertTrue(all(abs(p['df']) < 0.05 for p in r['parts']))
  33. self.assertLess(r['rms_db'], r['rms_db_single'] - 1.0)
  34. def test_beating_pair(self):
  35. r = fit(two(500, 500.8, 4.0, 4.0, 1.0, 0.7))
  36. self.assertIsNotNone(r)
  37. a, b = sorted(r['parts'], key=lambda p: p['df'])
  38. self.assertAlmostEqual(b['df'] - a['df'], 0.8, delta=0.02)
  39. self.assertAlmostEqual(b['a0'] / a['a0'], 0.7, delta=0.05)
  40. self.assertEqual(a['sign'] * b['sign'], 1.0)
  41. def test_antiphase_pair(self):
  42. r = fit(two(500, 500.5, 3.0, 5.0, 1.0, -0.5))
  43. self.assertIsNotNone(r)
  44. self.assertEqual(r['parts'][0]['sign'] * r['parts'][1]['sign'], -1.0)
  45. def test_single_decay_stays_single(self):
  46. self.assertIsNone(fit(two(500, 500, 3.0, 3.0, 1.0, 0.0)))
  47. def test_window_gain(self):
  48. self.assertAlmostEqual(ma.window_gain(100.0, 4096, SR), 1.0, delta=1e-3)
  49. # a fast partial: the window reads it louder than at its centre, as a direct sum shows
  50. t60, win = 0.15, 13000
  51. w = np.hanning(win)
  52. n = (np.arange(win) - (win - 1) / 2.0) / SR
  53. g = (w * np.exp(-6.9078 / t60 * n)).sum() / w.sum()
  54. self.assertAlmostEqual(ma.window_gain(t60, win, SR), g, places=6)
  55. self.assertGreater(ma.window_gain(t60, win, SR), 10.0)
  56. def test_fast_partial_level(self):
  57. # a 0.15 s partial next to a neighbour 20 Hz away (a long tracking window): the corrected level is right
  58. x = two(1300, 1280, 0.15, 0.3, 1.0, 0.01, seconds=2.0, noise=1e-7)
  59. modes = ma.analyse_modes(x, SR, fmin=1000, k=4, prom_db=6.0, window_exact=True)
  60. main = max(modes, key=lambda m: m['a0'])
  61. self.assertAlmostEqual(20 * np.log10(main['a0']), 0.0, delta=1.5)
  62. def test_lpm_antiphase_bit(self):
  63. a = {'key': 60.0, 'velocity': 1.0, 'gainDb': -20.0, 'contact': 0.0005, 'riseMs': 0.0, 'noiseDb': -20.0, 'noiseHz': 2000.0,
  64. 'noiseQ': 1.0, 'noiseMs': 10.0, 'modes': [{'ratio': 1.0, 'ampDb': 0.0, 't60': 1.0, 'shape': 3},
  65. {'ratio': 1.001, 'ampDb': -6.0, 't60': 2.0, 'shape': 3, 'invert': True}]}
  66. t = lpm.read(lpm.write(None, 'tubularbells', 0, 0.0, 0.02, 0.35, 0.0, [a], b''))
  67. m0, m1 = t['anchors'][0]['modes']
  68. self.assertEqual((m0['shape'], m1['shape']), (3, 3))
  69. self.assertFalse(m0.get('invert', False))
  70. self.assertTrue(m1['invert'])
  71. if __name__ == '__main__':
  72. unittest.main()