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?
1 Answer
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 = <x^2[n]>, P_y = <y^2[n]> \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.
-
$\begingroup$ 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 $\endgroup$– Son GokuCommented Aug 6 at 8:21
-
$\begingroup$ I think that I found a library in Python that solves my problem: scaper.readthedocs.io/en/latest/tutorial.html $\endgroup$– Son GokuCommented Aug 6 at 12:52