How to generate Pink Noise using Mathematica 8

1

I have tried to generate pink noise with the following commands :

audio = AudioGenerator["Pink"]

data = Transpose@{Range[0, 1 - 1/44100, 1/44100.], AudioData[audio]};

ListPlot[data, Frame -> True, Axes -> False]

These command I get from this blog . But by typing these command to Mathematica 8 , error messages are appeared.

Transpose::nmtx: The first two levels of the one-dimensional list {{0.,0.0000226757,0.0000453515,0.0000680272,0.0000907029,0.000113379,0.000136054,<<37>>,0.000997732,0.00102041,0.00104308,0.00106576,0.00108844,0.00111111,<<44050>>},<<1>>} cannot be transposed. >>

ListPlot::lpn: Transpose[{{0.,0.0000226757,0.0000453515,0.0000680272,0.0000907029,0.000113379,0.000136054,<<37>>,0.000997732,0.00102041,0.00104308,0.00106576,0.00108844,0.00111111,<<44050>>},<<1>>}] is not a list of numbers or pairs of numbers. >>

Please tell the solution.

BAPIRS

Posted 2017-10-17T05:39:39.063

Reputation: 21

If you read the error, it tells you the issue is in the Transpose call. In particular it says the lists can't be transposed. Generally that means they have different lengths. – b3m2a1 – 2017-10-17T06:00:11.783

1@b3m2a1 The irony is that it works well on my system. Seemingly, AudioData[audio] does not evaluate on the OP's system... and that's because AudioData was introduced with version 11. – Henrik Schumacher – 2017-10-17T06:02:03.813

@HenrikSchumacher it just yields different Length data. Must be system specific as it doesn't work for me (but was trivial to adapt). – b3m2a1 – 2017-10-17T06:02:44.920

@b3m2a1 Henrik is right, it's just because in v8 there's no AudioGenerator and AudioData. To BAPIRS: You can just check the color of these 2 functions, they're blue in v8, which suggests they don't have any internal definitions. Remember built-in funcitons are black. (There exists exceptions for undocumented functions though. ) Always keeping an eye on the syntax highlighting is a effective way to avoid simple mistakes. – xzczd – 2017-10-18T07:10:55.047

1@xzczd That's obviously the OPs problem, but interestingly, though, the code provided doesn't even always work in v11.2. On my mac I had to actually use the true Length of the AudioData to get it to work. – b3m2a1 – 2017-10-18T07:13:35.913

Answers

7

Mathematica 8 does not have AudioGenerator, but you can implement your own pink noise function. I looked up how to do this, and the simplest way seems to be the Voss algorithm, described in the first paragraph of this article, among other places.

Quoting:

The fundamental idea of the algorithm is to add up several sequences of uniform random numbers that get updated at different rates. The first source gets updated at every time step; the second source every other time step, the third source ever fourth step, and so on.

Here's my Mathematica 8 compatible implementation:

double = Riffle[#, #] &; (* transforms {1,2,3} to {1,1,2,2,3,3} *)

pink[k_] := Nest[
   Function[arr,
    double[arr] + RandomReal[{-1, 1}, 2 Length[arr]]
    ],
   {0.},
   k
   ];

norm[arr_] := arr/Max@Abs[arr]

pinkNoise[samples_] := norm@Take[pink[Ceiling@Log2[samples]], samples]

pink[k] generates $2^k$ samples. pinkNoise[n] will generate n samples by dropping samples from the end of pink's output and normalizing the result to lie between -1..1.

The article I linked is actually about how to improve this algorithm. You should probably read it and implement the better version.

Szabolcs

Posted 2017-10-17T05:39:39.063

Reputation: 213 047

2

AudioData was introduced with version 11.0, so it won't work with Mathematica 8.

Henrik Schumacher

Posted 2017-10-17T05:39:39.063

Reputation: 85 430