Skip to content

Commit

Permalink
Fixed per-frame angle calculation and added tests (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc1701 authored Oct 30, 2023
1 parent 7d0b3d9 commit 21593b9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tests/test_flight_2.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
segment, startxyz, endxyz, speeds
constant-speed flyby, -10 20 30, 10 20 30, 30 30
constant-speed flyby, 0 -10 30, 0 10 30, 30 30
2 changes: 2 additions & 0 deletions tests/test_flight_3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
segment, startxyz, endxyz, speeds
constant-speed flyby, -10 20 30, 10 20 30, 30 30
12 changes: 11 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def __init__(self, methodName: str = "runTest") -> None:
renderer_2 = UASEventRenderer(params_2, 'asphalt', fs, 1.5)
self.xout_2 = renderer_2.render(self.x)

params_3 = utils.load_params('tests/test_flight_3.csv')
renderer_3 = UASEventRenderer(params_3, 'asphalt', fs, 1.5)
self.xout_3 = renderer_3.render(self.x)

def test_output_sensible(self):
# rendering should equal length of calculated trajectory
self.assertEqual(len(self.renderer._flightpath.T), len(self.xout))
Expand All @@ -123,4 +127,10 @@ def test_output_sensible(self):
self.assertTrue((self.renderer.r[:n] == 0).all())

# rendering of greater distance should have lower max amplitude
self.assertGreater(np.max(abs(self.xout)), np.max(abs(self.xout_2)))
self.assertGreater(np.max(abs(self.xout)), np.max(abs(self.xout_3)))

# rendering of flight along Y should result in zero signal in Y channel
self.assertAlmostEqual(self.xout.T[1].sum(), 0)

# rendering of flight along X should result in zero signal in X channel
self.assertAlmostEqual(self.xout_2.T[3].sum(), 0)
20 changes: 11 additions & 9 deletions uasevent/environment_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,27 @@ def __init__(
):

self.max_amp = max_amp
self.c = c
self.fs = fs
self.reflection_surface = reflection_surface
self.theta, self.phi, self.r = utils.cart_to_sph(flightpath)

# calculate delays and amplitude curve
delays = (self.r / self.c) * self.fs
_, _, r = utils.cart_to_sph(flightpath)
delays = (r / c) * self.fs
self.init_delay = delays[0]
self.delta_delays = np.diff(delays)
self.amp_env = 1 / (self.r**2)
self.amp_env = 1 / (r**2)

self.frame_len = frame_len
self.hop_len = frame_len // 2
self.phi_per_frame = np.lib.stride_tricks.sliding_window_view(
self.phi, self.frame_len)[::self.hop_len].mean(1)
self.theta_per_frame = np.lib.stride_tricks.sliding_window_view(
self.theta, self.frame_len)[::self.hop_len].mean(1)
self.N = N

# calculate angles per frame for reflection filter and spatialisation
position_per_frame = np.lib.stride_tricks.sliding_window_view(
flightpath, self.frame_len, 1)[:, ::self.hop_len].mean(2)

self.theta_per_frame, self.phi_per_frame, _ = \
utils.cart_to_sph(position_per_frame)

def apply_doppler(self, x):
# init output array and initial read position
out = np.zeros(len(self.delta_delays) + 1)
Expand Down Expand Up @@ -192,7 +194,7 @@ def spatialise(self, x):
return x_out

def process(self, x):
if len(x) < len(self.r):
if len(x) < len(self.delta_delays + 1):
raise ValueError('Input signal shorter than path to be rendered')

return \
Expand Down

0 comments on commit 21593b9

Please sign in to comment.