I’m trying to get FFTW to work in C. It used to work for another project (which was in JNI), and I’m more or less copying the code from that one, sadly without results.
First I generate a sine signal, like this:
double* generateSignal() {
int fs=44100;
double fsd = 44100.0; // fs in double format
double f1=1000.0;
int i;
double PI = 3.141592653589793238462643;
double t[fs];
double value = 0.0;
for (i = 0; i < fs; i++) {
t[i] = value;
value += 1.0/fsd;
}
double* signal = (double*) malloc(sizeof(double) * fs);
for (i = 0; i < fs; i++) {
signal[i] = sqrt(2) * sin(2 * PI * f1 * t[i]);
}
return signal;
}
This works, I’m only posting it for completeness.
Next, I want to transform the signal using FFTW, which I do in the following method (based on the FFTW documentation):
void processSignal(double* signal) {
int size = 44100;
int i;
fftw_complex* in = fftw_malloc(sizeof(fftw_complex) * size);
fftw_complex* out = fftw_malloc(sizeof(fftw_complex) * size);
for (i = 0; i < size; i++) {
double* ptr = in[i];
*ptr = signal[i]; // set first double, real part
*(ptr + 1) = 0.0; // set second double, imaginary part
}
fftw_plan p = fftw_plan_dft_1d(size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(p);
for (i = 0; i < size; i++) {
double* ptr = out[i];
signal[i] = *ptr; // get real part
}
fftw_destroy_plan(p);
fftw_free(in);
fftw_free(out);
}
Please note this from the FFTW documentation: typedef double fftw_complex[2];
Now, this results in all values of the signal-array being -0.000000. I really can’t see the problem with this code, so could any please point out what I’m doing wrong?
Thanks.
PS: Dropped the print-statements from my code for clarity.
Gosh, this is stupid.
The values have magnitudes like -2.0E-9. The results are rounded, thus -0.00000 is printed.
A big thanks to Fred. His question “What is your expected output?” made me have a good look at the output of the JNI-project I mentioned before, and realizing my mistake.
Also thanks for the other answers!