Python谱减法语音降噪实例
作者:zipaiyou 发布时间:2023-07-26 05:19:55
标签:Python,谱减法,语音降噪
代码中用到了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()
来源:https://blog.csdn.net/weixin_41934101/article/details/81152172


猜你喜欢
- 前言mysql 相信大部分人都用过,索引肯定也是用过的,但是你知道如何创建恰当的索引吗?在数据量小的时候,不合适的索引对性能并不会有太大的影
- 本文为大家分享了vue $emit 和 $on 组件通信,供大家参考,具体内容如下<!DOCTYPE html> <htm
- 先来看个用Python实现的二分查找算法实例import sys def search2(a,m): low = 0 high = le
- Django1.11配合uni-app发起微信支付!经过三天的断断续续的奋战,我终于是干动了微信支付。为了以后不忘记,现在来一篇教程,来来来
- 前言朋友提问:创建Word文档并插入市面上有很多图表绘制库,例如echarts和highcharts等等。对于这种由js动态绘制的图表,我们
- 测试环境win10python 3.5yield功能简介简单来说,yield 的作用就是把一个函数变成一个 generator,带有 yie
- '去掉字符串头尾的连续的回车和空格 function trimVBcrlf(str) tr
- 什么是错误页面?是指链接指向的网页现在失效了,原因可能是用户输错了地址,也可能是网站结构调整,内容删除,或者地址变更都有可能出现这种情况。那
- 概述从今天开始, 小白我将带领大家一起来补充一下 数据库的知识.MySQL 安装下载地址:https://dev.mysql.com/dow
- pycharm每次新建项目都需要重新安装库,解决方法如下:新建项目时自定义选择库(自己安装python位置),不要创建新的(如下图)第一完成
- 更换国内源vi /etc/apt/sources.listdeb https://mirrors.aliyun.com/kali kali-
- startswith()方法Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返
- function ReportFileStatus(filespec) { var fso, s = filespec; fso = new
- 配置了好多天都报错…我真的含泪写下这篇文章1. 首先我们打开sql sever2. 打开sql配置管理器将IP1和IP
- subplot函数介绍matplotlib下, 一个 Figure 对象可以包含多个子图(Axes), 可以使用 subplot() 快速绘
- atom(一款开源的代码编辑器)是github专门为程序员推出的一个跨平台文本编辑器。具有简洁和直观的图形用户界面,并有很多有趣的特点:支持
- 这是我上一篇关于安全的文章的其中一节。这是一个众所周知的事实,对你运行中的网站的MySQL数据库备份是极为重要的只需按照下面3步做,一切都在
- MySQL自定义序列数实现往往有很多情况下,我们需要使用自己生成的唯一Id或保证不重复的序列号,特别是在高并发的场景下。为此,很容易想到的实
- 本文实例讲述了Python操作json的方法。分享给大家供大家参考,具体如下:python中对json操作方法有两种,解码loads()和编
- 一、Python下载安装1、Python官方下载地址:https://www.python.org/downloads官方下载速度太慢,你可