1、二维FFT相当于对行和列分别进行一维FFT运算。具体的实现办法如下:
先对各行逐一进行一维FFT,然后再对变换后的新矩阵的各列逐一进行一维FFT。相应的伪代码如下所示:
for (int i=0i<Mi++)
FFT_1D(ROW[i],N)
for (int j=0j<Nj++)
FFT_1D(COL[j],M)
其中,ROW[i]表示矩阵的第i行。注码搜意这只是一个简单的记法,并不能完全照抄。还需要通过一些语句来生成各行的数据。同理,COL[i]是对矩阵的第i列的一种简单表示方法。
所以,关键是一维FFT算法的实现。
2、例程:
#include <stdio.h>#include <math.h>
#include <stdlib.h>
#define N 1000
/*定义复数类型*/
typedef struct{
double real
double img
}complex
complex x[N], *W /*输入序列,变换核*/
int size_x=0 /*输入序列的大小,在本程序中仅限2的次幂*/
double PI /*圆周率*/
void fft() /*快速傅里叶变换*/
void initW() /*初始化变换核*/
void change() /*变址*/
void add(complex ,complex ,complex *) /*复数加法*/
void mul(complex ,complex ,complex *) /*复数乘法*/
void sub(complex ,complex ,complex *) /*复数减法*/
void output()
int main(){
int i /*输出结果*/
system("cls")
PI=atan(1)*4
printf("Please input the size of x:\n")
scanf("%d",&size_x)
printf("Please input the data in x[N]:\n")
for(i=0i<size_xi++)
scanf("%lf%lf",&x[i].real,&x[i].img)
initW()
fft()
output()
return 0
}
/*快速傅里叶变换*/
void fft(){
int i=0,j=0,k=0,l=0
complex up,down,product
change()
for(i=0i< log(size_x)/log(2) i++){ /*一级蝶形运算*/
l=1<<i
for(j=0j<size_xj+= 2*l ){ /*一组蝶形运算*/
for(k=0k<lk++){ /*一个蝶形运算*/
改弯mul(x[j+k+l],W[size_x*k/2/l],&product)
add(x[j+k],product,&up)
sub(x[j+k],product,&down)
x[j+k]=up
x[j+k+l]=down
}
}
}
}
/*初始化变换核*/
void initW(){
int i
W=(complex *)malloc(sizeof(complex) * size_x)
for(i=0i<size_xi++){
W[i].real=cos(2*PI/size_x*i)
W[i].img=-1*sin(2*PI/size_x*i)
}
}
/*变址计算,将x(n)码位倒置*/
void change(){
complex temp
unsigned short i=0,j=0,k=0
double t
for(i=0i<size_xi++){
k=ij=0
t=(log(size_x)/log(2))
while( (t--)>0 ){
j=j<<核模闷1
j|=(k & 1)
k=k>>1
}
if(j>i){
temp=x[i]
x[i]=x[j]
x[j]=temp
}
}
}
/*输出傅里叶变换的结果*/
void output(){
int i
printf("The result are as follows\n")
for(i=0i<size_xi++){
printf("%.4f",x[i].real)
if(x[i].img>=0.0001)printf("+%.4fj\n",x[i].img)
else if(fabs(x[i].img)<0.0001)printf("\n")
else printf("%.4fj\n",x[i].img)
}
}
void add(complex a,complex b,complex *c){
c->real=a.real+b.real
c->img=a.img+b.img
}
void mul(complex a,complex b,complex *c){
c->real=a.real*b.real - a.img*b.img
c->img=a.real*b.img + a.img*b.real
}
void sub(complex a,complex b,complex *c){
c->real=a.real-b.real
c->img=a.img-b.img
}
float ar[1024],ai[1024]/* 原始数据实部,虚部 */float a[2050]
void fft(int nn) /* nn数据长度 */
{
int n1,n2,i,j,k,l,m,s,l1
float t1,t2,x,y
float w1,w2,u1,u2,z
float fsin[10]={0.000000,1.000000,0.707107,0.3826834,0.1950903,0.09801713,0.04906767,0.02454123,0.01227154,0.00613588,}
float fcos[10]={-1.000000,0.000000,0.7071068,0.9238796,0.9807853,0.99518472,0.99879545,0.9996988,0.9999247,0.9999812,}
switch(nn)
{
case 1024: s=10break
case 512: s=9 break
case 256: s=8 break
}
n1=nn/2 n2=nn-1
j=1
for(i=1i<=nni++)
{
a[2*i]=ar[i-1]
a[2*i+1]=ai[i-1]
}
for(l=1l<n2l++)
{
if(l<j)
{
t1=a[2*j]
t2=a[2*j+1]
a[2*j]=a[2*l]
a[2*j+1]=a[2*l+1]
a[2*l]=t1
a[2*l+1]=t2
}
k=n1
while (k<j)
{
j=j-k
k=k/2
}
j=j+k
}
for(i=1i<=si++)
{
u1=1
u2=0
m=(1<<i)
k=m>缺哗芦>1
w1=fcos[i-1]
w2=-fsin[i-1]
for(j=1j<=kj++)
{
for(l=jl<nnl=l+m)
{
l1=l+k
t1=a[2*l1]*u1-a[2*l1+1]*u2
t2=a[2*l1]*u2+a[2*l1+1]*u1
a[2*l1]=a[2*l]-t1
a[2*l1+1]=a[2*l+1]-t2
a[2*l]=a[2*l]+t1
a[2*l+1]=a[2*l+1]+t2
}
z=u1*w1-u2*w2
u2=u1*w2+u2*w1
u1=z
}
}
for(i=1i<=nn/芦橘2i++)
{
ar[i]=4*a[2*i+2]/nn/* 实伏带部 */
ai[i]=-4*a[2*i+3]/nn/* 虚部 */
a[i]=4*sqrt(ar[i]*ar[i]+ai[i]*ai[i])/* 幅值 */
}
}
float ar[1024],ai[1024]/* 实部,虚部 */float a[2050] /* 实际值 */
void fft()
{
int n1,n2,i,j,k,l,m,s=10,nn=1024,l1
float t1,t2,x,y
float w1,w2,u1,u2,z
float fsin[10]={0.000000,1.000000,0.707107,0.3826834,0.1950903,0.09801713,0.04906767,0.02454123,0.01227154,0.00613588,}
float fcos[10]={-1.000000,0.000000,0.7071068,0.9238796,0.9807853,0.99518472,0.99879545,0.9996988,0.9999247,0.9999812,}
n1=nn/2 n2=nn-1
j=1
for(i=1i<=nni++)
{
a[2*i]=ar[i-1]
a[2*i+1]=ai[i-1]
}
for(l=1l<n2l++)
{
if(l<大芹拍j)
{
t1=a[2*j]
t2=a[2*j+1]
a[2*j]=a[2*l]
a[2*j+1]=a[2*l+1]
a[2*l]=t1
a[2*l+1]=t2
}
k=n1
while (k<j)
{
j=j-k
k=k/首圆2
}
j=j+k
}
for(i=1i<=si++)
{
u1=1
u2=0
m=(1<滚羡<i)
k=m>>1
w1=fcos[i-1]
w2=-fsin[i-1]
for(j=1j<=kj++)
{
for(l=jl<nnl=l+m)
{
l1=l+k
t1=a[2*l1]*u1-a[2*l1+1]*u2
t2=a[2*l1]*u2+a[2*l1+1]*u1
a[2*l1]=a[2*l]-t1
a[2*l1+1]=a[2*l+1]-t2
a[2*l]=a[2*l]+t1
a[2*l+1]=a[2*l+1]+t2
}
z=u1*w1-u2*w2
u2=u1*w2+u2*w1
u1=z
}
}
for(i=1i<=nn/2i++)
{
ar[i]=a[2*i+2]/nn
ai[i]=-a[2*i+3]/nn
a[i]=4*sqrt(ar[i]*ar[i]+ai[i]*ai[i])
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)