File size: 1,092 Bytes
391be2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
    }