我试图在C中进行离散傅立叶变换.
最初只是蛮力方法.首先,我让程序打开一个数据文件(幅度)并将数据放入一个数组(只有一个,因为我限制自己使用实值输入).
但是变换看起来不对,所以我尝试生成一个简单的波函数并检查它是否正确转换.
这是我的代码,剥去了钟声和口哨声:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M_PI 3.14159265358979323846
//the test wavefunction
double theoretical(double t)
{
double a = sin(M_PI * t) + 2 * sin(2 * M_PI * t) + 4 * sin(4 * M_PI * t);
return a;
}
//-------------------------------------------------------------------------
void dftreal(double inreal[], double outreal[], double outimag[], int linecount)
{
int n, k;
for (k = 0; k < linecount; k++)
{
double sumreal = 0;
double sumimag = 0;
for (n …Run Code Online (Sandbox Code Playgroud)