-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
172 lines (163 loc) · 6.2 KB
/
Copy pathfunction.py
File metadata and controls
172 lines (163 loc) · 6.2 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Function: 音频处理函数
import librosa
import numpy as np
from matplotlib import pyplot as plt
import librosa.display
import librosa.feature
def plot_waveform_and_spectrum(audio_data, sample_rate):
# 默认参数
n_fft = 512
win_length = 512
hop_length = 256
# 也可以用计算公式
# frame_t = 25 # 25ms 帧长
# hop_length_t = 10 # 10ms 步近
# win_length = int(sample_rate * frame_t / 1000) # 在该采样率下25ms的帧长对应的窗长
# hop_length = int(sample_rate * hop_length_t / 1000) # 在该采样率下10ms的步近对应的帧移
# n_fft = int(2**np.ceil(np.log2(win_length))) # FFT点数
# 波形
plt.figure()
plt.subplot(2, 1, 1)
librosa.display.waveshow(audio_data, sr=sample_rate)
plt.title("波形")
plt.xlabel("时间 (s)")
plt.ylabel("幅度")
# 频谱
plt.subplot(2, 1, 2)
audio_stft = librosa.stft(audio_data, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
audio_stft_db = librosa.amplitude_to_db(np.abs(audio_stft))
librosa.display.specshow(audio_stft_db, sr=sample_rate, hop_length=hop_length, y_axis='linear', cmap='viridis')
plt.colorbar(format='%+2.0f dB')
plt.title('频谱')
plt.tight_layout()
plt.show()
# 消除静音
def remove_mute(audio_data, sample_rate, top_db, position):
if position == 'both':
audio_data_t, index = librosa.effects.trim(audio_data, top_db=top_db, frame_length=1024, hop_length=256)
# 波形静音前后显示
plt.figure()
plt.subplot(2, 1, 1)
librosa.display.waveshow(audio_data, sr=sample_rate)
plt.axvline(float(index[0]/sample_rate), -0.5,1, color='r')
plt.axvline(float(index[1]/sample_rate), -0.5,1, color='r')
plt.title("原声")
plt.xlabel("时间 (s)")
plt.ylabel("幅度")
plt.subplot(2, 1, 2)
librosa.display.waveshow(audio_data_t, sr=sample_rate)
# 画静音前后的线
plt.title("去除静音")
plt.xlabel("时间 (s)")
plt.ylabel("幅度")
plt.tight_layout()
plt.show()
return audio_data_t
elif position == 'all':
# 去除内部噪音
# 分割
intervals = librosa.effects.split(audio_data, top_db=20)
# 重组
audio_data_s = librosa.effects.remix(audio_data, intervals)
# 波形静音前后显示
plt.figure()
plt.subplot(2, 1, 1)
librosa.display.waveshow(audio_data, sr=sample_rate)
for index in intervals:
plt.axvline(float(index[0]/sample_rate), -0.5,1, color='r')
plt.axvline(float(index[1]/sample_rate), -0.5,1, color='r')
plt.title("原声")
plt.xlabel("时间 (s)")
plt.ylabel("幅度")
plt.subplot(2, 1, 2)
librosa.display.waveshow(audio_data_s, sr=sample_rate)
# 画静音前后的线
plt.title("去除静音")
plt.xlabel("时间 (s)")
plt.ylabel("幅度")
plt.tight_layout()
plt.show()
return audio_data_s
# 预加重 一阶差分 弥补高频信号能量损失
def pre_emphasis(audio_data, sample_rate ,coef=0.97):
# 默认参数
n_fft = 512
win_length = 512
hop_length = 256
audio_pre = librosa.effects.preemphasis(audio_data,coef=coef)
# 原声STFT
audio_stft = librosa.stft(audio_data, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
audio_stft_db = librosa.amplitude_to_db(np.abs(audio_stft))
# 预加重频域STFT
audio_pre_stft = librosa.stft(audio_pre, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
audio_pre_stft_db = librosa.amplitude_to_db(np.abs(audio_pre_stft))
# 显示
plt.figure()
plt.subplot(2, 1, 1)
librosa.display.specshow(audio_stft_db, sr=sample_rate, hop_length=hop_length, y_axis='linear', x_axis='time', cmap='viridis')
plt.colorbar(format='%+2.0f dB')
plt.title('原声频域STFT')
plt.xlabel('时间 (s)')
plt.ylabel('频率 (Hz)')
plt.subplot(2, 1, 2)
librosa.display.specshow(audio_pre_stft_db, sr=sample_rate, hop_length=hop_length, y_axis='linear', x_axis='time', cmap='viridis')
plt.colorbar(format='%+2.0f dB')
plt.title('预加重频域STFT')
plt.xlabel('时间 (s)')
plt.ylabel('频率 (Hz)')
plt.tight_layout()
plt.show()
return audio_pre
# fbank特征提取
def fbank_feature(audio_data, sample_rate, n_mels):
# 默认参数
n_fft = 512
win_length = 512
hop_length = 256
# 梅尔滤波器组 n_mels = 40
# 计算fbank特征
fbank = librosa.feature.melspectrogram(y=audio_data, sr=sample_rate, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
fbank_db = librosa.power_to_db(fbank)
# 显示
plt.figure()
librosa.display.specshow(fbank_db, sr=sample_rate, y_axis='mel', x_axis='time')
plt.colorbar(format='%+2.0f dB')
plt.title('fbank特征')
plt.tight_layout()
plt.show()
# mfcc特征提取
def mfcc_feature(audio_data, sample_rate, n_mfcc, vstack):
# 默认参数
n_fft = 512
win_length = 512
hop_length = 256
# 计算mfcc特征
mfcc = librosa.feature.mfcc(y=audio_data, sr=sample_rate, n_mfcc=n_mfcc, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
if vstack:
# 特征拼接与差分
# 一阶差分
mfcc_delta = librosa.feature.delta(mfcc)
# 二阶差分
mfcc_delta2 = librosa.feature.delta(mfcc, order=2)
# 拼接
mfcc_all = np.vstack((mfcc, mfcc_delta, mfcc_delta2))
# 显示
plt.figure()
plt.subplot(2, 1, 1)
librosa.display.specshow(mfcc, sr=sample_rate, hop_length=hop_length, x_axis='time')
plt.colorbar()
plt.title('mfcc梅尔频率倒谱系数特征')
plt.subplot(2, 1, 2)
librosa.display.specshow(mfcc_all, sr=sample_rate, hop_length=hop_length, x_axis='time')
plt.colorbar()
plt.title('mfcc差分特征拼接')
plt.tight_layout()
plt.show()
else:
# 显示mfcc特征
plt.figure()
librosa.display.specshow(mfcc, sr=sample_rate, hop_length=hop_length, x_axis='time')
plt.colorbar()
plt.title('mfcc梅尔频率倒谱系数特征')
plt.tight_layout()
plt.show()