HighMap
HighMap copied to clipboard
Add 2D spectrum output
libfftw3-dev
#include <fftw3.h>
#include <vector>
#include <cmath>
#include <iostream>
int main() {
int N = 256; // Size of the 2D array
// Input array (real data)
std::vector<double> in(N * N);
// Output array for complex numbers (N * (N/2+1) because of the symmetry in FFTW's output)
std::vector<fftw_complex> out(N * (N / 2 + 1));
// Fill the input array with some example data (e.g., a simple gradient)
for (int i = 0; i < N * N; ++i) {
in[i] = static_cast<double>(i);
}
// Create the FFTW plan for real-to-complex transformation
fftw_plan plan = fftw_plan_dft_r2c_2d(N, N, in.data(), out.data(), FFTW_ESTIMATE);
// Execute the FFT
fftw_execute(plan);
// Compute and print the modulus of the first few output values
std::cout << "Modulus of FFT output:\n";
for (int i = 0; i < 5; ++i) {
double real_part = out[i][0];
double imag_part = out[i][1];
double modulus = std::sqrt(real_part * real_part + imag_part * imag_part);
std::cout << "modulus[" << i << "] = " << modulus << "\n";
}
// Clean up the FFTW resources
fftw_destroy_plan(plan);
fftw_cleanup();
return 0;
}