File size: 2,922 Bytes
a158d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np

from smart.python_speech_features.silence_detect import detect_silence


def calc_wave_features(signal, sample_rate):
    assert signal.dtype == np.int16
    assert sample_rate == 8000

    signal = np.array(signal, dtype=np.float32)
    # plt.plot(signal)
    # plt.show()

    l = len(signal)

    # 均值
    mean = np.mean(signal)

    # 方差
    var = np.var(signal)

    # 百分位数
    per = np.percentile(signal, q=[1, 25, 50, 75, 99])
    per1, per25, per50, per75, per99 = per

    # 静音段占比
    silences = detect_silence(
        signal=signal,
        samplerate=sample_rate,
        min_energy=120,
        min_cross_zero_rate=0.01
    )
    silence_total = 0
    for silence in silences:
        li = silence[1] - silence[0]
        silence_total += li
    silence_rate = silence_total / l

    # 非静音段方差
    last_e = 0
    non_silences = list()
    for silence in silences:
        b, e = silence
        if b > last_e:
            non_silences.append([last_e, b])
        last_e = e
    else:
        if l > last_e:
            non_silences.append([last_e, l])

    # 静音段的数量
    silence_count = len(non_silences)

    if silence_count == 0:
        mean_non_silence = 0
        var_non_silence = 0
        var_var_non_silence = 0
        var_non_silence_rate = 1
    else:
        signal_non_silences = list()
        for non_silence in non_silences:
            b, e = non_silence
            signal_non_silences.append(signal[b: e])

        # 非静音段, 各段方差的方差.
        v = list()
        for signal_non_silence in signal_non_silences:
            v.append(np.var(signal_non_silence))
        var_var_non_silence = np.var(v)

        signal_non_silences = np.concatenate(signal_non_silences)
        # 非静音段整体均值
        mean_non_silence = np.mean(signal_non_silences)
        # 非静音段整体方差
        var_non_silence = np.var(signal_non_silences)
        # 非静音段整体方差 除以 整体方差
        var_non_silence_rate = var_non_silence / var

    # 全段, 分段方差的方差
    sub_signal_list = np.split(signal, 20)

    whole_var = list()
    for sub_signal in sub_signal_list:
        sub_var = np.var(sub_signal)
        whole_var.append(sub_var)
    var_var_whole = np.var(whole_var)

    result = {
        'mean': mean,
        'var': var,
        'per1': per1,
        'per25': per25,
        'per50': per50,
        'per75': per75,
        'per99': per99,
        'silence_rate': silence_rate,
        'mean_non_silence': mean_non_silence,
        'silence_count': silence_count,
        'var_var_non_silence': var_var_non_silence,
        'var_non_silence': var_non_silence,
        'var_non_silence_rate': var_non_silence_rate,
        'var_var_whole': var_var_whole,

    }
    return result


if __name__ == '__main__':
    pass