import librosa
import numpy as np
from config import DefaultConfig
class Wiener:
def __init__(self):
self.config = DefaultConfig.weiner_config
self.file_config = DefaultConfig.file_config
self.filter = None
def genFilter(self, noise, data):
"""
:param noise: a series of noise data or a single noise data (Must have the same length as data)
:param data: a series of clean data or a single clean data (Must have the same length as noise)
:return:
"""
if isinstance(data, list) and isinstance(noise, list):
Px, Pn = [], []
for clean_data, noise_data in zip(data, noise):
S_clean = librosa.stft(clean_data, n_fft=self.config.fft_length,
hop_length=self.config.frame_shift_length, win_length=self.config.window_length)
S_noise = librosa.stft(noise_data, n_fft=self.config.fft_length,
hop_length=self.config.frame_shift_length, win_length=self.config.window_length)
Pxx = np.mean((np.abs(S_clean)) ** 2, axis=1, keepdims=True) # Dx1
Pnn = np.mean((np.abs(S_noise)) ** 2, axis=1, keepdims=True)
Px.append(Pxx)
Pn.append(Pnn)
train_Pxx = np.mean(np.concatenate(Px, axis=1), axis=1, keepdims=True)
train_Pnn = np.mean(np.concatenate(Pn, axis=1), axis=1, keepdims=True)
self.filter = (train_Pxx / (train_Pxx + self.config.alpha * train_Pnn)) ** self.config.beta
elif isinstance(data, np.ndarray) and isinstance(noise, np.ndarray):
S_clean = librosa.stft(data, n_fft=self.config.fft_length,
hop_length=self.config.frame_shift_length, win_length=self.config.window_length)
S_noise = librosa.stft(noise, n_fft=self.config.fft_length,
hop_length=self.config.frame_shift_length, win_length=self.config.window_length)
Pxx = np.mean((np.abs(S_clean)) ** 2, axis=1, keepdims=True)
Pnn = np.mean((np.abs(S_noise)) ** 2, axis=1, keepdims=True)
self.filter = (Pxx / (Pxx + self.config.alpha * Pnn)) ** self.config.beta
def waveFiltering(self, data, file_name="filtered.wav"):
"""
:param file_name: file name for saved filtered data
:param data: data with noise
:return: data filtered by wiener filter
"""
file_name = self.file_config.file_output_path + file_name
S_data = librosa.stft(data, n_fft=self.config.fft_length,
hop_length=self.config.frame_shift_length, win_length=self.config.window_length)
S_filtered = S_data * self.filter
filtered_data = librosa.istft(S_filtered, hop_length=self.config.frame_shift_length,
win_length=self.config.window_length)
return filtered_data
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
数字语音处理课设 基于python两种去噪算法传统的维纳滤波和改进的谱减法+维纳滤波源码.zip (30个子文件)
项目说明.7z 6KB
AudioUtil.py 2KB
resources
sound_original.wav 1.26MB
noisy_sound.wav 1.26MB
demo_sound_original.wav 688KB
noise.wav 1.26MB
WienerUtil.py 3KB
main.py 2KB
configs
__init__.py 23B
configs.py 1KB
NoiseUtil.py 1KB
output
demo_noisy_data.wav 474KB
wiener_subproc_filtered_data.wav 890KB
demo_wiener_subproc_filtered_data.wav 474KB
wiener_filtered_data.wav 890KB
demo_wiener_filtered_data.wav 474KB
noisy_data.wav 474KB
demo.py 2KB
imgs
pic4.png 256KB
pic2.png 289KB
pic10.png 77KB
pic3.png 76KB
pic5.png 65KB
pic7.png 184KB
pic8.png 68KB
pic1.png 10KB
pic9.png 72KB
pic6.png 106KB
SubFilterUtil.py 3KB
config.py 2KB
共 30 条
- 1
资源评论
- wcs199809112024-11-11资源内容总结的很到位,内容详实,很受用,学到了~
Scikit-learn
- 粉丝: 4260
- 资源: 1867
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功