国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

譜減法語音降噪的Python實現

2019-11-06 08:09:45
字體:
來源:轉載
供稿:網友

譜減法語音降噪的Python實現

本文是參考 [投稿]譜減法語音降噪原理 Matlab版本的代碼,采用Python實現的。具體的降噪原理請參閱上文。

下面是譜減法語音降噪的Python實現

文件speech_enhanced.py

#!/usr/bin/env pythonimport numpy as npimport waveimport nextpow2import math# 打開WAV文檔f = wave.open("filename.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 // 1000PERC = 50len1 = len_ * PERC // 100len2 = len_ - len1# 設置默認參數Thres = 3Expnt = 2.0beta = 0.002G = 0.9# 初始化漢明窗win = np.hamming(len_)# normalization gain for overlap+add with 50% overlapwinGain = len2 / sum(win)# Noise magnitude calculations - assuming that the first 5 frames is noise/silencenFFT = 2 * 2 ** (nextpow2.nextpow2(len_))noise_mean = np.zeros(nFFT)j = 0for 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 variablesk = 1img = 1jx_old = np.zeros(len1)Nframes = len(x) // len2 - 1xfinal = 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('outfile.wav', 'wb')# 設置參數wf.setparams(params)# 設置波形文件 .tostring()將array轉換為datawave_data = (winGain * xfinal).astype(np.short)wf.writeframes(wave_data.tostring())wf.close()

以上代碼中用到了nextpow2,其中n = nextpow2(x) 表示最接近x的2的n次冪,這里就不在貼出,有需要的朋友可以聯系(QQ:437482772)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临高县| 乌兰浩特市| 新化县| 金寨县| 孝感市| 奇台县| 甘谷县| 荔波县| 永年县| 大田县| 潮安县| 旬邑县| 库车县| 报价| 库车县| 垫江县| 丹江口市| 得荣县| 潍坊市| 庄河市| 广河县| 天门市| 朝阳县| 高碑店市| 贡觉县| 江都市| 民乐县| 嘉义市| 航空| 长顺县| 观塘区| 北海市| 高陵县| 贵港市| 唐河县| 靖江市| 溆浦县| 江孜县| 张家口市| 重庆市| 盐城市|