※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

/*
* test of self compiled fftw3
* 2D version
* @author maeda
* @date 2008/09/09
*/
 
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
 
#include "fftw3.h"
#pragma comment( lib, "fftw3.lib" )
 
#define SIZEX 16
#define SIZEY 8
#define SIZE (SIZEX*SIZEY)
 
int main( void ){
 
	fftw_complex *in  = NULL;
	fftw_complex *out = NULL;
	fftw_plan p       = NULL;
	int i,j,idx;
 
	size_t mem_size = sizeof(fftw_complex) * SIZE;
	in  = (fftw_complex*)fftw_malloc( mem_size );
	out = (fftw_complex*)fftw_malloc( mem_size );
 
	if( !in || !out ){
		fprintf( stderr, "failed to allocate %d[byte] memory(-.-)\n", (int)mem_size );
		return false;
	}
 
	// !! row-major alignment is recommended, but here, column-major.
	p = fftw_plan_dft_2d( SIZEY, SIZEX, in, out, FFTW_FORWARD, FFTW_ESTIMATE );
 
	// input data creation
	printf("----- INPUT -----\n");
	for( j=0; j<SIZEY; j++ ){
		for( i=0; i<SIZEX; i++ ){
			idx = SIZEX*j+i; // column-major alignment
			in[idx][0] = 1 + 2*sin(2*M_PI*i/SIZEX) + sin(4*M_PI*j/SIZEY);
			in[idx][1] = 0;
			printf("%d %d %lf %lf\n", i, j, in[idx][0], in[idx][1] );
		}
	}
 
	fftw_execute(p);
 
	// output is DC exchanged and scaled.
	double scale = 1. / SIZE;
	printf("\n----- RESULT -----\n");
	for( j=0; j<SIZEY; j++ ){
		for( i=0; i<SIZEX; i++ ){
			idx = SIZEX*j+i;
			printf("%d %d %lf %lf\n", i, j, out[idx][0]*scale, out[idx][1]*scale );
		}
	}
 
	if( p   ) fftw_destroy_plan(p);
	if( in  ) fftw_free(in);
	if( out ) fftw_free(out);
 
	return true;
}