34 lines
1 KiB
Python
34 lines
1 KiB
Python
"""Spectrogram DSP tests (synthetic signals, no ffmpeg)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
|
|
from extractors.features import spectrogram
|
|
|
|
|
|
def test_compute_features_on_tone():
|
|
sr = 16_000
|
|
t = np.linspace(0, 1.0, sr, endpoint=False)
|
|
pcm = 0.5 * np.sin(2 * np.pi * 440 * t).astype(np.float32)
|
|
feats = spectrogram.compute_features(pcm, sr)
|
|
assert feats["sample_rate"] == sr
|
|
assert abs(feats["duration_s"] - 1.0) < 0.01
|
|
assert feats["spectral_centroid_hz_mean"] > 0
|
|
assert feats["discontinuities_s"] == []
|
|
|
|
|
|
def test_detects_energy_discontinuity():
|
|
sr = 16_000
|
|
quiet = np.full(sr // 2, 0.001, dtype=np.float32)
|
|
loud = (0.6 * np.sin(2 * np.pi * 300 * np.linspace(0, 0.5, sr // 2))).astype(
|
|
np.float32
|
|
)
|
|
pcm = np.concatenate([quiet, loud])
|
|
feats = spectrogram.compute_features(pcm, sr)
|
|
assert len(feats["discontinuities_s"]) >= 1
|
|
|
|
|
|
def test_empty_signal():
|
|
feats = spectrogram.compute_features(np.array([], dtype=np.float32), 16_000)
|
|
assert feats["frames"] == 0
|