2D-FFT及IFFT(C语言实现(转载) 下载本文

内容发布更新时间 : 2024/5/19 0:57:24星期一 下面是文章的全部内容请认真阅读。

2D-FFT及IFFT(C语言实现(转载)

FFT与IFFT有如下关系:

相应的2D-FFT与2D-IFFT的关系如下:

所以可以利用一个FFT核心函数实现2D-FFT与2D-IFFT。代码如下:

#include <stdio.h> #include <stdlib.h> #include <math.h> #define intsize sizeof(int)

#define complexsize sizeof(complex) #define PI 3.1415926 int *a,*b;

int nLen,init_nLen,mLen,init_mLen,N,M; FILE *dataFile; typedef struct{ float real; float image; }complex;

complex *A,*A_In,*W;

complex Add(complex, complex); complex Sub(complex, complex); complex Mul(complex, complex); int calculate_M(int); void reverse(int,int); void readData(); void fft(int,int); void Ifft();

void printResult_fft(); void printResult_Ifft(); int main() {

int i,j; readData();

A = (complex *)malloc(complexsize*nLen); reverse(nLen,N); for(i=0; i<mLen; i++) {

for(j=0; j<nLen; j++) {

A[j].real = A_In[i*nLen+b[j]].real; A[j].image = A_In[i*nLen+b[j]].image;

}

fft(nLen,N);

for(j=0; j<nLen; j++) {

A_In[i*nLen+j].real = A[j].real; A_In[i*nLen+j].image = A[j].image; } } free(a); free(b); free(A);

A = (complex *)malloc(complexsize*mLen); reverse(mLen,M); for(i=0; i<nLen; i++) {

for(j=0; j<mLen; j++) {

A[j].real = A_In[b[j]*nLen+i].real; A[j].image = A_In[b[j]*nLen+i].image; }

fft(mLen,M);