-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_noise.py
44 lines (39 loc) · 1.23 KB
/
add_noise.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
import math
import librosa
import matplotlib.pyplot as plt
import numpy as np
from scipy.io.wavfile import write
def get_white_noise(signal,SNR) :
#RMS value of signal
RMS_s=math.sqrt(np.mean(signal**2))
#RMS values of noise
RMS_n=math.sqrt(RMS_s**2/(pow(10,SNR/20)))
#Additive white gausian noise. Thereore mean=0
#Because sample length is large (typically > 40000)
#we can use the population formula for standard daviation.
#because mean=0 STD=RMS
STD_n=RMS_n
noise=np.random.normal(0, STD_n, signal.shape[0])
return noise
#***convert complex np array to polar arrays (2 apprays; abs and angle)
def to_polar(complex_ar):
return np.abs(complex_ar),np.angle(complex_ar)
signal_file = 'sound/input.wav'
signal, sr = librosa.load(signal_file)
signal=np.interp(signal, (signal.min(), signal.max()), (-1, 1))
noise=get_white_noise(signal,SNR=10)
plt.plot(signal)
plt.show()
#analyze the frequency components in the signal
X=np.fft.rfft(noise)
radius,angle=to_polar(X)
plt.plot(radius)
plt.xlabel("FFT coefficient")
plt.ylabel("Magnitude")
plt.show()
signal_noise=signal+noise
plt.plot(signal_noise)
plt.xlabel("Sample number")
plt.ylabel("Amplitude")
plt.show()
write("sound/input_with_noise.wav",sr,signal_noise)