Advertisement
Guest User

Untitled

a guest
Apr 5th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. fftw_complex *genenerate_fft_noise(uint width, uint height, double power, double lower, double upper) {
  2.     fftw_plan plan;
  3.     uint size = width * height;
  4.     fftw_complex *in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * size);
  5.     fftw_complex *out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * size);
  6.     fftw_complex *in_end = in + size;
  7.     fftw_complex *out_end = out + size;
  8.     fftw_complex *p;
  9.  
  10.     for (p = in; p < in_end; p++) {
  11.         double a = 2. * PI * Random() / UINT32_MAX;
  12.         (*p)[0] = std::cos(a);
  13.         (*p)[1] = std::sin(a);
  14.     }
  15.  
  16.     plan = fftw_plan_dft_2d(MapSizeY(), MapSizeX(), in, out, FFTW_FORWARD, FFTW_ESTIMATE);
  17.     fftw_execute(plan);
  18.     fftw_destroy_plan(plan);
  19.  
  20.     p = out;
  21.     for (size_t y = 0; y < height; y++) {
  22.         auto fy = fftfreq(y, height);
  23.         for (size_t x = 0; x < width; x++, p++) {
  24.             auto f = std::hypot(fy, fftfreq(x, width));
  25.             auto e = (f > lower ? std::pow(f, power) : 0.);
  26.             (*p)[0] *= e;
  27.             (*p)[1] *= e;
  28.         }
  29.     }
  30.  
  31.     plan = fftw_plan_dft_2d(height, width, out, in, FFTW_BACKWARD, FFTW_ESTIMATE);
  32.     fftw_execute(plan);
  33.     fftw_destroy_plan(plan);
  34.  
  35.     fftw_free(out);
  36.  
  37.     normalize(in, in_end);
  38.  
  39.     return in;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement