-
Notifications
You must be signed in to change notification settings - Fork 419
/
noise_reduction_wiener_filtering.py
126 lines (108 loc) · 3.04 KB
/
noise_reduction_wiener_filtering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
from scipy.io import wavfile
import os
import pyroomacoustics as pra
import matplotlib.pyplot as plt
import time
from pyroomacoustics.denoise import IterativeWiener
"""
Test and algorithm parameters
"""
snr = 5 # SNR of input signal
# the number of LPC coefficients to consider
lpc_order = 15
# the number of iterations to update wiener filter
iterations = 2
# FFT length
frame_len = 512
# parameter update of the sigma in sigma tracking
alpha = 0.1 # smaller value allows noise floor to change faster
threshold = 0.003
plot_spec = True
"""
Prepare input file
"""
signal_fp = os.path.join(
os.path.dirname(__file__), "input_samples", "cmu_arctic_us_aew_a0001.wav"
)
noise_fp = os.path.join(
os.path.dirname(__file__), "input_samples", "doing_the_dishes.wav"
)
noisy_signal, signal, noise, fs = pra.create_noisy_signal(
signal_fp, snr=snr, noise_fp=noise_fp
)
wavfile.write(
os.path.join(
os.path.dirname(__file__), "output_samples", "denoise_input_IterativeWiener.wav"
),
fs,
noisy_signal.astype(np.float32),
)
"""
Apply approach
"""
scnr = IterativeWiener(frame_len, lpc_order, iterations, alpha, threshold)
# derived parameters
hop = frame_len // 2
window_a = pra.hann(frame_len)
window_s = pra.transform.stft.compute_synthesis_window(window_a, hop)
stft = pra.transform.STFT(
frame_len,
hop=hop,
analysis_window=window_a,
synthesis_window=window_s,
streaming=True,
)
speech_psd = np.ones(hop + 1) # initialize PSD
noise_psd = 0
start_time = time.time()
processed_audio = np.zeros(noisy_signal.shape)
n = 0
while noisy_signal.shape[0] - n >= hop:
# to frequency domain, 50% overlap
stft.analysis(
noisy_signal[n : (n + hop)]
)
# compute Wiener output
X = scnr.compute_filtered_output(current_frame=stft.fft_in_buffer, frame_dft=stft.X)
# back to time domain
processed_audio[n : n + hop] = stft.synthesis(X)
# update step
n += hop
proc_time = time.time() - start_time
print("Processing time: {} minutes".format(proc_time / 60))
"""
Save and plot spectrogram
"""
wavfile.write(
os.path.join(
os.path.dirname(__file__),
"output_samples",
"denoise_output_IterativeWiener.wav",
),
fs,
pra.normalize(processed_audio).astype(np.float32),
)
print(
"Noisy and denoised file written to: '%s'"
% os.path.join(os.path.dirname(__file__), "output_samples")
)
signal_norm = signal / np.abs(signal).max()
processed_audio_norm = processed_audio / np.abs(processed_audio).max()
if plot_spec:
min_val = -80
max_val = -40
plt.figure()
plt.subplot(3, 1, 1)
plt.specgram(noisy_signal[: n - hop], NFFT=256, Fs=fs, vmin=min_val, vmax=max_val)
plt.title("Noisy Signal")
plt.subplot(3, 1, 2)
plt.specgram(
processed_audio_norm[hop:n], NFFT=256, Fs=fs, vmin=min_val, vmax=max_val
)
plt.title("Denoised Signal")
plt.subplot(3, 1, 3)
plt.specgram(signal_norm[: n - hop], NFFT=256, Fs=fs, vmin=min_val, vmax=max_val)
plt.title("Original Signal")
plt.tight_layout(pad=0.5)
plt.show()