[LAD] Converting single-cycle wave to harmonic spectrum

Jeanette C. julien at mail.upb.de
Mon May 10 23:00:10 CEST 2021


Hi Fons and you other FFT initiates,
I have written a simple test program (end of this mail) that produces two sine 
waves one octave apart. I used the FFTW_R2HC plan, which operates on two 
double arrays. The output can be read as two halves:
up to half the array:
output[k] = real[k] the real part of the FFT
The second half:
output[size -k] = imaginary[k], the imaginary/complex part of the FFT
The output shows 0 for the first to real values, but the same strength for the 
first two imaginary parts. Is this to be expected?

Best wishes (code below),

Jeanette
*** fft_test.cpp ***
// g++ -o fft_test fft_test.cpp -lfftw3 -lm
#include <iostream>
#include <cmath>
#include <fftw3.h>

using namespace std;

int main()
{
 	// Create input, output arrays and allocate them
 	double *in, *out;
 	in = (double *) fftw_malloc(sizeof(double) * 2048);
 	out = (double *) fftw_malloc(sizeof(double) * 2048);

 	// Create the fftw plan: halfcomplex out[k] = real[k] out[size-k] = im[k]
 	fftw_plan conv_plan = fftw_plan_r2r_1d(2048, in, out, FFTW_R2HC, FFTW_ESTIMATE);

 	// Create input, two sinewaves one octave apart
 	for (int i = 0;i<2048;i++)
 	{
 		in[i] = sin(M_PI * i/1024) + sin(M_PI * i/512);
 	}

 	// Now execute the FFT on the in-array
 	fftw_execute(conv_plan);

 	// "beautify" the output, by setting everything below 10^(-10) to zero
 	for (int i = 0;i<2048;i++)
 	{
 		if (abs(out[i]) < 1e-10)
 		{
 			out[i] = 0;
 		}
 	}

 	// Print the first 16 harmonics, both real and imaginary parts (see
 	// half-complex fftw plan FFTW_R2HC)
 	for (int i = 0;i<16;i++)
 	{
 		cout << "Harm[" << i << "] = " << out[i] << ", " << out[2047 - i] << endl;
 	}

 	// Free everything
 	fftw_destroy_plan(conv_plan);
 	fftw_free(in);
 	fftw_free(out);
 	return 0;
}
*** end of file ***

-- 
  * Website: http://juliencoder.de - for summer is a state of sound
  * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g
  * Audiobombs: https://www.audiobombs.com/users/jeanette_c
  * GitHub: https://github.com/jeanette-c

But tell me what happens when it stops? <3
(Britney Spears)


More information about the Linux-audio-dev mailing list