以下代码应该使用fftw3库返回正弦信号的fft:
# include <stdlib.h>
# include <stdio.h>
# include <time.h>
# include <fftw3.h>
# include <iostream>
# include <cmath>
using namespace std;
int main (void)
{
int i;
const int N=256;
double Fs=1000;//sampling frequency
double T=1/Fs;//sample time
double f=500;//frequency
double *in;
fftw_complex *out;
double t[N-1];//time vector
fftw_plan plan_forward;
in = (double*) fftw_malloc( sizeof( double ) * N );
out = (fftw_complex*) fftw_malloc( sizeof( fftw_complex ) * N );
for (int i=0; i<N; i++)
{
t[i] = i*T;
in[i] = 0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
}
plan_forward = fftw_plan_dft_1d ( N, in, out, FFTW_FORWARD, FFTW_ESTIMATE );
fftw_execute ( plan_forward );
printf ( "\n" );
printf ( " Output FFT Coefficients:\n" );
printf ( "\n" );
for ( i = 0; i < N; i++ )
{
printf ( " %3d %12f %12f\n", i, out[i][0], out[i][1] );
}
fftw_destroy_plan ( plan_forward );
fftw_free ( in );
fftw_free ( out );
return 0;
}
我得到的错误无法转换‘double*’ to ‘double (*)[2]
函数中参数2的fftw_plan
。
任何人都知道如何解决这个问题?
答案 0 :(得分:3)
in
变量也需要fftw_complex *
,而不是double *
。 FFTW的复杂数据类型恰好是double[2]
,因此出现错误消息:编译器需要指向double[2]
的指针,但是您传入指向double
的指针。
答案 1 :(得分:1)
我所要做的就是将函数fftw_plan_dft_1d
更改为fftw_plan_dft_r2c_1d
。