Python环境下基于自适应滤波器的音频信号(wav格式)降噪方法

Python的集成环境我一般使用的是WinpythonWinpytho脱胎于pythonxy,面向科学计算兼顾数据分析与挖掘;Anaconda主要面向数据分析与挖掘方面,在大数据处理方面有自己特色的一些包;Winpytho强调便携性,被做成绿色软件,不写入注册表,安装其实就是解压到某个文件夹,移动文件夹甚至放到U盘里在其他电脑上也能用。抛开软件包的差异,我个人也推荐初学者用winpython,正因为其简单,问题也少点,由于便携性的特点系统坏了,重装后也能直接用。

请直接安装、使用winPython:
https://sourceforge.net/projects/winpython/因为很多模块以及集成的模块

IDE我用的spyder(类MATLAB界面)

Python环境下基于自适应滤波器的音频信号(wav格式)降噪方法_第1张图片

所用模块如下:

import numpy as np
import librosa
import soundfile as sf
import pyroomacoustics as pra

需要按照librosa和pyroomacoustics模块

pip install librosa
pip install pyroomacoustics

主要方法如下:

时域自适应滤波器Time Domain Adaptive Filters

- Least Mean Squares Filter (LMS)

- Block Least Mean Squares Filter (BLMS)

- Normalized Least Mean Squares Filter (NLMS)

- Block Normalized Least Mean Squares Filter (BNLMS)

- Recursive Least Squares Filter (RLS)

- Affine Projection Algorithm (APA)

- Kalman Filter (KALMAN)

非线性自适应滤波器Nonlinear Adaptive Filters

- Second Order Volterra Filter (SVF)

- Trigonometric Functional Link Adaptive Filter (FLAF)

- Adaptive Exponential Functional Link Adaptive Filter (AEFLAF)

- Split Funcional Link Adaptive Filter (SFLAF)

- Collaborative Functional Link Adaptive Filter (CFLAF)

频域自适应滤波器Frequency Domain Adaptive Filters

- Frequency Domain Adaptive Filter (FDAF)

- Partitioned-Block-Based Frequency Domain Adaptive Filter (PFDAF)

- Frequency Domain Kalman Filter (FDKF)

- Partitioned-Block-Based Frequency Domain Kalman Filter (PFDKF)

工学博士,担任《Mechanical System and Signal Processing》审稿专家,担任
《《控制与决策》,《系统工程与电子技术》,《电力系统保护与控制》等EI期刊审稿专家。
擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

程序部分代码如下:

import numpy as np
import librosa
#pip install librosa
#pip install pyroomacoustics
import soundfile as sf
import pyroomacoustics as pra

from time_domain_adaptive_filters.lms import lms
from time_domain_adaptive_filters.nlms import nlms
from time_domain_adaptive_filters.blms import blms
from time_domain_adaptive_filters.bnlms import bnlms
from time_domain_adaptive_filters.rls import rls
from time_domain_adaptive_filters.apa import apa
from time_domain_adaptive_filters.kalman import kalman
from frequency_domain_adaptive_filters.pfdaf import pfdaf
from frequency_domain_adaptive_filters.fdaf import fdaf
from frequency_domain_adaptive_filters.fdkf import fdkf
from frequency_domain_adaptive_filters.pfdkf import pfdkf
from nonlinear_adaptive_filters.volterra import svf
from nonlinear_adaptive_filters.flaf import flaf
from nonlinear_adaptive_filters.aeflaf import aeflaf
from nonlinear_adaptive_filters.sflaf import sflaf
from nonlinear_adaptive_filters.cflaf import cflaf


def main():
  x, sr  = librosa.load('samples/female.wav',sr=8000)
  d, sr  = librosa.load('samples/male.wav',sr=8000)

  rt60_tgt = 0.08
  room_dim = [2, 2, 2]

  e_absorption, max_order = pra.inverse_sabine(rt60_tgt, room_dim)
  room = pra.ShoeBox(room_dim, fs=sr, materials=pra.Material(e_absorption), max_order=max_order)
  room.add_source([1.5, 1.5, 1.5])
  room.add_microphone([0.1, 0.5, 0.1])
  room.compute_rir()
  rir = room.rir[0][0]
  rir = rir[np.argmax(rir):]

  y = np.convolve(x,rir)
  scale = np.sqrt(np.mean(x**2)) /  np.sqrt(np.mean(y**2))
  y = y*scale

  L = max(len(y),len(d))
  y = np.pad(y,[0,L-len(y)])
  d = np.pad(d,[L-len(d),0])
  x = np.pad(x,[0,L-len(x)])
  d = d + y

  sf.write('samples/x.wav', x, sr, subtype='PCM_16')
  sf.write('samples/d.wav', d, sr, subtype='PCM_16')

  print("processing time domain adaptive filters.")

  e = lms(x, d, N=256, mu=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/lms.wav', e, sr, subtype='PCM_16')

  e = blms(x, d, N=256, L=4, mu=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/blms.wav', e, sr, subtype='PCM_16')
  
  e = nlms(x, d, N=256, mu=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/nlms.wav', e, sr, subtype='PCM_16')

  e = bnlms(x, d, N=256, L=4, mu=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/bnlms.wav', e, sr, subtype='PCM_16')

  e = rls(x, d, N=256)
  e = np.clip(e,-1,1)
  sf.write('samples/rls.wav', e, sr, subtype='PCM_16')

  e = apa(x, d, N=256, P=5, mu=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/apa.wav', e, sr, subtype='PCM_16')

  e = kalman(x, d, N=256)
  e = np.clip(e,-1,1)
  sf.write('samples/kalman.wav', e, sr, subtype='PCM_16')

  print("processing nonlinear adaptive filters.")

  e = svf(x, d, M=256, mu1=0.1, mu2=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/volterra.wav', e, sr, subtype='PCM_16')
  
  e = flaf(x, d, M=256, P=5, mu=0.2)
  e = np.clip(e,-1,1)
  sf.write('samples/flaf.wav', e, sr, subtype='PCM_16')

  e = aeflaf(x, d, M=256, P=5, mu=0.05, mu_a=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/aeflaf.wav', e, sr, subtype='PCM_16')
  
  e = sflaf(x, d, M=256, P=5, mu_L=0.2, mu_FL=0.5)
  e = np.clip(e,-1,1)
  sf.write('samples/sflaf.wav', e, sr, subtype='PCM_16')

  e = cflaf(x, d, M=256, P=5, mu_L=0.2, mu_FL=0.5, mu_a=0.5)
  e = np.clip(e,-1,1)
  sf.write('samples/cflaf.wav', e, sr, subtype='PCM_16')

  print("processing frequency domain adaptive filters.")

  e = fdaf(x, d, M=256, mu=0.1)
  e = np.clip(e,-1,1)
  sf.write('samples/fdaf.wav', e, sr, subtype='PCM_16')

  e = fdkf(x, d, M=256)
  e = np.clip(e,-1,1)
  sf.write('samples/fdkf.wav', e, sr, subtype='PCM_16')

  e = pfdaf(x, d, N=8, M=64, mu=0.1, partial_constrain=True)
  e = np.clip(e,-1,1)
  sf.write('samples/pfdaf.wav', e, sr, subtype='PCM_16')

  e = pfdkf(x, d, N=8, M=64, partial_constrain=True)
  e = np.clip(e,-1,1)
  sf.write('samples/pfdkf.wav', e, sr, subtype='PCM_16')


if __name__ == '__main__':
  main()
  

你可能感兴趣的:(python,开发语言)