Search code examples
pythonnumpyfftnoisenoise-generator

How to generate a pink noise image?


I am trying to replicate "Frequency Synthesis of Landscapes" by P. Bourke in Python. I thought it would be a simple

import numpy as np
from scipy.fft import fft2, ifft2

whitenoise = np.random.uniform(0,1,(256,256,3))
fouriertransformed = np.fft.fftshift(fft2(whitenoise))
pinktransformed = np.reciprocal(fouriertransformed)
pinknoise = ifft2(np.fft.ifftshift(pinktransformed)).real

but it seems to be way more complicated. How can I achieve this, and how can I check that the power really falls of by 1/f**2 in the resulting image?


Solution

  • The problem here is that by computing pinktransformed = np.reciprocal(fouriertransformed) you compute the reciprocal of the amplitudes, but what you actually want is scaling these amplitudes by 1/f**2, so you'd have to replace that line with

    pinktransformed = fouriertransformed / f**2
    

    where f is an array that contains the frequencies corresponding to each bin of the fourier transform.