Skip to content

Instantly share code, notes, and snippets.

Last active January 28, 2022 06:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tam17aki/a0153f8d9c7f3d662f37bd6c0de04847 to your computer and use it in GitHub Desktop.
Save tam17aki/a0153f8d9c7f3d662f37bd6c0de04847 to your computer and use it in GitHub Desktop.
import numpy as np
import os
from 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 =
fs_n, noise =
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