-
Notifications
You must be signed in to change notification settings - Fork 42
Data Augmentation
John Martinsson edited this page Dec 14, 2016
·
14 revisions
The same class augmentation simply takes two signal segments s1, and s2, and additively combines them according to:
s_aug = alpha * s1 + (1-alpha) * s2
where alpha is drawn at random from the uniform distribution [0, 1).
Same Class Augmentation |
---|
def same_class_augmentation(wave, class_dir):
""" Perform same class augmentation of the wave by loading a random segment
from the class_dir and additively combine the wave with that segment.
"""
sig_paths = glob.glob(os.path.join(class_dir, "*.wav"))
aug_sig_path = np.random.choice(sig_paths, 1, replace=False)[0]
(fs, aug_sig) = utils.read_wave_file(aug_sig_path)
alpha = np.random.rand()
wave = (1.0-alpha)*wave + alpha*aug_sig
return wave
Noise augmentation is simply an addition of a random noise segment on top of the augmented signal with a dampening factor of 0.4.
s_aug += (noise_seg * 0.4) for noise_seg in noise_segs
where noise_segs are three randomly chosen noise segments from the directory of noise segments.
Noise Augmentation |
---|
def noise_augmentation(wave, noise_dir):
""" Perform noise augmentation of the wave by loading three noise segments
from the noise_dir and add these on top of the wave with a dampening factor
of 0.4
"""
noise_paths = glob.glob(os.path.join(noise_dir, "*.wav"))
aug_noise_paths = np.random.choice(noise_paths, 3, replace=False)
dampening_factor = 0.4
for aug_noise_path in aug_noise_paths:
(fs, aug_noise) = utils.read_wave_file(aug_noise_path)
wave = wave + aug_noise*dampening_factor
return wave
The time shift augmentation is a random shift of the signal by rolling the signal along the time axis. A wrap around shift.
def time_shift_spectrogram(spectrogram):
""" Shift a spectrogram along the time axis in the spectral-domain at random
"""
nb_cols = spectrogram.shape[1]
nb_shifts = np.random.randint(0, nb_cols)
return np.roll(spectrogram, nb_shifts, axis=1)
The pitch shift augmentation is a random roll in the range +-5% around the frequency axis. A wrap-around shift to preserve all information.
def pitch_shift_spectrogram(spectrogram):
""" Shift a spectrogram along the frequency axis in the spectral-domain at
random
"""
nb_cols = spectrogram.shape[0]
max_shifts = nb_cols//20 # around 5% shift
nb_shifts = np.random.randint(-max_shifts, max_shifts)
return np.roll(spectrogram, nb_shifts, axis=0)