C:\Program Files\DevStudio\VC\bin>VCVARS32.BAT
lib /machine:i386 /def:libfftw3-3.def
lib /machine:i386 /def:libfftw3f-3.def
lib /machine:i386 /def:libfftw3l-3.def
libfftw3-3.lib (単精度用)
libfftw3f-3.lib (倍精度用)
libfftw3l-3.lib (ロング精度用)
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include "fftw3.h"
#pragma comment(lib, "libfftw3-3.lib")
#define SIZE 128
#define RANGE 10
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char line[256];
// コンソールを生成
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);
int i;
fftw_plan plan;
fftw_complex *in_buf, *out_buf;
in_buf = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*SIZE);
out_buf = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*SIZE);
for(i = 0; i < SIZE; i++){
in_buf[i][0]=0.0;
in_buf[i][1]=0.0;
}
for(i = 0; i < RANGE; i++)
in_buf[i][0] = in_buf[SIZE - i - 1][0] = 1.0;
printf("#Input Function\n");
for (i = 0; i< SIZE; i++)
printf("%d %f %f\n", i, in_buf[i][0], in_buf[i][1]);
printf("\n\n");
plan = fftw_plan_dft_1d(SIZE, in_buf, out_buf, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(plan);
printf("#FFT by fftw\n");
for (i = 0; i< SIZE; i++)
printf("%d %f %f\n", i, out_buf[i][0] / sqrt(SIZE), out_buf[i][1] / sqrt(SIZE));
gets(line); // Returnキー入力待ち
return 0;
}
FILE *fp;
fp = fopen("test.csv","w");
for (i = 0; i< SIZE; i++)
fprintf(fp,"%d,%f,%f\n", i, out_buf[i][0] / sqrt(SIZE), out_buf[i][1] / sqrt(SIZE));
fclose(fp);
;C:\Program Files\gnuplot\binary
wgnuplot
fp = fopen ("test.gp","w");
fprintf(fp,"plot \"test.csv\" using 1:2 w l,\"test.csv\" using 1:3 w l\n");
fclose(fp);
system("gnuplot -persist test.gp");
plot [:][:] 'test.dat' using ($1/1000):4 with impulses linewidth 1
void singen(WORD data[], int sample, double f)
{
double pi = 3.1415926536;
int max = 65535;
double d;
double out;
int t;
d = 2*pi*f/sample;
for(t=0; t<sample ;t++) {
out = sin(t*d);
data[t] = (WORD)((out+1)*max/2);
}
}
#define N 1000000
WORD data[N];
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int i;
FILE *fp;
singen(data,N,1000);
fp = fopen("test.csv","w");
for (i = 0; i< N; i++)
fprintf(fp,"%d\n",data[i]);
fclose(fp);
fp = fopen ("test.gp","w");
fprintf(fp, "plot [0:1000] \"test.csv\"\n");
fclose(fp);
system("gnuplot -persist test.gp");
return 0;
}
void fft_execute(WORD data[], int sample, char *fname) {
FILE *fp;
int i;
double re,im,mag,ang;
fftw_complex *in, *out;
fftw_plan p;
//sample個のFFTW用の複素数配列を確保する
in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*sample);
out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*sample);
// 複素数配列に入力データをセット
for(i=0; i<sample; i++) { // t=0〜1secのデータ
in[i][0]=data[i]; // 実数部
in[i][1]=0.0; // 虚数部
}
// 1次元のDFTを実行
p = fftw_plan_dft_1d(sample,in,out,FFTW_FORWARD,FFTW_ESTIMATE);
fftw_execute(p);
// 結果を出力する
fp = fopen(fname,"w");
for(i=0; i<sample; i++) { // f=0〜1MHz(サンプリング周波数)のデータ
re = out[i][0]; // 複素数の実数部
im = out[i][1]; // 複素数の虚数部
mag = sqrt(re*re + im*im); // 大きさを計算
ang = atan2(im,re); // 位相角を計算
fprintf(fp,"%d,%.5lf,%.5lf,%.5lf,%.5lf\n",i,re,im,mag,ang);
}
// 後始末
fftw_destroy_plan(p);
fftw_free(in);
fftw_free(out);
}
#define N 1000000
WORD data[N];
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
FILE *fp;
singen(data,N,1000);
fft_execute(data,N,"test.csv");
fp = fopen ("test.gp","w");
fprintf(fp, "set datafile separator ','\n"
"set logscale x\n"
"set logscale y\n"
"plot [:][:] 'test.csv' using ($1/1000):4 with impulses linewidth 1\n"
);
fclose(fp);
system("gnuplot -persist test.gp");
return 0;
}
void singen(WORD data[], int sample, double f)
{
double pi = 3.1415926536;
int max = 65535;
double d;
double out;
int t=0,i,j;
int m,n;
d = 2*pi*f/sample;
n = sample/f; // 1周期当たりのサンプル数
m = f; // 繰返し数(つまり周波数)
for (i=0; i<m ;i++) {
for(j=0; j<n ;j++) {
out = sin(j*d);
data[t++] = (WORD)((out+1)*max/2);
}
}
}
#define N 1000000 // サンプリング周波数
#define M 10000 // サンプル数(方形窓の大きさ)
WORD data[N];
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int i;
FILE *fp;
double f = 1000;
singen(data,N,f);
fft_execute(data,M,"test.csv");
fp = fopen ("test.gp","w");
fprintf(fp, "set datafile separator ','\nset logscale x\n"
"set logscale y\n"
"plot [:][:] 'test.csv' using ($1*%f):4 with impulses linewidth 1\n"
,(double)N/M/1000.
);
fclose(fp);
system("gnuplot -persist test.gp");
return 0;
}
plot [:][:] 'test.dat' using ($1/1000):4 with impulses linewidth 1
plot [:][:] 'test.dat' using ($1/1000*10000):4 with impulses linewidth 1
plot [:][:] 'test.dat' using ($1/m/n):4 with impulses linewidth 1