Python谱减法语音降噪实例

所属分类: 脚本专栏 / python 阅读数: 1000
收藏 0 赞 0 分享

代码中用到了nextpow2,其中n = nextpow2(x) 表示最接近x的2的n次幂。

#!/usr/bin/env python
import numpy as np
import wave
import nextpow2
import 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 // 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('outfile.wav', 'wb')
# 设置参数
wf.setparams(params)
# 设置波形文件 .tostring()将array转换为data
wave_data = (winGain * xfinal).astype(np.short)
wf.writeframes(wave_data.tostring())
wf.close()

以上这篇Python谱减法语音降噪实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Python实现图像几何变换

这篇文章主要介绍了Python实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多