Skip to content

Commit

Permalink
add tests for new Room class, update for changes in Filter and HRTF
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMarc committed Feb 22, 2024
1 parent 62dfbd7 commit 686194d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions tests/test_binaural.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def test_externalize():
(hrtf.sources.vertical_polar[:, 0] == 0))[0][0]
sound = slab.Binaural.whitenoise(samplerate=hrtf.samplerate)
filtered = hrtf.data[idx_frontal].apply(sound)
filtered = filtered.resize(sound.duration)
external = sound.externalize()
assert numpy.abs(filtered.data-external.data).sum() < numpy.abs(filtered.data-sound.data).sum()
assert numpy.abs(sound.level - external.level).max() < 0.6
Expand Down
8 changes: 4 additions & 4 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def test_low_high_pass():
for i in range(10):
sound = slab.Sound.whitenoise(duration=2.0)
for kind, fir in itertools.product(["lp", "hp"], [False, True]):
for kind, fir in itertools.product(["lp", "hp"], ['FIR', 'IR', 'TF']):
edge_freq = numpy.random.uniform(100, 2000)
length = numpy.random.randint(500, 5000)
filt = slab.Filter.band(frequency=edge_freq, length=length, kind=kind, fir=fir)
Expand All @@ -24,7 +24,7 @@ def test_low_high_pass():

def test_band_pass_stop():
sound = slab.Sound.whitenoise(duration=2.0)
for kind, fir in itertools.product(["bp", "bs"], [False, True]):
for kind, fir in itertools.product(["bp", "bs"], ['FIR', 'IR', 'TF']):
lower_edge_freq = numpy.random.uniform(100, 1000)
higher_edge_freq = lower_edge_freq + numpy.random.uniform(100, 1000)
length = numpy.random.randint(500, 5000)
Expand All @@ -50,7 +50,7 @@ def test_custom_band():
[1., 0., 1., 0., 0., 1., 0.]
]
for i in range(10):
for fir, gain in itertools.product([True, False], gains):
for fir, gain in itertools.product(['FIR', 'IR', 'TF'], gains):
freqs += numpy.random.uniform(1, 10, 7)
freqs.sort()
length = numpy.random.randint(500, 5000)
Expand Down Expand Up @@ -133,7 +133,7 @@ def test_load_save():
(0+numpy.random.uniform(100, 2000), 2000+numpy.random.uniform(100, 2000)),
(0 + numpy.random.uniform(100, 2000), 2000 + numpy.random.uniform(100, 2000))
]):
for fir in (True, False):
for fir in ('FIR', 'IR', 'TF'):
filt = slab.Filter.band(kind=kind, frequency=freq, fir=fir)
filt.save(tmpdir/"filt.npy")
loaded = slab.Filter.load(tmpdir/"filt.npy")
Expand Down
53 changes: 48 additions & 5 deletions tests/test_hrtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def test_create_hrtf():
hrtf1 = slab.HRTF.kemar()
assert hrtf1.data[0].fir is True
assert hrtf1.data[0].fir == 'IR'
hrtf2 = slab.HRTF.kemar()
for i in range(len(hrtf1.data)):
numpy.testing.assert_equal(hrtf1[i].data, hrtf2[i].data)
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_plot_hrtf():
def test_diffuse_field():
hrtf = slab.HRTF.kemar()
dfs = hrtf.diffuse_field_avg()
assert dfs.fir is False
assert dfs.fir == 'IR'
assert dfs.n_frequencies == hrtf.data[0].n_taps
equalized = hrtf.diffuse_field_equalization()
for i in range(hrtf.n_sources):
Expand Down Expand Up @@ -120,8 +120,8 @@ def test_interpolate():
idx = numpy.random.choice(range(len(hrtf.sources.vertical_polar)))
azi, ele = hrtf.sources.vertical_polar[idx][0:2]
method = numpy.random.choice(['nearest', 'bary'])
h = hrtf.interpolate(azimuth=azi, elevation=ele, method=method)
_, spec_interp = h[0].tf(show=False)
filt = hrtf.interpolate(azimuth=azi, elevation=ele, method=method)
_, spec_interp = filt.tf(show=False)
_, spec_origin = hrtf[idx].tf(show=False)
nearer_channel = 0 if azi-180 < 0 else 1
# assert numpy.corrcoef(spec_interp[:, nearer_channel], spec_origin[:, nearer_channel]).min() > 0.99
Expand Down Expand Up @@ -162,4 +162,47 @@ def test_estimate_hrtf():
signal2 = slab.Sound.whitenoise(duration=duration-0.1, samplerate=fs-800)
hrtf2 = slab.HRTF.estimate_hrtf(recordings, signal2, sources)
assert hrtf1.samplerate == hrtf2.samplerate
assert hrtf1[0].n_samples == hrtf2[0].n_samples
assert hrtf1[0].n_samples == hrtf2[0].n_samples

def test_create_room():
for i in range(10):
size = numpy.random.randint(2, 51, 3)
listener = size / 2
source_cart = numpy.array([numpy.random.randint(1,i) for i in size])
source_pol = slab.HRTF._get_coordinates(source_cart-listener, 'cartesian')[1][0]
source_pol[0] -= 180
max_echos = numpy.random.randint(1, 250)
room = slab.Room(size=size, listener=listener, source=source_pol, order=1, max_echos=max_echos)
numpy.testing.assert_almost_equal(source_pol[1:], room.image_locs[0,1:], decimal=0)
room = slab.Room(size=[3,3,3], listener=[1,1,1]) # lists work?
print(room) # __str__ ok?

def test_reverb_time():
room = slab.Room()
t = room.reverb_time()
t = room.reverb_time([5,5,5]) # list ok?
assert isinstance(t, float)
for _ in range(10):
size = numpy.random.randint(1, 100, 3)
t = room.reverb_time(size=size)

def test_hrir():
for i in range(5):
size = numpy.random.randint(2, 51, 3)
listener = size / 2
source_cart = numpy.array([numpy.random.randint(1,i) for i in size])
source_pol = slab.HRTF._get_coordinates(source_cart-listener, 'cartesian')[1][0]
source_pol[0] -= 180
max_echos = numpy.random.randint(1, 50)
room = slab.Room(size=size, listener=listener, source=source_pol, order=1, max_echos=max_echos)
hrir = room.hrir()
for i in range(5):
tail = room.reverb(t_reverb=numpy.random.random()*3) # up to T60 = 3s
hrir = room.hrir(reverb=tail) # try hrir with premade tail
assert isinstance(hrir, slab.Filter)
for _ in range(5):
trim = numpy.random.randint(1, hrir.n_samples)
hrir_tmp = room.hrir(trim=trim) # try integer trim
hrir_tmp = room.hrir(trim=numpy.random.random()) # try float trim
sig = slab.Sound.vowel(duration=0.2, samplerate=hrir.samplerate)
out = hrir.apply(sig)

0 comments on commit 686194d

Please sign in to comment.