Python Mix an Audio with a Noise at Specific Signal-to-Noise Ratio (SNR) – Python Tutorial

By | April 12, 2023

In audio data augmentation, we often add a noise to a clean audio. In this tutorial, we will introduce you how to do using python.

For example, we may see:

In each augmentation, one noise file is randomly selected in the MUSAN database and added to the recording with [0, 5, 10, 15]
dB SNR.

In order to understand what is SNR, you can read:

Compute Audio SNR (Signal-to-Noise Ratio) in Python – Python Tutorial

How to mix a clean audio with a noise file in python?

Here we will create a python function to implement it.

For example:

import numpy
import librosa
import soundfile

def add_noise_snr(audio, noise, snr):
    clean_db = 10 * numpy.log10(numpy.mean(audio ** 2) + 1e-4)
    audio_length = audio.shape[0]
    if noise.shape[0] <= audio_length:
        shortage = audio_length - noise.shape[0]
        noise = numpy.pad(noise, (0, shortage), 'wrap')
    noise_db = 10 * numpy.log10(numpy.mean(noise ** 2) + 1e-4)

    add_noise = numpy.sqrt(10 ** ((clean_db - noise_db - snr) / 10)) * noise
    mix_audio = audio + add_noise
    return mix_audio

def read_audio(file_audio):
    audio, sr = librosa.load(file_audio, sr=8000, mono=True)
    return audio
def save_wav(audio, fx, sr = 8000):
    soundfile.write(fx, audio, sr, "PCM_16")

In this code, add_noise_snr(audio, noise, snr) function will add noise data to a clean audio data base on a snr value.

We can use this function as follows:

wav_file = r"speech-us-gov-0010.wav"
audio = read_audio(wav_file)
noise_file = "music-jamendo-0039.wav"
noise = read_audio(noise_file)

audio_noise = add_noise_snr(audio, noise, snr = 0)
save_wav(audio_noise, "audio_noise_0.wav")

audio_noise = add_noise_snr(audio, noise, snr = 5)
save_wav(audio_noise, "audio_noise_5.wav")

audio_noise = add_noise_snr(audio, noise, snr = 10)
save_wav(audio_noise, "audio_noise_10.wav")

Then, we will create 3 mixed audio files.

You should notice: the larger of the value of SNR, the clearer of the clean audio in mixed audio file.