audio_analyzer / time_domain.py
Mr7Explorer's picture
Update time_domain.py
ccdac71 verified
import numpy as np
import librosa
def compute_time_domain_stats(y):
"""Calculate time-domain statistics exactly as in original analyzer."""
# Peak amplitude
peak = float(np.max(np.abs(y)))
# RMS amplitude
rms = float(np.sqrt(np.mean(y ** 2)))
# dB conversions (protecting against log(0))
peak_db = 20 * np.log10(max(peak, 1e-12))
rms_db = 20 * np.log10(max(rms, 1e-12))
# Crest factor (indicator of compression)
crest_factor = peak_db - rms_db
# Noise floor estimate (10th percentile absolute amplitude)
abs_y = np.abs(y)
noise_floor = float(np.percentile(abs_y, 10))
# Estimated SNR
snr_est = 20 * np.log10(max(rms, 1e-12) / max(noise_floor, 1e-12))
# Zero-crossing rate (speech/noise indicator)
zcr = float(np.mean(librosa.feature.zero_crossing_rate(y)))
return {
"peak": peak,
"rms": rms,
"peak_db": peak_db,
"rms_db": rms_db,
"crest_factor_db": crest_factor,
"noise_floor": noise_floor,
"snr_db": snr_est,
"zero_crossing_rate": zcr
}