I am trying to add AWGN to my audio file. I convert my wav file to byte array. I am trying to add 10dB AWGN to this array. In matlab there is imnoise which adds AWGN to image. In java is there any library? Thanks in advance.
2 Answers
If you actually need the additive Gaussian white noise output similar to that of Matlab's imnoise
function, this is the extent of the code that you need to implement in Java:
B = A+MU+STD*randn(size(A))
where A
is your input data, B
is your output of the same size, MU
is the mean of the noise, and STD
is the standard deviation. Independent and identically distributed (IID) Gaussian white noise is added to each component of A
. This calculation needs to be done in floating point (as many of Matlab's image processing routines are).
The randn
function produces normal random variates. You can use java.util.Random.nextGaussian()
to produce these. If you need some extra speed, try a Java implementation of the Mersenne Twister algorithm.

- 18,384
- 4
- 37
- 73
Not sure if i got the question correctly. Ist that the thing your are looking for and then just regulate the db over whatever player you are using?

- 237
- 1
- 2
- 10
-
This generates noise at certain amplitude. I have a byte array. How can I adjust the dB? – Ferdi Fogs Jun 17 '13 at 21:13
-
If [this is the code] for the linked class, then I don't think that it actually implements Gaussian white noise. It implements some form of uniform noise. I don't think that it's even really white, but the output could be used to produce it (it's barely more than a call to `Math.random()`). The terms Gaussian noise and white noise are often misused so I'm not sure what the asker requires. – horchler Jun 17 '13 at 21:26
-
As I said before i'm not that sure either where the question ist going but if there is a byte array you can pack that into AudioData ect. making an AudioInputstream out of it. Careful you might hafe to write a wav header so the inputstream understands what it gets. And then have the java sound api to control volume over FloatControl. – Akunosh Jun 17 '13 at 21:32
-
`Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(//some dB value to increase or decrease); clip.start();` – Akunosh Jun 17 '13 at 21:38