# How can I add Audiofiles to an large Audiofile with specific SNR with no clipping

I have an audio file that I want to use as background noise. I also have other audio files that are to serve as foreground sounds and are to be placed at certain times. How can I ensure that there is no clipping when I overlay the audio files and how can I define an SNR between the foreground noise and the background noise?

• The background should not be changed segmentwise. It should stay constant. Commented Aug 5 at 12:15

Let's say your foreground sound is $$x[n]$$ and the noise signal is $$y[n]$$. First you need to determine of each signal.

$$P_x = , P_y = \tag{1}$$

Depending on your exact definition of SNR or the specific requirements of your application you may have to apply a frequency weighting first.

You add the signals with a specific SNR by creating a weighted sum

$$z[n] = x[n] + g\cdot y[n] \tag{2}$$

The SNR is given by the ratio of the two powers, i.e.

$$SNR = 10 \log_{10}(\frac{P_x}{g^2Py}) \tag{3}$$

Given a desired SNR you can determine g as

$$g = \sqrt{\frac{P_x}{P_y}10^{-SNR/10}} \tag{4}$$

The easiest way to avoid clipping is to scale the entire expression so that the worst case sum can't clip and so we get the final answer

$$z_{noclip}[n] = \frac{1}{1+g}x[n] + \frac{g}{1+g}y[n] \tag{5}$$

There are smarter ways of managing clipping but this is simple and safe.

• Hallo Hilmar, thank you for your answer. I would like to a have a constant P_x, because i don´t want to change my original noise-sound and i would like to have several SNRs. I am not sure if the equation you gave me is able to handle this. so x should be constant. y[n] can be scaled Commented Aug 6 at 8:21
• I think that I found a library in Python that solves my problem: scaper.readthedocs.io/en/latest/tutorial.html Commented Aug 6 at 12:52