标签:fftw
FFTW首页:http://www.fftw.org/测试demo
#include "fftw3.h" #include <stdio.h> #define N 8 int main() { int i; fftw_complex *din,*out; fftw_plan p; din = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); if((din==NULL)||(out==NULL)) { printf("Error:insufficient available memory\n"); } else { for(i=0; i<N; i++)/*测试数据*/ { din[i][0] = i+1; din[i][1] = 0; } } p = fftw_plan_dft_1d(N, din, out, FFTW_FORWARD,FFTW_ESTIMATE); fftw_execute(p); /* repeat as needed */ fftw_destroy_plan(p); fftw_cleanup(); for(i=0;i<N;i++)/*OUTPUT*/ { printf("%f,%fi\n",din[i][0],din[i][1]); } printf("\n"); for(i=0;i<N;i++)/*OUTPUT*/ { printf("%f,%fi\n",out[i][0],out[i][1]); } if(din!=NULL) fftw_free(din); if(out!=NULL) fftw_free(out); getchar(); return 0; }结果输出如下:
IplImage *img1 = 0; IplImage *img2 = 0; uchar *img1_data; uchar *img2_data; fftw_complex *data_in; fftw_complex *fft; fftw_complex *ifft; fftw_plan plan_f; fftw_plan plan_b; int width, height, step; int i, j, k; /* load original image */ img1 = cvLoadImage("lena.bmp", CV_LOAD_IMAGE_GRAYSCALE); if (img1 == 0) { std::cout << "cannot load file" << std::endl; return 0; } /* create new image for IFFT result */ img2 = cvCreateImage(cvSize(img1->width, img1->height), IPL_DEPTH_8U, 1); /* get image properties */ width = img1->width; height = img1->height; step = img1->widthStep; img1_data = (uchar *)img1->imageData; img2_data = (uchar *)img2->imageData; /* initialize arrays for fftw operations */ data_in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * width * height); fft = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * width * height); ifft = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * width * height); /* create plans */ plan_f = fftw_plan_dft_1d(width * height, data_in, fft, FFTW_FORWARD, FFTW_ESTIMATE); plan_b = fftw_plan_dft_1d(width * height, fft, ifft, FFTW_BACKWARD, FFTW_ESTIMATE); /* load img1's data to fftw input */ for (i = 0, k = 0; i < height; ++i) { for (j = 0; j < width; ++j) { data_in[k][0] = (double)img1_data[i * step + j]; data_in[k][1] = 0.0; k++; } } /* perform FFT */ fftw_execute(plan_f); /* perform IFFT */ fftw_execute(plan_b); /* normalize IFFT result */ for (i = 0; i < width * height; ++i) { ifft[i][0] /= (double)(width * height); } /* copy IFFT result to img2's data */ for (i = 0, k = 0; i < height; ++i) { for (j = 0; j < width; ++j) { img2_data[i * step + j] = (uchar)ifft[k++][0]; } } /* display images */ cvNamedWindow("original_image", CV_WINDOW_AUTOSIZE); cvNamedWindow("IFFT", CV_WINDOW_AUTOSIZE); cvShowImage("original_image", img1); cvShowImage("IFFT", img2); cvWaitKey(0); /* free memory */ cvDestroyWindow("original_image"); cvDestroyWindow("IFFT"); cvReleaseImage(&img1); cvReleaseImage(&img2); fftw_destroy_plan(plan_f); fftw_destroy_plan(plan_b); fftw_free(data_in); fftw_free(fft); fftw_free(ifft);
标签:fftw
原文地址:http://blog.csdn.net/cyh706510441/article/details/46676123