Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wattai committed Nov 3, 2024
1 parent d2660be commit c29e3ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/sse/music_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def fit_transform(
X_freq.conj().reshape(-1, self.N_ch, 1) @ X_freq.reshape(-1, 1, self.N_ch)
) / (X_freq.shape[0] / self.fs)

self.eigval, self.eigvec = np.linalg.eig(self.R)
# self.eigval, self.eigvec = np.linalg.eig(self.R)
self.eigvec, self.eigval, _ = np.linalg.svd(self.R)

self.minvec = self.eigvec[:, :, -1].reshape(-1, self.N_ch, 1)

self.thetas = np.linspace(-np.pi / 2, np.pi / 2, self.N_theta)
Expand Down
38 changes: 33 additions & 5 deletions tests/test_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,40 @@
from sse.music_v2 import MusicSoundSourceLocator


def make_dummy_signals(
theta=15.0,
fs=16000,
N_fft=128,
c=340,
d=1.0,
) -> np.ndarray:
tdoa = d * np.sin(np.deg2rad(theta)) / c
# tdoa = d * np.cos(np.deg2rad(theta)) / c

width = N_fft // 2
# s = np.random.randn(N_fft + 2*width) # signal source.
ptdoa = np.round(tdoa * fs).astype("i") # point of TDOA.
t = np.linspace(0, N_fft + 2 * width - 1, N_fft + 2 * width) / fs # base time.
t1 = t[width + ptdoa : width + N_fft + ptdoa]
t2 = t[width : width + N_fft]
x1 = np.sin(2 * np.pi * 500 * t1)[:, None]
x2 = np.sin(2 * np.pi * 500 * t2)[:, None]
x = np.c_[x1, x2]
return x


class TestMusicSoundSourceLocator:
def test_fit_transform(self):
self.locator = MusicSoundSourceLocator(
fs=16000,
d=1.0,
fs=48000,
d=0.1,
N_theta=180,
)
X = make_dummy_signals(
theta=40.0,
fs=48000,
N_fft=2048,
d=0.1,
)
X = np.random.randn(1000, 2)
out = self.locator.fit_transform(X=X)
print(out)
predicted_theta = self.locator.fit_transform(X=X)
np.testing.assert_allclose(predicted_theta, 42.737430)

0 comments on commit c29e3ef

Please sign in to comment.