Skip to content

Commit b676d2f

Browse files
trojanjaymgeier
authored andcommitted
Test for util.db, util.direction_vector
This is a part of #61
1 parent b4bce72 commit b676d2f

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

sfs/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def db(x, power=False):
361361
362362
"""
363363
with np.errstate(divide='ignore'):
364-
return 10 if power else 20 * np.log10(np.abs(x))
364+
return (10 if power else 20) * np.log10(np.abs(x))
365365

366366

367367
class XyzComponents(np.ndarray):

tests/test_util.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
import sfs
55

66
cart_sph_data = [
7-
((1, 1, 1), (np.pi/4, np.arccos(1/np.sqrt(3)), np.sqrt(3))),
8-
((-1, 1, 1), (np.arctan2(1, -1), np.arccos(1/np.sqrt(3)), np.sqrt(3))),
9-
((1, -1, 1), (-np.pi/4, np.arccos(1/np.sqrt(3)), np.sqrt(3))),
10-
((-1, -1, 1), (np.arctan2(-1, -1), np.arccos(1/np.sqrt(3)), np.sqrt(3))),
11-
((1, 1, -1), (np.pi/4, np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
12-
((-1, 1, -1), (np.arctan2(1, -1), np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
13-
((1, -1, -1), (-np.pi/4, np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
14-
((-1, -1, -1), (np.arctan2(-1, -1), np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
7+
((1, 1, 1), (np.pi / 4, np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
8+
((-1, 1, 1), (np.arctan2(1, -1), np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
9+
((1, -1, 1), (-np.pi / 4, np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
10+
((-1, -1, 1), (np.arctan2(-1, -1), np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
11+
((1, 1, -1), (np.pi / 4, np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
12+
((-1, 1, -1), (np.arctan2(1, -1), np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
13+
((1, -1, -1), (-np.pi / 4, np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
14+
((-1, -1, -1), (np.arctan2(-1, -1),
15+
np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
1516
]
1617

1718

@@ -27,3 +28,48 @@ def test_sph2cart(coord, polar):
2728
alpha, beta, r = polar
2829
b = sfs.util.sph2cart(alpha, beta, r)
2930
assert_allclose(b, coord)
31+
32+
33+
direction_vector_data = [
34+
((np.pi / 4, np.pi / 4), (0.5, 0.5, np.sqrt(2) / 2)),
35+
((3 * np.pi / 4, 3 * np.pi / 4), (-1 / 2, 1 / 2, -np.sqrt(2) / 2)),
36+
((3 * np.pi / 4, -3 * np.pi / 4), (1 / 2, -1 / 2, -np.sqrt(2) / 2)),
37+
((np.pi / 4, -np.pi / 4), (-1 / 2, -1 / 2, np.sqrt(2) / 2)),
38+
((-np.pi / 4, np.pi / 4), (1 / 2, -1 / 2, np.sqrt(2) / 2)),
39+
((-3 * np.pi / 4, 3 * np.pi / 4), (-1 / 2, -1 / 2, -np.sqrt(2) / 2)),
40+
((-3 * np.pi / 4, -3 * np.pi / 4), (1 / 2, 1 / 2, -np.sqrt(2) / 2)),
41+
((-np.pi / 4, -np.pi / 4), (-1 / 2, 1 / 2, np.sqrt(2) / 2)),
42+
]
43+
44+
45+
@pytest.mark.parametrize('input, vector', direction_vector_data)
46+
def test_direction_vector(input, vector):
47+
alpha, beta = input
48+
c = sfs.util.direction_vector(alpha, beta)
49+
assert_allclose(c, vector)
50+
51+
52+
db_data = [
53+
(0, -np.inf),
54+
(1, 0),
55+
(10, 10),
56+
(10 * 2, (10 + 3.010299956639813)),
57+
(10 * 10, (10 + 10)),
58+
(10 * 3, (10 + 4.771212547196624)),
59+
(10 * 4, (10 + 6.02059991327962)),
60+
(10 * 0.5, (10 - 3.01029995663981198)),
61+
(10 * 0.1, (10 - 10)),
62+
(10 * 0.25, (10 - 6.02059991327962396))
63+
]
64+
65+
66+
@pytest.mark.parametrize('linear, decibel', db_data)
67+
def test_db_amplitude(linear, decibel):
68+
d = sfs.util.db(linear, True)
69+
assert_allclose(d, decibel)
70+
71+
72+
@pytest.mark.parametrize('linear, decibel', db_data)
73+
def test_db_power(linear, decibel):
74+
d = sfs.util.db(linear)
75+
assert_allclose(d, 2 * decibel)

0 commit comments

Comments
 (0)