1
$\begingroup$

I am trying to understand what is explained in this webpage, in the section entitled "Generating noise with different power spectra laws". After reading that section, I thought I could do it in Mathematica, since the author provides the code in C. image where code is

Unfortunately I do not know that language, so I do not have something done in Mathematica. I would like to ask your help to make translation from C to Mathematica by calling it somehow. If someone could help me finish it would be fantastic. Thank you in advance for your help.

$\endgroup$
2
  • $\begingroup$ Why do you need the conversion? C is much faster than mma and you can use Run as a bridge? $\endgroup$ – Turgon Oct 20 '17 at 13:39
  • $\begingroup$ @Turgon I want to do the conversion from C to MMA, since I plan to do what is said in the section Frequency Synthesis of Landscapes (and clouds) of that same website $\endgroup$ – bullitohappy Oct 21 '17 at 17:07
8
$\begingroup$

There are deep conceptual differences between C and Mathematica. Look up each of the Mathematica functions used here. Click on the "Details and Options" and study this until you think you understand what is being done here. Then you will need to make certain that I didn't make any mistakes in the translation. After that you will need to do more to polish this. This might get you started:

capN = 8192; beta = 2.5; SeedRandom[1234];
{real, imag} = Transpose[Table[
   mag = (i + 1.)^-(beta/2)*RandomVariate[NormalDistribution[]]; 
   pha = 2*Pi*RandomReal[{0, 1}];
   mag*{Cos[pha], Sin[pha]},
   {i, 1, capN/2}
]];
real = Join[{0.}, real, Reverse[Most[real]]];
imag = Join[{0.}, Most[imag], {0.}, -1*Reverse[Most[imag]]];
ListPlot[Map[Re, Fourier[real + I*imag]], PlotLabel->"beta=2.5"]
Table[{i, real[[i]]}, {i, 1, capN}]

enter image description here

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you very much for your help, I am reading it carefully, I am still investigating in "Details and Options" as you recommended me $\endgroup$ – bullitohappy Oct 21 '17 at 16:56
  • $\begingroup$ Thank you very much for your help, I am reading it carefully, according to your recommendation, I am consulting "Details and Options" of the functions that you used. $\endgroup$ – bullitohappy Oct 21 '17 at 17:13
  • $\begingroup$ @bullitohappy You might also consider giving both versions exactly the same random sequence and see if the result is approximately the same. Roundoff is going to cause some small variations, but this might expose problems for you to track down and try to correct. $\endgroup$ – Bill Oct 21 '17 at 19:58
2
$\begingroup$

Here's a slightly compressed version of Bill's code, with some slight corrections:

With[{β = 5/2, n = 8192/2},
     BlockRandom[SeedRandom[1234];
                 tmp = RandomVariate[NormalDistribution[], n]
                       Append[Exp[2 π I RandomReal[1, n - 1]], 1]/Range[2, n + 1]^(β/2);
                 ListLinePlot[Re[Fourier[Join[{0}, tmp, Conjugate[Reverse[Most[tmp]]]], 
                                         FourierParameters -> {1, -1}]]]]]

colored noise

Even with the seeding, the result looks different, since I am generating the random numbers en masse instead of one-by-one. The gross properties of the noise should still be present, however.

There's supposed to be a more efficient FFT algorithm that exploits the conjugate-even structure of the problem, but I haven't tried implementing that yet.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.