Last active
January 28, 2022 06:35
-
-
Save tam17aki/a0153f8d9c7f3d662f37bd6c0de04847 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import os | |
from scipy.io import wavfile | |
import pyroomacoustics as pra | |
frame_len = 80 | |
mu = 30 # higher value can give more suppression but more distortion | |
thres = 0.01 # threshold between (signal+noise) and noise | |
# How many frames to look back for the noise floor estimate. | |
lookback = 20 | |
# How many samples to skip when estimating the covariance matrices | |
# with past samples. | |
skip = 1 | |
""" | |
preparation of noisy input signal | |
""" | |
snr = 5 # SNR of input signal | |
speech_file = "./input/arctic_a0010.wav" | |
noise_file = "./input/exercise_bike.wav" | |
# load WAV files, should have same sampling rates! | |
fs_s, audio = wavfile.read(speech_file) | |
fs_n, noise = wavfile.read(noise_file) | |
assert fs_s == fs_n, "audio and noise wav files should have same sampling rate!" | |
# truncate to same length | |
noise = noise[:len(audio)] | |
# weight noise according to desired SNR | |
signal_level = np.linalg.norm(audio) | |
noise_level = np.linalg.norm(noise) | |
noise_fact = signal_level / 10**(snr / 20) | |
noise_weighted = noise * noise_fact / noise_level | |
# add signal and noise | |
noisy_signal = audio + noise_weighted | |
noisy_signal /= np.abs(noisy_signal).max() | |
noisy_signal -= noisy_signal.mean() | |
""" | |
noise reduction via subspace method | |
""" | |
# import or create `noisy_signal` | |
denoised_signal = pra.denoise.subspace.apply_subspace( | |
noisy_signal, frame_len=frame_len, mu=mu, | |
lookback=lookback, skip=skip, thresh=thres) | |
# output results | |
outputDir = './output' | |
os.makedirs(outputDir, exist_ok=True) | |
print("Output noisy speech.") | |
wavfile.write("{}/noisy_speech.wav".format(outputDir), fs_s, noisy_signal) | |
print("Output denoised speech.") | |
wavfile.write("{}/denoised_speech.wav".format(outputDir), fs_s, denoised_signal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment