DSPRelated.com
Forums

Generate Pink Noise Efficiently

Started by Robert Scott March 19, 2013
On Wed, 20 Mar 2013 02:35:58 -0700 (PDT), kalvin.news@gmail.com wrote:

>Combine http://www.firstpr.com.au/dsp/rand31/ with http://www.firstpr.com.au/dsp/pink-noise/ and you are pretty much done :) >
Apparently I had the wrong definition of "pink noise". What I really wanted was not pink noise but any noise with a power distribution that is centered around a given frequency and a bandwidth that is wide enough not to sound like a coherent tone. Sorry for the misunderstanding. The pseudo-random source looks very promising, if I can follow it with an efficient 2-pole bandpass filter with adjustable center frequency. Robert Scott Hopkins, MN
I don't think you'll find a more efficient algorithm than an IIR filter.

Here is a derivation of numerous biquad filters based on the Bilinear
Transform:

http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt

It was written by a frequent poster on these boards and I found it very
informative.



>On Wed, 20 Mar 2013 02:35:58 -0700 (PDT), kalvin.news@gmail.com wrote: > >>Combine http://www.firstpr.com.au/dsp/rand31/ with
http://www.firstpr.com.au/dsp/pink-noise/ and you are pretty much done :)
>> > >Apparently I had the wrong definition of "pink noise". What I really >wanted was not pink noise but any noise with a power distribution that >is centered around a given frequency and a bandwidth that is wide >enough not to sound like a coherent tone. Sorry for the >misunderstanding. > >The pseudo-random source looks very promising, if I can follow it >with an efficient 2-pole bandpass filter with adjustable center >frequency. > >Robert Scott >Hopkins, MN >
On Wed, 20 Mar 2013 03:02:15 +0000, glen herrmannsfeldt wrote:

> Robert Scott <no-one@notreal.invalid> wrote: >> I am looking for a compute-efficient algorithm to generate pink noise. >> The application must run on a smartphone (iPhone or Android w/NDK) and >> take up a small fraction of the available compute time. The >> application has other compute-intensive real-time tasks that cannot be >> bogged down by this noise-generating task. > > (snip) > > Can you tell about how much computation, adds and multiplies, can be > done per second? > > An LFSR should make an efficient white noise generator, but I haven't > thought about how to pinkify it. > > Now, say you generate two streams with different peaks, then take an > appropriate fraction of each. That is, instead of shifting the peak of > one source shift the fractions of two. That requires at least two > multiplies per sample. > > -- glen
Google is Your Friend. One of the many ways to generate pink noise that works well in a sampled environment is to add up the outputs of a bunch of discrete-time white noise generators, with periods 1, 2, 4, ... There's many schemes (which you find with a web search; many from sometime-denizens of this group). The "many white noise generator" scheme has a variant that staggers the sampling of the 2, 4, 8 part so that each sample only updates two generators -- you could alter that and just get two bits from one long LFSR, use one for the "fast" generator and the other one for whichever "slow" generator has come up. You can also run white noise through a pinking filter, as RBJ suggests. Here again, Google is Your Friend. I suspect that the LFSR method will take less processing power (I know it would in an FPGA!) but I cannot be sure. -- My liberal friends think I'm a conservative kook. My conservative friends think I'm a liberal kook. Why am I not happy that they have found common ground? Tim Wescott, Communications, Control, Circuits & Software http://www.wescottdesign.com
On 3/20/13 1:27 AM, glen herrmannsfeldt wrote:
> robert bristow-johnson<rbj@audioimagination.com> wrote: > >>> Robert Scott<no-one@notreal.invalid> wrote: >>>> I am looking for a compute-efficient algorithm to generate pink noise. > > (snip, I wrote) >>> Can you tell about how much computation, adds and multiplies, >>> can be done per second? > >>> An LFSR should make an efficient white noise generator, > >> single bits, well a bipolar single bit. don't use the shift register >> word for a random number. > > In general I agree, but for noise it doesn't have to be that random. >
it does if you want it to be white. what i am saying is that the spectrum of the shift register word, taken as an N-bit twos-comp, is not white. but if you take the bits that fall offa the end, a[n], and do this: x[n] = (-1)^a[n] *that* is white. the p.d.f. is not guassian nor uniform, it's two delta functions that are almost equal in height, but not quite, because in the LFSR sequence, there is one more a[n]=1 than there is a[n]=0. now that's not so bad if it goes through 3 or more stages of filtering to get to pink. i would bet that what comes outa the pinking filter will be pretty close to guassian. especially if you have a lot of sections. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On 3/20/2013 7:40 AM, Robert Scott wrote:
> On Wed, 20 Mar 2013 02:35:58 -0700 (PDT), kalvin.news@gmail.com > wrote: > > The pseudo-random source looks very promising, if I can follow it > with an efficient 2-pole bandpass filter with adjustable center > frequency.
A loop as small as ~100 random numbers appear as noise to human ear. 1. Pre-create a circular buffer of bandpassed noise at the lowest frequency. 2. Adjust playback rate to change the center frequency keeping the same relative bandwidth. A crude adjustment by generating the index of the next sample would be sufficient. That is probably most efficient way. Vladimir Vassilevsky DSP and Mixed Signal Designs www.abvolt.com
On 3/20/13 8:40 AM, Robert Scott wrote:
> On Wed, 20 Mar 2013 02:35:58 -0700 (PDT), kalvin.news@gmail.com wrote: > >> Combine http://www.firstpr.com.au/dsp/rand31/ with http://www.firstpr.com.au/dsp/pink-noise/ and you are pretty much done :) >> > > Apparently I had the wrong definition of "pink noise". What I really > wanted was not pink noise but any noise with a power distribution that > is centered around a given frequency and a bandwidth that is wide > enough not to sound like a coherent tone. Sorry for the > misunderstanding. > > The pseudo-random source looks very promising, if I can follow it > with an efficient 2-pole bandpass filter with adjustable center > frequency. >
are you trying to synthesize an "airy" musical note of some sort? anyway, as Bob Adams suggested: "If you are sweeping bandpass-filtered white noise you can just set the amplitude as you sweep to get the pinkiness that you desire." this means, if the bandwidth in *octaves* is held constant as the center frequency is adjusted, then you want to adjust the gain of the BPF (or perhaps the peaking EQ filter) to be 1/sqrt(f0). because if the bandwidth in octaves is held constant, the bandwidth in Hz increases as f0 increases. and the power will increase proportionately, if you do not compensate with the constant gain. so each of the numerator coefficients should be scaled by (f0)^(-1/2) to keep the power of the filtered "tone" constant. probably, you want to adjust the gain to compensate for narrowing the bandwidth, but i don't know for sure you want to do that. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On 3/20/2013 8:40 AM, Robert Scott wrote:
> On Wed, 20 Mar 2013 02:35:58 -0700 (PDT), kalvin.news@gmail.com wrote: > >> Combine http://www.firstpr.com.au/dsp/rand31/ with http://www.firstpr.com.au/dsp/pink-noise/ and you are pretty much done :) >> > > Apparently I had the wrong definition of "pink noise". What I really > wanted was not pink noise but any noise with a power distribution that > is centered around a given frequency and a bandwidth that is wide > enough not to sound like a coherent tone. Sorry for the > misunderstanding. > > The pseudo-random source looks very promising, if I can follow it > with an efficient 2-pole bandpass filter with adjustable center > frequency.
Maybe there is another way of thinking about this. Instead of creating noise and then shaping the spectrum, why not generate a tone with noise in the frequency content? I think if you add phase noise to a sine generator it will do something like you want, no? The sine generator can be easily tuned. The noise amplitude may have to be adjusted for the frequency to give a constant relative bandwidth. As it turns out, there are a number of ways of easily generating a tone from a phase sequence. -- Rick
On Wed, 20 Mar 2013 18:55:47 -0400, rickman <gnuarm@gmail.com> wrote:

>On 3/20/2013 8:40 AM, Robert Scott wrote: >> On Wed, 20 Mar 2013 02:35:58 -0700 (PDT), kalvin.news@gmail.com wrote: >> >>> Combine http://www.firstpr.com.au/dsp/rand31/ with http://www.firstpr.com.au/dsp/pink-noise/ and you are pretty much done :) >>> >> >> Apparently I had the wrong definition of "pink noise". What I really >> wanted was not pink noise but any noise with a power distribution that >> is centered around a given frequency and a bandwidth that is wide >> enough not to sound like a coherent tone. Sorry for the >> misunderstanding. >> >> The pseudo-random source looks very promising, if I can follow it >> with an efficient 2-pole bandpass filter with adjustable center >> frequency. > >Maybe there is another way of thinking about this. Instead of creating >noise and then shaping the spectrum, why not generate a tone with noise >in the frequency content?
What does "noise in the frequency content" mean wrt a tone?
>I think if you add phase noise to a sine generator it will do something >like you want, no? The sine generator can be easily tuned. The noise >amplitude may have to be adjusted for the frequency to give a constant >relative bandwidth.
A complex tone has constant magnitude and with completely random phase will not produce gaussian or uniform distribution of the magnitude in the real or imaginary parts, so that must be considered. In other words, it is not the equivalent of mixing a desired noise distribution or spectrum to the frequency of the tone. Since the OP seems interested in some "hump" of noise at a particular frequency, I'd suggest generating complex noise at baseband, apply a low-pass filter of the desired shape, and then apply a complex mix to the desired frequency. If complex noise is desired then that should get you there, if real-valued noise is desired, then take the real part of the result.
>As it turns out, there are a number of ways of easily generating a tone >from a phase sequence.
Eric Jacobsen Anchor Hill Communications http://www.anchorhill.com
rickman <gnuarm@gmail.com> wrote:
> On 3/20/2013 8:40 AM, Robert Scott wrote:
(snip)
>> Apparently I had the wrong definition of "pink noise". What I really >> wanted was not pink noise but any noise with a power distribution that >> is centered around a given frequency and a bandwidth that is wide >> enough not to sound like a coherent tone. Sorry for the >> misunderstanding.
>> The pseudo-random source looks very promising, if I can follow it >> with an efficient 2-pole bandpass filter with adjustable center >> frequency.
> Maybe there is another way of thinking about this. Instead of creating > noise and then shaping the spectrum, why not generate a tone with noise > in the frequency content?
That is that I was trying to think of, but didn't. I had some ideas of adding and subtracting LFSR outputs, but that didn't seem to get close.
> I think if you add phase noise to a sine generator it will do something > like you want, no? The sine generator can be easily tuned. The noise > amplitude may have to be adjusted for the frequency to give a constant > relative bandwidth.
OK, so run a phase accumulator, add the output of an LFSR, and then into a sine LUT. Or use the LFSR output as input to the phase accumulator. But back to my previous thought. If you take two uniform random numbers and add them, then the sum is not uniform. The more you add, the closer you get to Gaussian shape. A favorite way to generate random Gaussians is to add uniformly distributed random numbers, multiply by a constant (which might be 1) then subtract the mean. The LFSR needs AND, table lookup, and XOR. A few of those and some adders doesn't take much (computational) work. If you add that to a constant, and use the constant as input to the phase accumulator, the mean phase increment can change instantly. I would have to think at least a little bit to know what the power spectrum would look like. Actually, I just realized that this is what they do to reduce RFI from computers. A single frequency clock has a nice big peak in its power spectrum. By randomizing the clock rate, even a little bit, the spectrum is spread out and much less of a problem.
> As it turns out, there are a number of ways of easily generating a tone > from a phase sequence.
You mean like a sine LUT with or without interpolation? And speed vs. time trade-off in table size vs. interpolation. -- glen
Eric Jacobsen <eric.jacobsen@ieee.org> wrote:

(snip)

> What does "noise in the frequency content" mean wrt a tone?
(snip, someone wrote)
>>I think if you add phase noise to a sine generator it will do something >>like you want, no? The sine generator can be easily tuned. The noise >>amplitude may have to be adjusted for the frequency to give a constant >>relative bandwidth.
> A complex tone has constant magnitude and with completely random phase > will not produce gaussian or uniform distribution of the magnitude in > the real or imaginary parts, so that must be considered. In other > words, it is not the equivalent of mixing a desired noise distribution > or spectrum to the frequency of the tone.
Okay, but say only slightly random phase. Since it is hard to do the lookup in radians, consider the phase in cycles, so a binary number from 0 to almost 1. Now, say you add random noise with amplitude 1/32 or even 1/64 or 1/128. (Other values will require a multiply.)
> Since the OP seems interested in some "hump" of noise at a particular > frequency, I'd suggest generating complex noise at baseband, apply a > low-pass filter of the desired shape, and then apply a complex mix to > the desired frequency. If complex noise is desired then that should > get you there, if real-valued noise is desired, then take the real > part of the result.
The OP seems to want to minimize the amount of computation required. Shifts and adds are still, on most processors, faster than multiplies and divides. -- glen