轉自:https://github.com/itaa/soja-box/tree/master/enhance_speach
譜減法語音降噪
?
#!/usr/bin/env python
import numpy as np
import wave
import nextpow2
import math
# 打開WAV文檔
f = wave.open("input_file.wav")
# 讀取格式信息
# (nchannels, sampwidth, framerate, nframes, comptype, compname)
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
fs = framerate
# 讀取波形數據
str_data = f.readframes(nframes)
f.close()
# 將波形數據轉換為數組
x = np.fromstring(str_data, dtype=np.short)
# 計算參數
len_ = 20 * fs // 1000 # 樣本中幀的大小
PERC = 50 # 窗口重疊占幀的百分比
len1 = len_ * PERC // 100 # 重疊窗口
len2 = len_ - len1 # 非重疊窗口
# 設置默認參數
Thres = 3
Expnt = 2.0
beta = 0.002
G = 0.9
# 初始化漢明窗
win = np.hamming(len_)
# normalization gain for overlap+add with 50% overlap
winGain = len2 / sum(win)
# Noise magnitude calculations - assuming that the first 5 frames is noise/silence
nFFT = 2 * 2 ** (nextpow2.nextpow2(len_))
noise_mean = np.zeros(nFFT)
j = 0
for k in range(1, 6):
noise_mean = noise_mean + abs(np.fft.fft(win * x[j:j + len_], nFFT))
j = j + len_
noise_mu = noise_mean / 5
# --- allocate memory and initialize various variables
k = 1
img = 1j
x_old = np.zeros(len1)
Nframes = len(x) // len2 - 1
xfinal = np.zeros(Nframes * len2)
# ========================= Start Processing ===============================
for n in range(0, Nframes):
# Windowing
insign = win * x[k-1:k + len_ - 1]
# compute fourier transform of a frame
spec = np.fft.fft(insign, nFFT)
# compute the magnitude
sig = abs(spec)
# save the noisy phase information
theta = np.angle(spec)
SNRseg = 10 * np.log10(np.linalg.norm(sig, 2) ** 2 / np.linalg.norm(noise_mu, 2) ** 2)
def berouti(SNR):
if -5.0 <= SNR <= 20.0:
a = 4 - SNR * 3 / 20
else:
if SNR < -5.0:
a = 5
if SNR > 20:
a = 1
return a
def berouti1(SNR):
if -5.0 <= SNR <= 20.0:
a = 3 - SNR * 2 / 20
else:
if SNR < -5.0:
a = 4
if SNR > 20:
a = 1
return a
if Expnt == 1.0: # 幅度譜
alpha = berouti1(SNRseg)
else: # 功率譜
alpha = berouti(SNRseg)
#############
sub_speech = sig ** Expnt - alpha * noise_mu ** Expnt;
# 當純凈信號小于噪聲信號的功率時
diffw = sub_speech - beta * noise_mu ** Expnt
# beta negative components
def find_index(x_list):
index_list = []
for i in range(len(x_list)):
if x_list[i] < 0:
index_list.append(i)
return index_list
z = find_index(diffw)
if len(z) > 0:
# 用估計出來的噪聲信號表示下限值
sub_speech[z] = beta * noise_mu[z] ** Expnt
# --- implement a simple VAD detector --------------
if SNRseg < Thres: # Update noise spectrum
noise_temp = G * noise_mu ** Expnt + (1 - G) * sig ** Expnt # 平滑處理噪聲功率譜
noise_mu = noise_temp ** (1 / Expnt) # 新的噪聲幅度譜
# flipud函數實現矩陣的上下翻轉,是以矩陣的“水平中線”為對稱軸
# 交換上下對稱元素
sub_speech[nFFT // 2 + 1:nFFT] = np.flipud(sub_speech[1:nFFT // 2])
x_phase = (sub_speech ** (1 / Expnt)) * (np.array([math.cos(x) for x in theta]) + img * (np.array([math.sin(x) for x in theta])))
# take the IFFT
xi = np.fft.ifft(x_phase).real
# --- Overlap and add ---------------
xfinal[k-1:k + len2 - 1] = x_old + xi[0:len1]
x_old = xi[0 + len1:len_]
k = k + len2
# 保存文件
wf = wave.open('en_outfile.wav', 'wb')
# 設置參數
wf.setparams(params)
# 設置波形文件 .tostring()將array轉換為data
wave_data = (winGain * xfinal).astype(np.short)
wf.writeframes(wave_data.tostring())
wf.close()
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
