大家好,我正在使用fftw C库来计算嵌入式系统上的一些信号处理应用程序的频谱。但是,在我的项目中,我遇到了一些小问题。
下面是一个简单的程序,我编写它是为了确保我正确实现了fftw函数。基本上,我想计算12个数字序列的fft,然后进行ifft,并再次获得相同的数字序列。如果您已经安装了fftw3和gcc,那么只需使用以下命令编译即可运行此程序:
gcc -g -lfftw3 -lm fftw_test.c -o fftw_test
目前我的fft长度与输入数组大小相同。
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
#include <stdint.h>
#include <math.h>
#include <fftw3.h>
int main(void)
{
double array[] = {0.1, 0.6, 0.1, 0.4, 0.5, 0, 0.8, 0.7, 0.8, 0.6, 0.1,0};
//double array2[] = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0};
double *out;
double *err;
int i,size = 12;
fftw_complex *out_cpx;
fftw_plan fft;
fftw_plan ifft;
out_cpx = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*size);
out = (double *) malloc(size*sizeof(double));
err = (double *) malloc(size*sizeof(double));
fft = fftw_plan_dft_r2c_1d(size, array, out_cpx, FFTW_ESTIMATE); //Setup fftw plan for fft
ifft = fftw_plan_dft_c2r_1d(size, out_cpx, out, FFTW_ESTIMATE); //Setup fftw plan for ifft
fftw_execute(fft);
fftw_execute(ifft);
//printf("Input: \tOutput: \tError:\n");
printf("Input: \tOutput:\n");
for(i=0;i<size;i++)
{
err[i] = abs(array[i] - out[i]);
printf("%f\t%f\n",(array[i]),out[i]);
//printf("%f\t%f\t%f\n",(array[i]),out[i],err[i]);
}
fftw_destroy_plan(fft);
fftw_destroy_plan(ifft);
fftw_free(out_cpx);
free(err);
free(out);
return 0;
}
这将产生以下输出:
Input: Output:
0.100000 1.200000
0.600000 7.200000
0.100000 1.200000
0.400000 4.800000
0.500000 6.000000
0.000000 0.000000
0.800000 9.600000
0.700000 8.400000
0.800000 9.600000
0.600000 7.200000
0.100000 1.200000
0.000000 0.000000
显然ifft产生了一些放大的结果。在这里找到的fftw文档: 关于缩放的fftw文档。 它提到了一些缩放,但我正在使用“r2c”和“c2r”变换,而不是FFT_FORWARD和FFT_BACKWARD。任何见解都将不胜感激。