English: Plot of sampled pink noise with a power spectral density of 1/f. The noise was created by random sampling of white noise and subsequent filtering in frequency space.
#! /usr/bin/env python3# -*- coding:utf8 -*-importmatplotlib.pyplotaspltimportnumpyasnpfrommathimport*plt.rcParams['font.sans-serif']='DejaVu Sans'np.random.seed(6)"""Note: pink noise is actually not so well depictable. As more higher frequencycomponentes are added, the amplitude goes to infinity and will be dominatedby high-frequency noise. Thus, the image changes a lot with the cutoff samplingdensity. We choose roughtly half a linewidth for sampling for a decentappearance."""nsamples=501t0=1.t=np.linspace(0,t0,nsamples)dt=t[1:]-t[:-1]white_noise=np.random.normal(0,1,nsamples)fourier_amplitudes=np.fft.rfft(white_noise)frequencies=np.fft.rfftfreq(nsamples,d=t[1]-t[0])fourier_amplitudes[1:]/=np.sqrt(frequencies)[1:]# 1/sqrt(f) amplitude spectrumX=np.fft.irfft(fourier_amplitudes,n=nsamples,norm='ortho')X-=np.mean(X)X/=np.std(X)fig=plt.figure(figsize=(520/90.0,340/90.0),dpi=72)plt.plot(t,X)plt.grid(True)plt.xlim(t[0],t[-1])plt.ylim(-3.5,3.5)plt.xlabel('t')plt.ylabel('X')plt.tight_layout()plt.savefig('Pink-noise-trace.svg')