Tuesday, October 18, 2011

Generating White Noise Sound on Octave / Matlab


The following white noise sound was generated with GNU Octave's random number generator rand(), which generates uniformly distributed random values in the interval [0,1). The wavwrite() function expects values in [-1.0, 1.0), so we multiply by 2 and shift down by 1. So to generate 10 seconds of noise sampled at 48 kHz:
white=rand(48000*10,1)*2-1;
I'm not sure about the inner workings of /dev/urandom, but I used the "reseed" command to feed it data from random.org, so this should be pretty random. To export this to a 16-bit, 48 kHz .wav file in our home directory, the command is:
wavwrite(white,48000,16,'white noise uniform.wav');
Then I converted the .wav file to a FLAC-encoded Ogg (.oga) file:
flac --ogg "white noise uniform.wav"
(The "compressed" FLAC file is actually larger than the .wav file.)  :) Then I generated a lossily-compressed .ogg Vorbis file:
oggenc "white noise uniform.wav"
RMS value derived from:
sox "white noise uniform.wav" -e stat
and then in dB is 20 \log_{10}(\mathrm{RMS\ amplitude})
Histogram derived from
hist(white,100,1)

Source:wikipedia.org