/*
* dft.c
*
* Created on: 2018年5月16日
* Author: ChiWang
*/
#include
#include
#include "fftw3.h"
#define N 16 //采样个数
#define PI 3.1415926535
typedef struct //complex数据类型,用于实现傅里叶运算
{
float r;
float i;
}complex;
complex dft_out[N]; //complex out data
float dft_in[N]; //sample in data
float amp[N]; //real_amp=amp*2/N
void DFT_Cal(void)
{
int i = 0;
int n = 0;
complex part[N];
for(i=0; i f=50Hz,w=2*Pi/T -> w=2*pi*f -> 220sin(2*pi*f*t)
dft_in[i] = 220*sin(2*PI*50*i*0.02/N);//一个周期采样N个点,那么一个点的时间就是T/N=0.02/N
printf("%lf ",dft_in[i]);
}
/* fftw related init */
float *fft_in_buffer=NULL;
fftwf_complex *fft_out_buffer = NULL;
fftwf_plan fft_plan;
size_t FFT_len = N;
fft_in_buffer = (float *)fftwf_malloc(sizeof(float) * FFT_len);
fft_out_buffer = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * FFT_len);
#define FFTW 1
#if FFTW
fft_plan = fftwf_plan_dft_r2c_1d(FFT_len, dft_in, fft_out_buffer, FFTW_ESTIMATE);
fftwf_execute(fft_plan);
#else
DFT_Cal();//进行DFT计算
#endif
printf("
");
for(i=0; i1,使能FFTW,运行输出(#define FFTW 1,并下载libfftw3f-3.dll,然后编译时连接该库):0.000000 84.190353 155.563492 203.253494 220.000000 203.253494 155.563492 84.190353 0.000000 -84.190353 -155.563492 -203.253494 -220.000000 -203.253494 -155.563492 -84.190353
(freq=0.000000,amp=0.000000)
(freq=50.000000,amp=220.000000)
(freq=100.000000,amp=0.000000)
(freq=150.000000,amp=0.000002)
(freq=200.000000,amp=0.000000)
(freq=250.000000,amp=0.000002)
(freq=300.000000,amp=0.000000)
(freq=350.000000,amp=0.000006)
(freq=400.000000,amp=0.000000)
(freq=450.000000,amp=0.000000)
(freq=500.000000,amp=0.000000)
(freq=550.000000,amp=0.000000)
(freq=600.000000,amp=0.000000)
(freq=650.000000,amp=0.000000)
(freq=700.000000,amp=0.000000)2,直接DFT计算运行输出(#define FFTW 0):0.000000 84.190353 155.563492 203.253494 220.000000 203.253494 155.563492 84.190353 0.000000 -84.190353 -155.563492 -203.253494 -220.000000 -203.253494 -155.563492 -84.190353
(freq=0.000000,amp=0.000002)
(freq=50.000000,amp=220.000000)
(freq=100.000000,amp=0.000002)
(freq=150.000000,amp=0.000001)
(freq=200.000000,amp=0.000000)
(freq=250.000000,amp=0.000001)
(freq=300.000000,amp=0.000000)
(freq=350.000000,amp=0.000004)
(freq=400.000000,amp=0.000000)
(freq=450.000000,amp=0.000004)
(freq=500.000000,amp=0.000000)
(freq=550.000000,amp=0.000001)
(freq=600.000000,amp=0.000000)
(freq=650.000000,amp=0.000001)
(freq=700.000000,amp=0.000002)
(freq=750.000000,amp=220.000000)