DSPRelated.com
Forums

generate white noise in assembly

Started by hbarcellos October 9, 2008
Hi all,

I'm working whit ADSP 21161 analog devices, I'm looking for a way to
generate a white noise in assembly, something like a random function in C.

kind regard's

Henrique
Hi Henrique,

I think you should use 1 or 2 timers.
You make them run much faster than your sample rate.
For every samples you want to get, you take the timers value (there are
some kind of random values) and you do a little operation to get a number
between -1 and 1.
Otherwise, I'm sure you can find the approprieted C function on the web.

good luck,

Mike



hbarcellos wrote:
> Hi all, > > I'm working whit ADSP 21161 analog devices, I'm looking for a way to > generate a white noise in assembly, something like a random function in C. > > kind regard's
A linear congruential generator is probably good enough. Thay are easy to code. If you don't need all the bits, discard the lower-order ones. http://en.wikipedia.org/wiki/Linear_congruential_generator Jerry -- Engineering is the art of making what you want from things you can get. ����������������������������������������������������������������������� ** Posted from http://www.teranews.com **
Jerry Avins wrote:
> hbarcellos wrote: > > Hi all, > > > I'm working whit ADSP 21161 analog devices, I'm looking for a way to > > generate a white noise in assembly, something like a random function in C. > > > kind regard's > > A linear congruential generator is probably good enough. Thay are easy > to code. If you don't need all the bits, discard the lower-order ones.http://en.wikipedia.org/wiki/Linear_congruential_generator
For DSP, the linear congruential generators might be good enough. There is a particular pair of numbers "a" and "b", such that x(n) = (a x(n-1) + b ) % 2^32 produced what looks and sounds like uniform random integers and has a period of 2^32 - 1. For processors that support 32bit fixed-point arithmetic (like the SHARC), this is extremely fast (the % is for free). The numbers "a" and "b" are in one of Knuth's volumes and the old Numerical Recipes in C (in the new version, they completely discourage the use of linear congurential generators - a pity, because for dithering and simple randomization, the above generator works just fine). Hey wait, the old version of NR is available online here: http://www.nrbook.com/a/bookcpdf.php Chapter 7.1 (Uniform Deviates), section Quick and Dirty Generators. So "a" is 1664525 and "b" is 1013904223. Regards, Andor
Andor wrote:
> Jerry Avins wrote: >> hbarcellos wrote: >>> Hi all, >>> I'm working whit ADSP 21161 analog devices, I'm looking for a way to >>> generate a white noise in assembly, something like a random function in C. >>> kind regard's >> A linear congruential generator is probably good enough. Thay are easy >> to code. If you don't need all the bits, discard the lower-order ones.http://en.wikipedia.org/wiki/Linear_congruential_generator > > For DSP, the linear congruential generators might be good enough. > There is a particular pair of numbers "a" and "b", such that > > x(n) = (a x(n-1) + b ) % 2^32 > > produced what looks and sounds like uniform random integers and has a > period of 2^32 - 1. For processors that support 32bit fixed-point > arithmetic (like the SHARC), this is extremely fast (the % is for > free). The numbers "a" and "b" are in one of Knuth's volumes and the > old Numerical Recipes in C (in the new version, they completely > discourage the use of linear congurential generators - a pity, because > for dithering and simple randomization, the above generator works just > fine). > > Hey wait, the old version of NR is available online here: > > http://www.nrbook.com/a/bookcpdf.php > > Chapter 7.1 (Uniform Deviates), section Quick and Dirty Generators. So > "a" is 1664525 and "b" is 1013904223.
The link I cited gives several pairs. Knuth (TACP, chap 3) has a lot of good discussion, but makes no specific recommendations. A general Linear congruential RNG has the form X_{n+1} = (a*X_n + c)mod m. For the ANSI C RNG, m=2^32, a=1103515245, c=12345, and bits 30..16 are returned. Jerry -- Engineering is the art of making what you want from things you can get. ����������������������������������������������������������������������� ** Posted from http://www.teranews.com **
Jerry Avins wrote:
> Andor wrote: > > Jerry Avins wrote: > >> hbarcellos wrote: > >>> Hi all, > >>> I'm working whit ADSP 21161 analog devices, I'm looking for a way to > >>> generate a white noise in assembly, something like a random function in C. > >>> kind regard's > >> A linear congruential generator is probably good enough. Thay are easy > >> to code. If you don't need all the bits, discard the lower-order ones.http://en.wikipedia.org/wiki/Linear_congruential_generator > > > For DSP, the linear congruential generators might be good enough. > > There is a particular pair of numbers "a" and "b", such that > > > x(n) = (a x(n-1) + b ) % 2^32 > > > produced what looks and sounds like uniform random integers and has a > > period of 2^32 - 1. For processors that support 32bit fixed-point > > arithmetic (like the SHARC), this is extremely fast (the % is for > > free). The numbers "a" and "b" are in one of Knuth's volumes and the > > old Numerical Recipes in C (in the new version, they completely > > discourage the use of linear congurential generators - a pity, because > > for dithering and simple randomization, the above generator works just > > fine). > > > Hey wait, the old version of NR is available online here: > > >http://www.nrbook.com/a/bookcpdf.php > > > Chapter 7.1 (Uniform Deviates), section Quick and Dirty Generators. So > > "a" is 1664525 and "b" is 1013904223. > > The link I cited gives several pairs.
Yeah, I read it after I posted.
> Knuth (TACP, chap 3) has a > lot of good discussion, but makes no specific recommendations.
In the second edition of NR, they say that all LC PRNG are about equally non-random, and the quick and dirty solution above is just as good (or bad) as the others. As I wrote, in the current edition (3), they discourage the use of LC PRNGs all together.
> A general Linear congruential RNG has the form �X_{n+1} = (a*X_n + c) > mod m. > For the ANSI C RNG, m=2^32, a=1103515245, c=12345, and bits 30..16 are > returned.
It makes sense only to return the higher bits. It can be shown that each bit in a sequence of LC PRNs has a period of at most 2^(bit position+1), eg. the LSB has a period of at most 2. I learned that the hard way :-). Regards, Andor
Andor wrote:

   ...

> It makes sense only to return the higher bits. It can be shown that > each bit in a sequence of LC PRNs has a period of at most 2^(bit > position+1), eg. the LSB has a period of at most 2. I learned that the > hard way :-).
It's one of thos things that's obvious only after it's seen. The bottom n bits taken as a whole can have at most 2^n values. There is one relatively good PRNG in which the LSB alternates between 0 and 1. (The returned values alternate odd and even.) Jerry -- Engineering is the art of making what you want from things you can get. ����������������������������������������������������������������������� ** Posted from http://www.teranews.com **
Andor wrote:
(snip)
> In the second edition of NR, they say that all LC PRNG are about > equally non-random, and the quick and dirty solution above is just as > good (or bad) as the others. As I wrote, in the current edition (3), > they discourage the use of LC PRNGs all together.
>>A general Linear congruential RNG has the form X_{n+1} = (a*X_n + c) >>mod m. >>For the ANSI C RNG, m=2^32, a=1103515245, c=12345, and bits 30..16 are >>returned.
> It makes sense only to return the higher bits. It can be shown that > each bit in a sequence of LC PRNs has a period of at most 2^(bit > position+1), eg. the LSB has a period of at most 2. I learned that the > hard way :-).
This is true if m is a power of two. I believe it isn't necessarily true if m is not a power of two. It is very common, though, for m to be a power of two. Otherwise, if I need random low bits I divide by a prime number sort of near sqrt(m) before using it. -- glen
>>>>> "glen" == glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:
glen> Andor wrote: glen> (snip) >> In the second edition of NR, they say that all LC PRNG are about >> equally non-random, and the quick and dirty solution above is just as >> good (or bad) as the others. As I wrote, in the current edition (3), >> they discourage the use of LC PRNGs all together. >>> A general Linear congruential RNG has the form X_{n+1} = (a*X_n + c) >>> mod m. >>> For the ANSI C RNG, m=2^32, a=1103515245, c=12345, and bits 30..16 are >>> returned. >> It makes sense only to return the higher bits. It can be shown that >> each bit in a sequence of LC PRNs has a period of at most 2^(bit >> position+1), eg. the LSB has a period of at most 2. I learned that the >> hard way :-). glen> This is true if m is a power of two. I believe it isn't necessarily glen> true if m is not a power of two. It is very common, though, for m glen> to be a power of two. Otherwise, if I need random low bits I divide glen> by a prime number sort of near sqrt(m) before using it. Do you take the remainder as the random number? If so, this has a bias, so the result isn't quite uniform. May or may not matter. Ray
Raymond Toy wrote:
(snip, I wrote)

> glen> This is true if m is a power of two. I believe it isn't necessarily > glen> true if m is not a power of two. It is very common, though, for m > glen> to be a power of two. Otherwise, if I need random low bits I divide > glen> by a prime number sort of near sqrt(m) before using it.
> Do you take the remainder as the random number? If so, this has a > bias, so the result isn't quite uniform. May or may not matter.
Divide by some fairly large prime, then mod the (small) number of choices I would like. For a simulated dice roll, modulo 6. It won't be perfectly uniform, but close enough for most uses. It should be much better than some of the microsoft card games, which sometime don't seem very random at all. -- glen