-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathnoise_addition_utils.py
141 lines (105 loc) · 3.94 KB
/
noise_addition_utils.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch
import numpy as np
from pathlib import Path
from torch.utils.data import Dataset, DataLoader
from matplotlib import colors, pyplot as plt
from numpy.fft import rfft, irfft
from scipy.io.wavfile import read
import torchaudio
# Set Audio backend as Sounfile for windows and Sox for Linux
torchaudio.set_audio_backend("soundfile")
def ms(x):
return (np.abs(x)**2).mean()
def rms(x):
return np.sqrt(ms(x))
def normalise(y, power):
"""
Normalise power in y to power specified.
Standard signal if power=1
The mean power of a Gaussian with `mu=0` and `sigma=x` is x^2.
"""
return y * np.sqrt(power / ms(y))
def noise(N, color, power):
"""
Noise generator.
N: Amount of samples.
color: Color of noise.
power: power = std_dev^2
https://en.wikipedia.org/wiki/Colors_of_noise
"""
noise_generators = {
'white': white,
'pink': pink,
'blue': blue,
'brown': brown,
'violet': violet
}
return noise_generators[color](N, power)
def white(N, power):
y = np.random.randn(N).astype(np.float32)
return normalise(y, power)
def pink(N, power):
orig_N = N
# Because doing rfft->ifft produces different length outputs depending if its odd or even length inputs
N+=1
x = np.random.randn(N).astype(np.float32)
X = rfft(x) / N
S = np.sqrt(np.arange(X.size)+1.) # +1 to avoid divide by zero
y = irfft(X/S).real[:orig_N]
return normalise(y, power)
def blue(N, power):
orig_N = N
# Because doing rfft->ifft produces different length outputs depending if its odd or even length inputs
N+=1
x = np.random.randn(N).astype(np.float32)
X = rfft(x) / N
S = np.sqrt(np.arange(X.size)) # Filter
y = irfft(X*S).real[:orig_N]
return normalise(y, power)
def brown(N, power):
orig_N = N
# Because doing rfft->ifft produces different length outputs depending if its odd or even length inputs
N+=1
x = np.random.randn(N).astype(np.float32)
X = rfft(x) / N
S = np.arange(X.size)+1 # Filter
y = irfft(X/S).real[:orig_N]
return normalise(y, power)
def violet(N, power):
orig_N = N
# Because doing rfft->ifft produces different length outputs depending if its odd or even length inputs
N+=1
x = np.random.randn(N).astype(np.float32)
X = rfft(x) / N
S = np.arange(X.size) # Filter
y = irfft(X*S).real[0:orig_N]
return normalise(y, power)
def generate_colored_gaussian_noise(file_path='./sample_audio.wav', snr=10, color='white'):
# Load audio data into a 1D numpy array
un_noised_file, _ = torchaudio.load(file_path)
un_noised_file = un_noised_file.numpy()
un_noised_file = np.reshape(un_noised_file, -1)
# Create an audio Power array
un_noised_file_watts = un_noised_file ** 2
# Create an audio Decibal array
un_noised_file_db = 10 * np.log10(un_noised_file_watts)
# Calculate signal power and convert to dB
un_noised_file_avg_watts = np.mean(un_noised_file_watts)
un_noised_file_avg_db = 10 * np.log10(un_noised_file_avg_watts)
# Calculate noise power
added_noise_avg_db = un_noised_file_avg_db - snr
added_noise_avg_watts = 10 ** (added_noise_avg_db / 10)
# Generate a random sample of additive gaussian noise
added_noise = noise(len(un_noised_file), color, added_noise_avg_watts)
# Add Noise to the Un-Noised signal
noised_audio = un_noised_file + added_noise
return noised_audio
def load_audio_file(file_path='./sample_audio.wav'):
waveform, _ = torchaudio.load(file_path)
waveform = waveform.numpy()
waveform = np.reshape(waveform, -1)
return waveform
def save_audio_file(np_array=np.array([0.5]*1000),file_path='./sample_audio.wav', sample_rate=48000, bit_precision=16):
np_array = np.reshape(np_array, (1,-1))
torch_tensor = torch.from_numpy(np_array)
torchaudio.save(file_path, torch_tensor, sample_rate, bits_per_sample=bit_precision)