C++ 使用FFTW并计算得到的傅里叶级数
我是FFTW的新手。我想把一个函数分解成傅里叶级数。到目前为止,我还没有做到。我的代码如下:C++ 使用FFTW并计算得到的傅里叶级数,c++,fft,fftw,dft,C++,Fft,Fftw,Dft,我是FFTW的新手。我想把一个函数分解成傅里叶级数。到目前为止,我还没有做到。我的代码如下: // 1) Create discretizations for my function 'my_function' int N = 100; // number of discretizations float x_step = (x_end - x_start) / ((float)(N - 1)); fftw_complex *Input, *Output; fftw_plan
// 1) Create discretizations for my function 'my_function'
int N = 100; // number of discretizations
float x_step = (x_end - x_start) / ((float)(N - 1));
fftw_complex *Input, *Output;
fftw_plan my_plan;
Input = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
Output = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
float x = x_start;
ForIndex(i, N) {
Input[i][0] = my_function(x);
cout << "Input[" << i << "]=" << Input[i][0] << endl;
Input[i][1] = 0;
x += x_step;
}
my_plan = fftw_plan_dft_1d(N, Input, Output, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(my_plan);
// 3) Evaluation - this is the part I am confused with
// I should get something very close to 'my_function' when I plot, shouldn't I?
ForIndex(i, N) {
float sum = 0;
float x = (float)i;
sum = Output[0][0] / 2.0f;
for (int k = 1; k < N; k++) {
// Fourier series
// http://en.wikipedia.org/wiki/Fourier_series
float s = 2.0f*(float)M_PI * (float)k * x / (float)N;
sum += Output[k][0] * std::cos(s) + Output[k][1] * std::sin(s);
// I also tried
// sum += Output[k][0] * std::sin(s + Output[k][1]);
// to no avail
}
cout << "For x=" << x << ", y=" << (sum / (float)N) << endl;
}
//1)为我的函数“我的函数”创建离散化
int N=100;//离散化次数
浮动x_步进=(x_结束-x_开始)/(浮动)(N-1));
fftw_复数*输入,*输出;
fftw_计划我的计划;
输入=(fftw_复合体*)fftw_malloc(sizeof(fftw_复合体)*N);
输出=(fftw_复合体*)fftw_malloc(sizeof(fftw_复合体)*N);
浮动x=x_开始;
ForIndex(i,N){
输入[i][0]=my_函数(x);
cout您非常接近预期结果!获得正确输出需要两点:
- 第一项
Output
是输入信号的平均值。因此它是sum=Output[0][0];
,而不是sum=Output[0][0]/2.0f;
i
是复数,i*i==-1
因此,当虚部相乘时,必须在结果中添加减号。因此:
sum += Output[k][0] * std::cos(s) - Output[k][1] * std::sin(s);
下面是一段经过更正的代码,将由g++main.cpp-o main-lfftw3-lm
编译:
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
float my_function(float x){
return x;
}
int main(int argc, char* argv[]){
// 1) Create discretizations for my function 'my_function'
int N = 100; // number of discretizations
float x_end=1;
float x_start=0;
int i;
float x_step = (x_end - x_start) / ((float)(N - 1));
fftw_complex *Input, *Output;
fftw_plan my_plan;
Input = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
Output = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N);
float x = x_start;
for(i=0;i<N;i++){
Input[i][0] = my_function(x);
cout << "Input[" << i << "]=" << Input[i][0] << endl;
Input[i][1] = 0;
x += x_step;
}
my_plan = fftw_plan_dft_1d(N, Input, Output, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(my_plan);
for(i=0;i<N;i++){
printf("%d %g+%gi\n",i,Output[i][0],Output[i][1]);
}
// 3) Evaluation - this is the part I am confused with
// I should get something very close to 'my_function' when I plot, shouldn't I?
for(i=0;i<N;i++) {
float sum = 0;
float x = (float)i;
sum = Output[0][0];
for (int k = 1; k < N; k++) {
// Fourier series
// http://en.wikipedia.org/wiki/Fourier_series
float s = 2.0f*(float)M_PI * (float)k * x / (float)N;
sum += Output[k][0] * std::cos(s) - Output[k][1] * std::sin(s);
// I also tried
// sum += Output[k][0] * std::sin(s + Output[k][1]);
// to no avail
}
cout << "For i=" << i <<" x="<<x_start+x_step*i<< " f(x)="<<my_function(x_start+x_step*i)<<" y=" << (sum / (float)N) << endl;
}
}
my_plan2 = fftw_plan_dft_1d(N, Output, input, FFTW_BACKWARD, FFTW_ESTIMATE);