Some time ago I released a fast and accurate algorithm for generating pink noise with a live demo that runs in the browser. This site has disappeared now, here it is again, updated to work with modern web audio.

The explanations seem a bit rough, like the reasoning to sum squared spectrum, which was required because the sum of uncorrelated noises is not the sum of their Z-transforms. The graphic that overlays FFT magnitude over calculated spectrum suggest that the math is solid.

The heart of the algorithm is a multirate 2nd order CIC upsampler for linear interpolation of multiple noise sources. The individual noises are just bits in a register which get updated at different intervals, so the whole multirate schebang boils down to just one addition and a subtraction. There is more beauty, like binary hacks on floats and use of LFSR register to perform FIR filtering by table lookup.

Anyway, here in full glory the original article with sound and graphics:


A New Shade of Pink

A quick and accurate method to generate pink noise that I would like to share. The code is available on GitHub here — if you are curious how a few binary operations yield pink noise, or if you just want to hear it, read on.

Pink Noise

Pink noise, or 1/f noise, has a frequency rolloff of 3 dB per octave, which for audio purposes is often the preferred choice because it is perceived as wide band noise. A reasonable way for generating pink noise digitally is to filter white noise, but unfortunately a 3 dB per octave filter design is not straightforward, so it comes with a compromise between performance and accuracy.

Another common approach found here is to sum a bunch of white noise sources that run on the nominal sample rate divided by powers of two and use zero order hold interpolation for upsampling. This produces lots of aliasing, but as the goal is noise, who cares? But still the spectrum of this emits some unpleasant bumps and valleys.

If linear interpolation, also known as first order hold, is used for upsampling, the spectrum comes much closer to the desired 3 dB rolloff. So this new method adds up twelve independent white noise sources at octave spaced sample rates upsampled by linear interpolation, added to white noise passed through a 12-tap finite impulse response (FIR) filter to fine tune the frequency response, especially close to fs/2. The illustration below shows the twelve upsampled noise sources, the FIR filtered white noise in bright purple, and below the sum of all in a nice shade of pink:

This might look too complicated to be effective, but with white noise coming in the right form, things get surprisingly easy.

White Noise

Consider white noise consisting of uncorrelated 16-bit numbers — if quantised down to 8 bit, is it still white? Unsurprisingly the answer is yes; the numbers won't get correlated by quantisation. This also holds for quantisation down to a single bit. So once all assumptions that white noise needs to have uniform, Gaussian, or whatever distribution are flushed, all that is needed is a source of uncorrelated bits: welcome, linear feedback shift register! A proper LFSR generates a stream of uncorrelated bits, spectrally flat like white noise if you interpret those as +1 and −1. If the feedback term is chosen properly, the result is a maximum length sequence (MLS) of pseudorandom bits that repeats after 2n−1 cycles. With a sample rate of 44.1 kHz, a 32-bit LFSR runs about a day before repeating, which is good enough.

FIR thing first

It is easy to see that a 12-tap FIR filter has 4096 possible outputs if the input signal is just a single bit wide. This can be precomputed and put into a lookup table, but to avoid cache misses, two tables of 64 entries each might be better. The LFSR feedback term is chosen so that twelve successive bits can be used as a 12-bit index. Split into two 6-bit indices, the whole FIR calculation boils down to two table lookups. The purpose of this filter is to minimise the error introduced by the upsampled noise signals. Finding the coefficients requires a moderate amount of math.

The zero order hold sequence g of a sequence s upsampled by m to the nominal rate, noted s↑ here, is:

$$g(n) = \sum_{i=0}^{n} \bigl[s{\uparrow}(i) - s{\uparrow}(i - m)\bigr]$$

The first order hold / linear interpolation is:

$$y(n) = m^{-1} \sum_{j=0}^{n} \sum_{i=0}^{j} \bigl[s{\uparrow}(i) - s{\uparrow}(i - m)\bigr]$$

With the transfer function:

$$Y(z) = m^{-1}\, \frac{(1 - z^{-m})^2}{(1 - z^{-1})^2}$$

For adding up and interpolating the independent bit-noise sequences sk↑ with mk = 2k, the complete signal is:

$$y(n) = \sum_{k=1}^{K} 2^{-k} \sum_{j=0}^{n} \sum_{i=0}^{j} \bigl[s_k{\uparrow}(i) - s_k{\uparrow}(i - 2^k)\bigr]$$

As each sk↑ is upsampled by inserting 2k−1 zeros, the gain of sk↑ is 2−k and the squared magnitude for the sum of all is:

$$|H(z)|^2 = (1 - z^{-1})^{-4} \sum_{k=1}^{K} 2^{-3k}\,(1 - z^{-2^k})^4$$

This squared magnitude, or power as you might call it, is used for calculating the FIR coefficients, which should have the squared magnitude response of:

$$|F(z)|^2 \approx |H(z)|^2 - a\,|z|^{-1}$$

There is surely some clever way to calculate the gain value a; iterative guessing worked well for me.

Upsampling Noise

Generating and summing up all noise sources can be made quite effective. For this we start with the zero order hold versions gk(n). We don't really perform the time expansion with zero stuffing and integration to get there, so we rewrite the summation as:

$$y(n) = \sum_{k=1}^{K} 2^{-k} \sum_{i=0}^{n} \bigl[g_k(i) - g_k(i - 2^k)\bigr]$$

As all gk are single bit signals and any 2−k can be represented by a fixed point number with just a single bit set, both terms 2−k gk(n) and 2−k gk(n − 2k) can be combined into one single word respectively, which simplifies integration a lot. For simplicity we call these two words inc and dec, where each bit represents one of the random bit sequences. Bit bK−k of inc needs to be updated every 2k times, and with a clever scheme like this, only one bit position has to be altered per step. Before updating, the old bit value of inc has to be moved to the same position in dec for the 2k-long delay.

Performance — live demo

The algorithm performs well enough to run in real time in JavaScript. Click Play below to listen. Magnitude responses of individual noise sources and the combined response are plotted. With audio enabled, a spectrum analyser is overlaid to verify the math. Noise sources can be muted individually to confirm they are independent.

Comparison

How does it compare? Below is the magnitude error of several pink noise generators. The horizontal position of graphs is arbitrarily chosen to make it readable. The error of the pink noise generator found on simplynoise.com, the self-proclaimed "best free color noise generator on the Internet", is unfortunately too large for this graph. All other algorithms can be found here. The grid is 0.1 dB for magnitude or 0.2 dB for squared magnitude.

More detail on the specific generators:

Generator Color Range Error Multiplies Bitops Notes
rbj 20 Hz – 21 kHz 0.61 dB 6 6
pke 10 Hz – 22 kHz 0.91 dB 7 7
pk3 10 Hz – 22 kHz 0.06 dB 14 13 excellent
voss 5 Hz – 22 kHz 1.83 dB ~2 ≥11
simplynoise.com 15 Hz – 18 kHz ~2.4 dB ? ? measured
new shade 9 Hz – 22 kHz 0.04 dB 0 13

Questions or corrections? Get in touch.