使用c中的fftw进行谐波分析

时间:2013-06-20 17:42:04

标签: c gcc complex-numbers fftw

所以我有一个周期性的系列,我需要获得没有一次谐波的那个系列中最高的数量。

该系列的尺寸为360

所以我有

f(0)=value1
f(1)=value2
...
f(359)=value360

有人建议要做到这一点,我必须得到360点离散傅立叶变换DFT(f(0),f(1)... f(359)) - > (F(0),F(1)... F(359))然后将F(0)和F(359)设置为0,这将消除一次谐波。在此之后,我应该进行360点逆离散傅立叶变换iDFT,然后在结果中搜索最高值。

要做到这一点,我正在使用c中的fft库,但是我在尝试弄清楚如何正确使用它们时遇到了一些麻烦。我的周期系列由不太复杂的实数组成,所以我这样做:

#include "complex.h"
#include "fftw.h"
#include "rfftw.h"

...

fftw_real in_r[360]; //input 1d array of real numbers
fftw_complex out_c[360]; //output 1d array of complex numbers
rfftwnd_plan p_DFT; //plan to calculate the DFT
rfftwnd_plan p_iDFT; //plan to calculate the iDFT

p_DFT = rfftwnd_create_plan(1, 360, FFTW_REAL_TO_COMPLEX, FFTW_MEASURE);
p_iDFT = rfftwnd_create_plan(1, 360, FFTW_COMPLEX_TO_REAL, FFTW_MEASURE);

rffftwnd_one_real_to_complex(p_DFT,in_r,out_c);

//I GET AN ERROR IN BOTH THIS CALLS: incompatible types when assigning to type
//‘fftw_complex’ from type ‘complex double’
out_c[0]=0.0 + 0.0*_Complex_I;
out_c[359]=0.0 + 0.0*_Complex_I;

rfftwnd_one_complex_to_real(p_iDFT,out_c,in_r);

for(i=0;i<360;i++)
  max=fmaxf(in_r[i],max);

所以我有几个问题。

首先,我如何将输出数组的第一个和最后一个元素设置为0,因为它很复杂但是不允许我将复数作为一个复数?

其次,我是如何做到这一点的?或者我错过了什么?

第三(这是我接下来要做的事情的后续行动)。我可以使用FFTW库来获得该系列的第一至第四次谐波的幅度和相位吗?如果是这样,怎么样?

感谢您的帮助。

更新:

我改变了

#include "fftw.h"

代表

#include "fftw3.h"

所以在我的包含中我有

#include "complex.h"
#include "fftw3.h"
#include "rfftw.h"

但是我得到了同样的错误

更新2:

我也遇到这样的错误

/usr/include/fftw.h:307:13: error: conflicting types for ‘fftw_destroy_plan’

因为在fftw3.h之后包含了rfftw.h。但是,如果我删除rfftw.h然后我会得到这样的错误

error: unknown type name ‘fftw_real’

fftw3.h和rfftw.h之间似乎存在冲突,但是我无法删除rfftw.h,因为那时我无法使用我需要的功能。

1 个答案:

答案 0 :(得分:2)

参考:

fftw_complex out_c[360]; //output 1d array of complex numbers

...

//I GET AN ERROR IN BOTH THIS CALLS: incompatible types when assigning to type
//‘fftw_complex’ from type ‘complex double’
out_c[0]=0.0 + 0.0*_Complex_I;
out_c[359]=0.0 + 0.0*_Complex_I;

要在C99模式下使用gcc(选项-std=c99),你需要按正确的顺序包含标题:

#include <complex.h>
#include <fftw3.h>

这种方式fftw_complex应定义为本机复杂类型。


否则它是typedef double fftw_complex[2],所以分配如下:

out_c[0][0] = 0.0; // real part
out_c[0][1] = 0.0;  // imaginary part
...

请参阅here for details有关如何实施复杂数字的信息。


使用fftw3

rfftw.h已过时。来自the update-to-fftw3 page

  

FFTW 2具有单独的数据类型fftw_plan,fftwnd_plan,rfftw_plan和rfftwnd_plan,用于复杂和真实的一维和多维变换,每种类型都有自己的“破坏”功能。在FFTW 3中,所有计划都是fftw_plan类型,所有计划都被fftw_destroy_plan(计划)破坏。