Function Repository Resource:

ColoredNoise

Source Notebook

Create an array with a specified slope in the power spectral density

Contributed by: Julien Kluge

ResourceFunction["ColoredNoise"][count,alpha]

creates array of length count with a slope of alpha/2 in the Fourier domain.

ResourceFunction["ColoredNoise"][count,name]

creates array of length count with a slope corresponding to the name noise color in the Fourier domain.

ResourceFunction["ColoredNoise"][{c1,c2,},alpha]

creates a multidimensional array of lengths c1,c2, ….

Details and Options

The mean of the created array is by default effectively zero.
The standard deviation of the created array is normalized to one.
The function threads over the alpha parameter.

Examples

Basic Examples (3) 

Create 10 samples of white noise (slope zero):

In[1]:=
ResourceFunction["ColoredNoise"][10, 0]
Out[1]=

Create 10 samples of pink noise (slope minus one):

In[2]:=
ResourceFunction["ColoredNoise"][10, "Pink"]
Out[2]=

Create violet noise in two dimensions:

In[3]:=
ResourceFunction["ColoredNoise"][{5, 4}, "Violet"]
Out[3]=

Scope (3) 

Plot different noise slopes in the time domain:

In[4]:=
ListLinePlot[ResourceFunction["ColoredNoise"][1000, #], PlotLabel -> ("\[Alpha] = " <> ToString[#])] & /@ {-2, -1, 0, 1, 2}
Out[4]=

Plot different noise slopes in the Fourier domain:

In[5]:=
ListLogLogPlot[
   Abs[Fourier[ResourceFunction["ColoredNoise"][10000, #]]][[
    2 ;; 5000]], PlotLabel -> ("2\[Cross]Slope = \[Alpha] = " <> ToString[#]), GridLines -> Automatic] & /@ {-2, -1, 0, 1, 2}
Out[5]=

Create the same display for two dimensions:

In[6]:=
MatrixPlot[
   Log@Abs[Fourier[ResourceFunction["ColoredNoise"][{200, 200}, #]][[
      2 ;; 100, 2 ;; 100]]], ColorFunction -> GrayLevel, PlotLabel -> ("\[Alpha] = " <> ToString[#])] & /@ {-2, -1, 0, 1, 2}
Out[6]=

All supported color keywords with their respective alpha applied:

In[7]:=
ResourceFunction[
 "ColoredNoise"][3, {{2, "Violet", "Purple"}, {1, "Blue"}, {0, "White"}, {-1, "Pink", "Flicker"}, {-2, "Red", "Brown"}}]
Out[7]=

The mean and standard deviation are always zero and one, respectively:

In[8]:=
{Mean[#], StandardDeviation[#]} &@
 ResourceFunction["ColoredNoise"][1000, 1]
Out[8]=

Options (4) 

Deviation (2) 

Turn off the normalization of the standard deviation:

In[9]:=
StandardDeviation[
 ResourceFunction["ColoredNoise"][10, 0, "Deviation" -> True]]
Out[9]=

Set the normalization of the standard deviation to a specific value:

In[10]:=
StandardDeviation[
 ResourceFunction["ColoredNoise"][10, 0, "Deviation" -> 5]]
Out[10]=

FourierParameters (1) 

Apply the Fourier transform with different FourierParameters:

In[11]:=
ResourceFunction["ColoredNoise"][10, 0, FourierParameters -> {1, -1}]
Out[11]=

Offset (1) 

Set the offset to a specific value:

In[12]:=
Mean[ResourceFunction["ColoredNoise"][10, 0, "Offset" -> 5]]
Out[12]=

Applications (2) 

Visualize the relation of alphas to the slopes in the power spectral density, the Allan deviation and the influence to the autocorrelation:

In[13]:=
plotOptions = {GridLines -> Automatic, Joined -> True, ImageSize -> 200, PlotStyle -> Black};
Grid[{{"\[Alpha]", "Noise", "Power Spectral Density", "Allan Deviation", "Autocorrelation"}}~Join~
  Table[noise = ResourceFunction["ColoredNoise"][10000, alpha]; {alpha,
    ListLinePlot[noise, Evaluate[Sequence @@ plotOptions]],
    ListLogLogPlot[
     ResourceFunction["WelchSpectralEstimate"][noise, 1.0, "Detrend" -> None], Evaluate[Sequence @@ plotOptions]],
    ListLogLogPlot[
     ResourceFunction["AllanDeviation"][noise, 1.0, {{300}}, "FrequencyData" -> True], Evaluate[Sequence @@ plotOptions], PlotRange -> {{1, Length[noise]/3}, All}],
    ListLinePlot[
     RotateRight[ListCorrelate[noise, noise, {-1, -1}]/Length[noise], Floor[Length[noise]/2]], Evaluate[Sequence @@ plotOptions], PlotRange -> {All, {-1.05, 1.05}}]
    }, {alpha, -2, 2}], Frame -> All]
Out[14]=

Create array with a specified shape of the Allan deviation, mimicking an active atomic clock:

In[15]:=
SeedRandom[314159265];
noise = 5*10^-14*ResourceFunction["ColoredNoise"][10^6, "Blue"] + 10^-14*ResourceFunction["ColoredNoise"][10^6, "Flicker", "Deviation" -> True] + 5*10^-17*
    ResourceFunction["ColoredNoise"][10^6, "Red", "Deviation" -> True];
ListLogLogPlot[
 ResourceFunction["AllanDeviation"][noise, 1.0, {{300}}, "FrequencyData" -> True], Joined -> True, GridLines -> Automatic, PlotRange -> {{0.5, 3*10^5}, {10^-14, 7*10^-14}}, Epilog -> {Dashed,
   Black, InfiniteLine[Log[{{1, 5*10^-14}, {10, 10^-14}}]], Inset["\[Alpha]=1", Log@{1, 2*10^-14}],
   Red, InfiniteLine[Log[{{1, 1.2*10^-14}, {10, 1.2*10^-14}}]], Inset["\[Alpha]=-1", Log@{500, 1.5*10^-14}],
   Blue, InfiniteLine[Log[{{10^4, 10^-14}, {10^5, 5*10^-14}}]], Inset["\[Alpha]=-2", Log@{5*10^4, 1.5*10^-14}]
   }]
Out[17]=

Possible Issues (2) 

The samples argument must be greater than two:

In[18]:=
ResourceFunction["ColoredNoise"][#, 0] & /@ Range[-1, 3]
Out[18]=

The alpha argument must be a known noise color or else a real number:

In[19]:=
ResourceFunction["ColoredNoise"][10, "Orange"]
Out[19]=

Neat Examples (2) 

Noise with alpha values between -1 and -2 sound like a waterfall or turbulent flowing river:

In[20]:=
noise = ResourceFunction["ColoredNoise"][
   44100*3, -1]; (*create 3 seconds of noise*)

noise = noise/
   Max[Abs[MinMax[noise]]]; (*normalize to [-1,1]*)
ListPlay[noise, SampleRate -> 44100]
Out[21]=

Create soft 2D-noise maps by choosing low alpha values:

In[22]:=
MatrixPlot[ResourceFunction["ColoredNoise"][{200, 200}, -6]]
Out[22]=

Publisher

Julien Kluge

Version History

  • 1.0.0 – 08 April 2022

Source Metadata

Related Resources

License Information